1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QQMLBUILTINFUNCTIONS_P_H |
5 | #define QQMLBUILTINFUNCTIONS_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <private/qjsengine_p.h> |
19 | #include <private/qqmlglobal_p.h> |
20 | #include <private/qqmlplatform_p.h> |
21 | #include <private/qv4functionobject_p.h> |
22 | |
23 | #include <QtCore/qnamespace.h> |
24 | #include <QtCore/qdatetime.h> |
25 | #include <QtCore/qsize.h> |
26 | #include <QtCore/qrect.h> |
27 | #include <QtCore/qpoint.h> |
28 | |
29 | #include <QtQml/qqmlcomponent.h> |
30 | #include <QtQml/qqmlengine.h> |
31 | |
32 | QT_BEGIN_NAMESPACE |
33 | |
34 | class Q_QML_EXPORT QtObject : public QObject |
35 | { |
36 | Q_OBJECT |
37 | Q_PROPERTY(QQmlApplication *application READ application CONSTANT) |
38 | Q_PROPERTY(QQmlPlatform *platform READ platform CONSTANT) |
39 | Q_PROPERTY(QObject *inputMethod READ inputMethod CONSTANT) |
40 | Q_PROPERTY(QObject *styleHints READ styleHints CONSTANT) |
41 | |
42 | #if QT_CONFIG(translation) |
43 | Q_PROPERTY(QString uiLanguage READ uiLanguage WRITE setUiLanguage BINDABLE uiLanguageBindable) |
44 | #endif |
45 | |
46 | QML_NAMED_ELEMENT(Qt) |
47 | QML_SINGLETON |
48 | QML_EXTENDED_NAMESPACE(Qt) |
49 | |
50 | Q_CLASSINFO("QML.StrictArguments" , "true" ) |
51 | |
52 | public: |
53 | enum LoadingMode { Asynchronous = 0, Synchronous = 1 }; |
54 | Q_ENUM(LoadingMode); |
55 | |
56 | static QtObject *create(QQmlEngine *, QJSEngine *jsEngine); |
57 | |
58 | Q_INVOKABLE QJSValue include(const QString &url, const QJSValue &callback = QJSValue()) const; |
59 | Q_INVOKABLE bool isQtObject(const QJSValue &value) const; |
60 | |
61 | Q_INVOKABLE QVariant color(const QString &name) const; |
62 | Q_INVOKABLE QVariant rgba(double r, double g, double b, double a = 1) const; |
63 | Q_INVOKABLE QVariant hsla(double h, double s, double l, double a = 1) const; |
64 | Q_INVOKABLE QVariant hsva(double h, double s, double v, double a = 1) const; |
65 | Q_INVOKABLE bool colorEqual(const QVariant &lhs, const QVariant &rhs) const; |
66 | |
67 | Q_INVOKABLE QRectF rect(double x, double y, double width, double height) const; |
68 | Q_INVOKABLE QPointF point(double x, double y) const; |
69 | Q_INVOKABLE QSizeF size(double width, double height) const; |
70 | Q_INVOKABLE QVariant vector2d(double x, double y) const; |
71 | Q_INVOKABLE QVariant vector3d(double x, double y, double z) const; |
72 | Q_INVOKABLE QVariant vector4d(double x, double y, double z, double w) const; |
73 | Q_INVOKABLE QVariant quaternion(double scalar, double x, double y, double z) const; |
74 | |
75 | Q_INVOKABLE QVariant matrix4x4() const; |
76 | Q_INVOKABLE QVariant matrix4x4(double m11, double m12, double m13, double m14, |
77 | double m21, double m22, double m23, double m24, |
78 | double m31, double m32, double m33, double m34, |
79 | double m41, double m42, double m43, double m44) const; |
80 | Q_INVOKABLE QVariant matrix4x4(const QJSValue &value) const; |
81 | |
82 | Q_INVOKABLE QVariant lighter(const QJSValue &color, double factor = 1.5) const; |
83 | Q_INVOKABLE QVariant darker(const QJSValue &color, double factor = 2.0) const; |
84 | Q_INVOKABLE QVariant alpha(const QJSValue &baseColor, double value) const; |
85 | Q_INVOKABLE QVariant tint(const QJSValue &baseColor, const QJSValue &tintColor) const; |
86 | |
87 | Q_INVOKABLE QString formatDate(QDate date, const QString &format) const; |
88 | Q_INVOKABLE QString formatDate(const QDateTime &dateTime, const QString &format) const; |
89 | Q_INVOKABLE QString formatDate(const QString &string, const QString &format) const; |
90 | Q_INVOKABLE QString formatDate(QDate date, Qt::DateFormat format) const; |
91 | Q_INVOKABLE QString formatDate(const QDateTime &dateTime, Qt::DateFormat format) const; |
92 | Q_INVOKABLE QString formatDate(const QString &string, Qt::DateFormat format) const; |
93 | |
94 | Q_INVOKABLE QString formatTime(QTime time, const QString &format) const; |
95 | Q_INVOKABLE QString formatTime(const QDateTime &dateTime, const QString &format) const; |
96 | Q_INVOKABLE QString formatTime(const QString &time, const QString &format) const; |
97 | Q_INVOKABLE QString formatTime(QTime time, Qt::DateFormat format) const; |
98 | Q_INVOKABLE QString formatTime(const QDateTime &dateTime, Qt::DateFormat format) const; |
99 | Q_INVOKABLE QString formatTime(const QString &time, Qt::DateFormat format) const; |
100 | |
101 | Q_INVOKABLE QString formatDateTime(const QDateTime &date, const QString &format) const; |
102 | Q_INVOKABLE QString formatDateTime(const QString &string, const QString &format) const; |
103 | Q_INVOKABLE QString formatDateTime(const QDateTime &date, Qt::DateFormat format) const; |
104 | Q_INVOKABLE QString formatDateTime(const QString &string, Qt::DateFormat format) const; |
105 | |
106 | #if QT_CONFIG(qml_locale) |
107 | Q_INVOKABLE QString formatDate(QDate date, const QLocale &locale = QLocale(), |
108 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
109 | Q_INVOKABLE QString formatDate(const QDateTime &dateTime, const QLocale &locale = QLocale(), |
110 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
111 | Q_INVOKABLE QString formatDate(const QString &string, const QLocale &locale = QLocale(), |
112 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
113 | Q_INVOKABLE QString formatTime(QTime time, const QLocale &locale = QLocale(), |
114 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
115 | Q_INVOKABLE QString formatTime(const QDateTime &dateTime, const QLocale &locale = QLocale(), |
116 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
117 | Q_INVOKABLE QString formatTime(const QString &time, const QLocale &locale = QLocale(), |
118 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
119 | Q_INVOKABLE QString formatDateTime(const QDateTime &date, const QLocale &locale = QLocale(), |
120 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
121 | Q_INVOKABLE QString formatDateTime(const QString &string, const QLocale &locale = QLocale(), |
122 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
123 | Q_INVOKABLE QLocale locale() const; |
124 | Q_INVOKABLE QLocale locale(const QString &name) const; |
125 | #endif |
126 | |
127 | Q_INVOKABLE QUrl url(const QUrl &url) const; |
128 | Q_INVOKABLE QUrl resolvedUrl(const QUrl &url) const; |
129 | Q_INVOKABLE QUrl resolvedUrl(const QUrl &url, QObject *context) const; |
130 | Q_INVOKABLE bool openUrlExternally(const QUrl &url) const; |
131 | |
132 | Q_INVOKABLE QVariant font(const QJSValue &fontSpecifier) const; |
133 | Q_INVOKABLE QStringList fontFamilies() const; |
134 | |
135 | Q_INVOKABLE QString md5(const QString &data) const; |
136 | Q_INVOKABLE QString btoa(const QString &data) const; |
137 | Q_INVOKABLE QString atob(const QString &data) const; |
138 | |
139 | Q_INVOKABLE void quit() const; |
140 | Q_INVOKABLE void exit(int retCode) const; |
141 | |
142 | Q_INVOKABLE QObject *createQmlObject(const QString &qml, QObject *parent, |
143 | const QUrl &url = QUrl(QStringLiteral("inline" ))) const; |
144 | Q_INVOKABLE QQmlComponent *createComponent(const QUrl &url, QObject *parent) const; |
145 | Q_INVOKABLE QQmlComponent *createComponent( |
146 | const QUrl &url, QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous, |
147 | QObject *parent = nullptr) const; |
148 | |
149 | Q_INVOKABLE QQmlComponent *createComponent(const QString &moduleUri, |
150 | const QString &typeName, QObject *parent) const; |
151 | Q_INVOKABLE QQmlComponent *createComponent(const QString &moduleUri, const QString &typeName, |
152 | QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous, |
153 | QObject *parent = nullptr) const; |
154 | |
155 | Q_INVOKABLE QJSValue binding(const QJSValue &function) const; |
156 | Q_INVOKABLE void callLater(QQmlV4FunctionPtr args); |
157 | |
158 | #if QT_CONFIG(translation) |
159 | QString uiLanguage() const; |
160 | void setUiLanguage(const QString &uiLanguage); |
161 | QBindable<QString> uiLanguageBindable(); |
162 | #endif |
163 | |
164 | // Not const because created on first use, and parented to this. |
165 | QQmlPlatform *platform(); |
166 | QQmlApplication *application(); |
167 | |
168 | QObject *inputMethod() const; |
169 | QObject *styleHints() const; |
170 | |
171 | private: |
172 | friend struct QV4::ExecutionEngine; |
173 | |
174 | QtObject(QV4::ExecutionEngine *engine); |
175 | |
176 | QQmlEngine *qmlEngine() const { return m_engine->qmlEngine(); } |
177 | QJSEngine *jsEngine() const { return m_engine->jsEngine(); } |
178 | QV4::ExecutionEngine *v4Engine() const { return m_engine; } |
179 | |
180 | struct Contexts { |
181 | QQmlRefPointer<QQmlContextData> context; |
182 | QQmlRefPointer<QQmlContextData> effectiveContext; |
183 | }; |
184 | Contexts getContexts() const; |
185 | |
186 | QQmlPlatform *m_platform = nullptr; |
187 | QQmlApplication *m_application = nullptr; |
188 | |
189 | QV4::ExecutionEngine *m_engine = nullptr; |
190 | }; |
191 | |
192 | namespace QV4 { |
193 | |
194 | namespace Heap { |
195 | |
196 | struct ConsoleObject : Object { |
197 | void init(); |
198 | }; |
199 | |
200 | #define QQmlBindingFunctionMembers(class, Member) \ |
201 | Member(class, Pointer, JavaScriptFunctionObject *, bindingFunction) |
202 | DECLARE_HEAP_OBJECT(QQmlBindingFunction, JavaScriptFunctionObject) { |
203 | DECLARE_MARKOBJECTS(QQmlBindingFunction) |
204 | void init(const QV4::JavaScriptFunctionObject *bindingFunction); |
205 | }; |
206 | |
207 | } |
208 | |
209 | struct ConsoleObject : Object |
210 | { |
211 | V4_OBJECT2(ConsoleObject, Object) |
212 | |
213 | static ReturnedValue method_error(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
214 | static ReturnedValue method_log(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
215 | static ReturnedValue method_info(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
216 | static ReturnedValue method_profile(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
217 | static ReturnedValue method_profileEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
218 | static ReturnedValue method_time(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
219 | static ReturnedValue method_timeEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
220 | static ReturnedValue method_count(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
221 | static ReturnedValue method_trace(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
222 | static ReturnedValue method_warn(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
223 | static ReturnedValue method_assert(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
224 | static ReturnedValue method_exception(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
225 | |
226 | }; |
227 | |
228 | struct Q_QML_EXPORT GlobalExtensions { |
229 | static void init(Object *globalObject, QJSEngine::Extensions extensions); |
230 | |
231 | #if QT_CONFIG(translation) |
232 | static QString currentTranslationContext(ExecutionEngine *engine); |
233 | static ReturnedValue method_qsTranslate(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
234 | static ReturnedValue method_qsTranslateNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
235 | static ReturnedValue method_qsTr(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
236 | static ReturnedValue method_qsTrNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
237 | static ReturnedValue method_qsTrId(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
238 | static ReturnedValue method_qsTrIdNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
239 | #endif |
240 | static ReturnedValue method_gc(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
241 | |
242 | // on String:prototype |
243 | static ReturnedValue method_string_arg(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
244 | |
245 | }; |
246 | |
247 | struct QQmlBindingFunction : public QV4::JavaScriptFunctionObject |
248 | { |
249 | V4_OBJECT2(QQmlBindingFunction, JavaScriptFunctionObject) |
250 | |
251 | static ReturnedValue virtualCall( |
252 | const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
253 | |
254 | Heap::JavaScriptFunctionObject *bindingFunction() const { return d()->bindingFunction; } |
255 | QQmlSourceLocation currentLocation() const; // from caller stack trace |
256 | }; |
257 | |
258 | inline bool FunctionObject::isBinding() const |
259 | { |
260 | return d()->vtable() == QQmlBindingFunction::staticVTable(); |
261 | } |
262 | |
263 | } |
264 | |
265 | QT_END_NAMESPACE |
266 | |
267 | #endif // QQMLBUILTINFUNCTIONS_P_H |
268 | |