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 | QML_ADDED_IN_VERSION(2, 0) |
50 | |
51 | Q_CLASSINFO("QML.StrictArguments" , "true" ) |
52 | |
53 | public: |
54 | enum LoadingMode { Asynchronous = 0, Synchronous = 1 }; |
55 | Q_ENUM(LoadingMode); |
56 | |
57 | static QtObject *create(QQmlEngine *, QJSEngine *jsEngine); |
58 | |
59 | Q_INVOKABLE QJSValue include(const QString &url, const QJSValue &callback = QJSValue()) const; |
60 | Q_INVOKABLE bool isQtObject(const QJSValue &value) const; |
61 | |
62 | Q_INVOKABLE QVariant color(const QString &name) const; |
63 | Q_INVOKABLE QVariant rgba(double r, double g, double b, double a = 1) const; |
64 | Q_INVOKABLE QVariant hsla(double h, double s, double l, double a = 1) const; |
65 | Q_INVOKABLE QVariant hsva(double h, double s, double v, double a = 1) const; |
66 | Q_INVOKABLE bool colorEqual(const QVariant &lhs, const QVariant &rhs) const; |
67 | |
68 | Q_INVOKABLE QRectF rect(double x, double y, double width, double height) const; |
69 | Q_INVOKABLE QPointF point(double x, double y) const; |
70 | Q_INVOKABLE QSizeF size(double width, double height) const; |
71 | Q_INVOKABLE QVariant vector2d(double x, double y) const; |
72 | Q_INVOKABLE QVariant vector3d(double x, double y, double z) const; |
73 | Q_INVOKABLE QVariant vector4d(double x, double y, double z, double w) const; |
74 | Q_INVOKABLE QVariant quaternion(double scalar, double x, double y, double z) const; |
75 | |
76 | Q_INVOKABLE QVariant matrix4x4() const; |
77 | Q_INVOKABLE QVariant matrix4x4(double m11, double m12, double m13, double m14, |
78 | double m21, double m22, double m23, double m24, |
79 | double m31, double m32, double m33, double m34, |
80 | double m41, double m42, double m43, double m44) const; |
81 | Q_INVOKABLE QVariant matrix4x4(const QJSValue &value) const; |
82 | |
83 | Q_INVOKABLE QVariant lighter(const QJSValue &color, double factor = 1.5) const; |
84 | Q_INVOKABLE QVariant darker(const QJSValue &color, double factor = 2.0) const; |
85 | Q_INVOKABLE QVariant alpha(const QJSValue &baseColor, double value) const; |
86 | Q_INVOKABLE QVariant tint(const QJSValue &baseColor, const QJSValue &tintColor) const; |
87 | |
88 | Q_INVOKABLE QString formatDate(const QDate &date, const QString &format) const; |
89 | Q_INVOKABLE QString formatDate(const QDateTime &dateTime, const QString &format) const; |
90 | Q_INVOKABLE QString formatDate(const QString &string, const QString &format) const; |
91 | Q_INVOKABLE QString formatDate(const QDate &date, Qt::DateFormat format) const; |
92 | Q_INVOKABLE QString formatDate(const QDateTime &dateTime, Qt::DateFormat format) const; |
93 | Q_INVOKABLE QString formatDate(const QString &string, Qt::DateFormat format) const; |
94 | |
95 | Q_INVOKABLE QString formatTime(const QTime &time, const QString &format) const; |
96 | Q_INVOKABLE QString formatTime(const QDateTime &dateTime, const QString &format) const; |
97 | Q_INVOKABLE QString formatTime(const QString &time, const QString &format) const; |
98 | Q_INVOKABLE QString formatTime(const QTime &time, Qt::DateFormat format) const; |
99 | Q_INVOKABLE QString formatTime(const QDateTime &dateTime, Qt::DateFormat format) const; |
100 | Q_INVOKABLE QString formatTime(const QString &time, Qt::DateFormat format) const; |
101 | |
102 | Q_INVOKABLE QString formatDateTime(const QDateTime &date, const QString &format) const; |
103 | Q_INVOKABLE QString formatDateTime(const QString &string, const QString &format) const; |
104 | Q_INVOKABLE QString formatDateTime(const QDateTime &date, Qt::DateFormat format) const; |
105 | Q_INVOKABLE QString formatDateTime(const QString &string, Qt::DateFormat format) const; |
106 | |
107 | #if QT_CONFIG(qml_locale) |
108 | Q_INVOKABLE QString formatDate(const QDate &date, const QLocale &locale = QLocale(), |
109 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
110 | Q_INVOKABLE QString formatDate(const QDateTime &dateTime, const QLocale &locale = QLocale(), |
111 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
112 | Q_INVOKABLE QString formatDate(const QString &string, const QLocale &locale = QLocale(), |
113 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
114 | Q_INVOKABLE QString formatTime(const QTime &time, const QLocale &locale = QLocale(), |
115 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
116 | Q_INVOKABLE QString formatTime(const QDateTime &dateTime, const QLocale &locale = QLocale(), |
117 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
118 | Q_INVOKABLE QString formatTime(const QString &time, const QLocale &locale = QLocale(), |
119 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
120 | Q_INVOKABLE QString formatDateTime(const QDateTime &date, const QLocale &locale = QLocale(), |
121 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
122 | Q_INVOKABLE QString formatDateTime(const QString &string, const QLocale &locale = QLocale(), |
123 | QLocale::FormatType formatType = QLocale::ShortFormat) const; |
124 | Q_INVOKABLE QLocale locale() const; |
125 | Q_INVOKABLE QLocale locale(const QString &name) const; |
126 | #endif |
127 | |
128 | Q_INVOKABLE QUrl url(const QUrl &url) const; |
129 | Q_INVOKABLE QUrl resolvedUrl(const QUrl &url) const; |
130 | Q_INVOKABLE QUrl resolvedUrl(const QUrl &url, QObject *context) const; |
131 | Q_INVOKABLE bool openUrlExternally(const QUrl &url) const; |
132 | |
133 | Q_INVOKABLE QVariant font(const QJSValue &fontSpecifier) const; |
134 | Q_INVOKABLE QStringList fontFamilies() const; |
135 | |
136 | Q_INVOKABLE QString md5(const QString &data) const; |
137 | Q_INVOKABLE QString btoa(const QString &data) const; |
138 | Q_INVOKABLE QString atob(const QString &data) const; |
139 | |
140 | Q_INVOKABLE void quit() const; |
141 | Q_INVOKABLE void exit(int retCode) const; |
142 | |
143 | Q_INVOKABLE QObject *createQmlObject(const QString &qml, QObject *parent, |
144 | const QUrl &url = QUrl(QStringLiteral("inline" ))) const; |
145 | Q_INVOKABLE QQmlComponent *createComponent(const QUrl &url, QObject *parent) const; |
146 | Q_INVOKABLE QQmlComponent *createComponent( |
147 | const QUrl &url, QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous, |
148 | QObject *parent = nullptr) const; |
149 | |
150 | Q_INVOKABLE QQmlComponent *createComponent(const QString &moduleUri, |
151 | const QString &typeName, QObject *parent) const; |
152 | Q_INVOKABLE QQmlComponent *createComponent(const QString &moduleUri, const QString &typeName, |
153 | QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous, |
154 | QObject *parent = nullptr) const; |
155 | |
156 | Q_INVOKABLE QJSValue binding(const QJSValue &function) const; |
157 | Q_INVOKABLE void callLater(QQmlV4Function *args); |
158 | |
159 | #if QT_CONFIG(translation) |
160 | QString uiLanguage() const; |
161 | void setUiLanguage(const QString &uiLanguage); |
162 | QBindable<QString> uiLanguageBindable(); |
163 | #endif |
164 | |
165 | // Not const because created on first use, and parented to this. |
166 | QQmlPlatform *platform(); |
167 | QQmlApplication *application(); |
168 | |
169 | QObject *inputMethod() const; |
170 | QObject *styleHints() const; |
171 | |
172 | private: |
173 | friend struct QV4::ExecutionEngine; |
174 | |
175 | QtObject(QV4::ExecutionEngine *engine); |
176 | |
177 | QQmlEngine *qmlEngine() const { return m_engine->qmlEngine(); } |
178 | QJSEngine *jsEngine() const { return m_engine->jsEngine(); } |
179 | QV4::ExecutionEngine *v4Engine() const { return m_engine; } |
180 | |
181 | struct Contexts { |
182 | QQmlRefPointer<QQmlContextData> context; |
183 | QQmlRefPointer<QQmlContextData> effectiveContext; |
184 | }; |
185 | Contexts getContexts() const; |
186 | |
187 | QQmlPlatform *m_platform = nullptr; |
188 | QQmlApplication *m_application = nullptr; |
189 | |
190 | QV4::ExecutionEngine *m_engine = nullptr; |
191 | }; |
192 | |
193 | namespace QV4 { |
194 | |
195 | namespace Heap { |
196 | |
197 | struct ConsoleObject : Object { |
198 | void init(); |
199 | }; |
200 | |
201 | #define QQmlBindingFunctionMembers(class, Member) \ |
202 | Member(class, Pointer, FunctionObject *, bindingFunction) |
203 | DECLARE_HEAP_OBJECT(QQmlBindingFunction, FunctionObject) { |
204 | DECLARE_MARKOBJECTS(QQmlBindingFunction) |
205 | void init(const QV4::FunctionObject *bindingFunction); |
206 | }; |
207 | |
208 | } |
209 | |
210 | struct ConsoleObject : Object |
211 | { |
212 | V4_OBJECT2(ConsoleObject, Object) |
213 | |
214 | static ReturnedValue method_error(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
215 | static ReturnedValue method_log(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
216 | static ReturnedValue method_info(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
217 | static ReturnedValue method_profile(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
218 | static ReturnedValue method_profileEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
219 | static ReturnedValue method_time(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
220 | static ReturnedValue method_timeEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
221 | static ReturnedValue method_count(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
222 | static ReturnedValue method_trace(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
223 | static ReturnedValue method_warn(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
224 | static ReturnedValue method_assert(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
225 | static ReturnedValue method_exception(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
226 | |
227 | }; |
228 | |
229 | struct Q_QML_PRIVATE_EXPORT GlobalExtensions { |
230 | static void init(Object *globalObject, QJSEngine::Extensions extensions); |
231 | |
232 | #if QT_CONFIG(translation) |
233 | static QString currentTranslationContext(ExecutionEngine *engine); |
234 | static ReturnedValue method_qsTranslate(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
235 | static ReturnedValue method_qsTranslateNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
236 | static ReturnedValue method_qsTr(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
237 | static ReturnedValue method_qsTrNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
238 | static ReturnedValue method_qsTrId(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
239 | static ReturnedValue method_qsTrIdNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
240 | #endif |
241 | static ReturnedValue method_gc(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
242 | |
243 | // on String:prototype |
244 | static ReturnedValue method_string_arg(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
245 | |
246 | }; |
247 | |
248 | struct QQmlBindingFunction : public QV4::FunctionObject |
249 | { |
250 | V4_OBJECT2(QQmlBindingFunction, FunctionObject) |
251 | |
252 | Heap::FunctionObject *bindingFunction() const { return d()->bindingFunction; } |
253 | QQmlSourceLocation currentLocation() const; // from caller stack trace |
254 | }; |
255 | |
256 | inline bool FunctionObject::isBinding() const |
257 | { |
258 | return d()->vtable() == QQmlBindingFunction::staticVTable(); |
259 | } |
260 | |
261 | } |
262 | |
263 | QT_END_NAMESPACE |
264 | |
265 | #endif // QQMLBUILTINFUNCTIONS_P_H |
266 | |