Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlbuiltinfunctions_p.h
Go to the documentation of this file.
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/qjsvalue_p.h>
20#include <private/qjsmanagedvalue_p.h>
21#include <private/qqmlengine_p.h>
22#include <private/qqmlglobal_p.h>
23#include <private/qqmlplatform_p.h>
24#include <private/qqmltypewrapper_p.h>
25#include <private/qv4functionobject_p.h>
26
27#include <QtCore/qnamespace.h>
28#include <QtCore/qdatetime.h>
29#include <QtCore/qsize.h>
30#include <QtCore/qrect.h>
31#include <QtCore/qpoint.h>
32
33#include <QtQml/qqmlcomponent.h>
34#include <QtQml/qqmlengine.h>
35
36QT_BEGIN_NAMESPACE
37
38using namespace Qt::StringLiterals;
39
42
43class Q_QML_EXPORT QtObject : public QObject
44{
45 Q_OBJECT
46 Q_PROPERTY(QQmlApplication *application READ application CONSTANT)
47 Q_PROPERTY(QQmlPlatform *platform READ platform CONSTANT)
48 Q_PROPERTY(QObject *inputMethod READ inputMethod CONSTANT)
49 Q_PROPERTY(QObject *styleHints READ styleHints CONSTANT)
50
51#if QT_CONFIG(translation)
52 Q_PROPERTY(QString uiLanguage READ uiLanguage WRITE setUiLanguage BINDABLE uiLanguageBindable NOTIFY uiLanguageChanged)
53#endif
54
55 QML_NAMED_ELEMENT(Qt)
56 QML_SINGLETON
57 QML_EXTENDED_NAMESPACE(Qt)
58
59 Q_CLASSINFO("QML.StrictArguments", "true")
60
61public:
62 enum LoadingMode { Asynchronous = 0, Synchronous = 1 };
63 Q_ENUM(LoadingMode);
64
65 static QtObject *create(QQmlEngine *, QJSEngine *jsEngine);
66
67 Q_INVOKABLE QJSValue include(const QString &url, const QJSValue &callback = QJSValue()) const;
68 Q_INVOKABLE bool isQtObject(const QJSValue &value) const;
69
70 Q_INVOKABLE QVariant color(const QString &name) const;
71 Q_INVOKABLE QVariant rgba(double r, double g, double b, double a = 1) const;
72 Q_INVOKABLE QVariant hsla(double h, double s, double l, double a = 1) const;
73 Q_INVOKABLE QVariant hsva(double h, double s, double v, double a = 1) const;
74 Q_INVOKABLE bool colorEqual(const QVariant &lhs, const QVariant &rhs) const;
75
76 Q_INVOKABLE QRectF rect(double x, double y, double width, double height) const;
77 Q_INVOKABLE QPointF point(double x, double y) const;
78 Q_INVOKABLE QSizeF size(double width, double height) const;
79 Q_INVOKABLE QVariant vector2d(double x, double y) const;
80 Q_INVOKABLE QVariant vector3d(double x, double y, double z) const;
81 Q_INVOKABLE QVariant vector4d(double x, double y, double z, double w) const;
82 Q_INVOKABLE QVariant quaternion(double scalar, double x, double y, double z) const;
83
84 Q_INVOKABLE QVariant matrix4x4() const;
85 Q_INVOKABLE QVariant matrix4x4(double m11, double m12, double m13, double m14,
86 double m21, double m22, double m23, double m24,
87 double m31, double m32, double m33, double m34,
88 double m41, double m42, double m43, double m44) const;
89 Q_INVOKABLE QVariant matrix4x4(const QJSValue &value) const;
90
91 Q_INVOKABLE QVariant lighter(const QJSValue &color, double factor = 1.5) const;
92 Q_INVOKABLE QVariant darker(const QJSValue &color, double factor = 2.0) const;
93 Q_INVOKABLE QVariant alpha(const QJSValue &baseColor, double value) const;
94 Q_INVOKABLE QVariant tint(const QJSValue &baseColor, const QJSValue &tintColor) const;
95
96 Q_INVOKABLE QString formatDate(QDate date, const QString &format) const;
97 Q_INVOKABLE QString formatDate(const QDateTime &dateTime, const QString &format) const;
98 Q_INVOKABLE QString formatDate(const QString &string, const QString &format) const;
99 Q_INVOKABLE QString formatDate(QDate date, Qt::DateFormat format) const;
100 Q_INVOKABLE QString formatDate(const QDateTime &dateTime, Qt::DateFormat format) const;
101 Q_INVOKABLE QString formatDate(const QString &string, Qt::DateFormat format) const;
102
103 Q_INVOKABLE QString formatTime(QTime time, const QString &format) const;
104 Q_INVOKABLE QString formatTime(const QDateTime &dateTime, const QString &format) const;
105 Q_INVOKABLE QString formatTime(const QString &time, const QString &format) const;
106 Q_INVOKABLE QString formatTime(QTime time, Qt::DateFormat format) const;
107 Q_INVOKABLE QString formatTime(const QDateTime &dateTime, Qt::DateFormat format) const;
108 Q_INVOKABLE QString formatTime(const QString &time, Qt::DateFormat format) const;
109
110 Q_INVOKABLE QString formatDateTime(const QDateTime &date, const QString &format) const;
111 Q_INVOKABLE QString formatDateTime(const QString &string, const QString &format) const;
112 Q_INVOKABLE QString formatDateTime(const QDateTime &date, Qt::DateFormat format) const;
113 Q_INVOKABLE QString formatDateTime(const QString &string, Qt::DateFormat format) const;
114
115#if QT_CONFIG(qml_locale)
116 Q_INVOKABLE QString formatDate(QDate date, const QLocale &locale = QLocale(),
117 QLocale::FormatType formatType = QLocale::ShortFormat) const;
118 Q_INVOKABLE QString formatDate(const QDateTime &dateTime, const QLocale &locale = QLocale(),
119 QLocale::FormatType formatType = QLocale::ShortFormat) const;
120 Q_INVOKABLE QString formatDate(const QString &string, const QLocale &locale = QLocale(),
121 QLocale::FormatType formatType = QLocale::ShortFormat) const;
122 Q_INVOKABLE QString formatTime(QTime time, const QLocale &locale = QLocale(),
123 QLocale::FormatType formatType = QLocale::ShortFormat) const;
124 Q_INVOKABLE QString formatTime(const QDateTime &dateTime, const QLocale &locale = QLocale(),
125 QLocale::FormatType formatType = QLocale::ShortFormat) const;
126 Q_INVOKABLE QString formatTime(const QString &time, const QLocale &locale = QLocale(),
127 QLocale::FormatType formatType = QLocale::ShortFormat) const;
128 Q_INVOKABLE QString formatDateTime(const QDateTime &date, const QLocale &locale = QLocale(),
129 QLocale::FormatType formatType = QLocale::ShortFormat) const;
130 Q_INVOKABLE QString formatDateTime(const QString &string, const QLocale &locale = QLocale(),
131 QLocale::FormatType formatType = QLocale::ShortFormat) const;
132 Q_INVOKABLE QLocale locale() const;
133 Q_INVOKABLE QLocale locale(const QString &name) const;
134#endif
135
136 Q_INVOKABLE QUrl url(const QUrl &url) const;
137 Q_INVOKABLE QUrl resolvedUrl(const QUrl &url) const;
138 Q_INVOKABLE QUrl resolvedUrl(const QUrl &url, QObject *context) const;
139 Q_INVOKABLE bool openUrlExternally(const QUrl &url) const;
140
141 Q_INVOKABLE QVariant font(const QJSValue &fontSpecifier) const;
142 Q_INVOKABLE QStringList fontFamilies() const;
143
144 Q_INVOKABLE QString md5(const QString &data) const;
145 Q_INVOKABLE QString btoa(const QString &data) const;
146 Q_INVOKABLE QString atob(const QString &data) const;
147
148 Q_INVOKABLE QByteArray btoa(const QByteArray &data) const;
149 Q_INVOKABLE QByteArray atob(const QByteArray &data) const;
150
151 Q_INVOKABLE QByteArray btoa(const QVariantList &data) const;
152 Q_INVOKABLE QByteArray atob(const QVariantList &data) const;
153
154 Q_INVOKABLE void quit() const;
155 Q_INVOKABLE void exit(int retCode) const;
156
157 Q_INVOKABLE QObject *createQmlObject(const QString &qml, QObject *parent,
158 const QUrl &url = QUrl(QStringLiteral("inline"))) const;
159 Q_INVOKABLE QQmlComponent *createComponent(const QUrl &url, QObject *parent) const;
160 Q_INVOKABLE QQmlComponent *createComponent(
161 const QUrl &url, QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous,
162 QObject *parent = nullptr) const;
163
164 Q_INVOKABLE QQmlComponent *createComponent(const QString &moduleUri,
165 const QString &typeName, QObject *parent) const;
166 Q_INVOKABLE QQmlComponent *createComponent(const QString &moduleUri, const QString &typeName,
167 QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous,
168 QObject *parent = nullptr) const;
169
170 Q_INVOKABLE QJSValue binding(const QJSValue &function) const;
171 Q_INVOKABLE void callLater(QQmlV4FunctionPtr args);
172
173 Q_INVOKABLE double enumStringToValue(const QJSManagedValue &enumType, const QString &string);
174 Q_INVOKABLE QString enumValueToString(const QJSManagedValue &enumType, double value);
175 Q_INVOKABLE QStringList enumValueToStrings(const QJSManagedValue &enumType, double value);
176
177#if QT_CONFIG(translation)
178 QString uiLanguage() const;
179 void setUiLanguage(const QString &uiLanguage);
180 QBindable<QString> uiLanguageBindable();
181 Q_SIGNAL void uiLanguageChanged();
182#endif
183
184 // Not const because created on first use, and parented to this.
185 QQmlPlatform *platform();
186 QQmlApplication *application();
187
188 QObject *inputMethod() const;
189 QObject *styleHints() const;
190
191private:
192 friend struct QV4::ExecutionEngine;
193
194 QtObject(QV4::ExecutionEngine *engine);
195
196 QQmlEngine *qmlEngine() const { return m_engine->qmlEngine(); }
197 QJSEngine *jsEngine() const { return m_engine->jsEngine(); }
198 QV4::ExecutionEngine *v4Engine() const { return m_engine; }
199
200 struct Contexts {
201 QQmlRefPointer<QQmlContextData> context;
202 QQmlRefPointer<QQmlContextData> effectiveContext;
203 };
204 Contexts getContexts() const;
205
206 template<typename Ret, typename HandleScoped, typename HandleUnscoped>
207 Ret retrieveFromEnum(const QJSManagedValue &enumType, HandleScoped &&handleScoped,
208 HandleUnscoped &&handleUnscoped, QV4::ExecutionEngine *engine)
209 {
210 Q_ASSERT(engine);
211
212 // It's fine to hold a bare pointer to the internals of a QJSManagedValue
213 // The managed value keeps a QV4::PersistentValue after all.
214 QV4::Value *internal = QJSManagedValuePrivate::member(&enumType);
215 Q_ASSERT(internal);
216
217 QV4::Heap::QQmlEnumWrapper *enumWrapper = nullptr;
218 if (QV4::QQmlEnumWrapper *wrapper = internal->as<QV4::QQmlEnumWrapper>()) {
219 enumWrapper = wrapper->d();
220 } else {
221 engine->throwTypeError("Invalid first argument, expected enum"_L1);
222 return Ret();
223 }
224
225 bool ok;
226 const QQmlType type = enumWrapper->type();
227 const int enumIndex = enumWrapper->enumIndex;
228 auto *typeLoader = m_engine->typeLoader();
229 const auto value = enumWrapper->scoped
230 ? handleScoped(type, typeLoader, enumIndex, &ok)
231 : handleUnscoped(type, typeLoader, enumIndex, &ok);
232
233 if (!ok)
234 engine->throwReferenceError("Invalid second argument, entry"_L1);
235
236 return value;
237 }
238
239 QQmlPlatform *m_platform = nullptr;
240 QQmlApplication *m_application = nullptr;
241
242 QV4::ExecutionEngine *m_engine = nullptr;
243};
244
245namespace QV4 {
246
247namespace Heap {
248
250 void init();
251};
252
253#define QQmlBindingFunctionMembers(class, Member)
254 Member(class, Pointer, JavaScriptFunctionObject *, bindingFunction)
256 DECLARE_MARKOBJECTS(QQmlBindingFunction)
257 void init(const QV4::JavaScriptFunctionObject *bindingFunction);
258};
259
260}
261
263{
264 V4_OBJECT2(ConsoleObject, Object)
265
266 static ReturnedValue method_error(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
267 static ReturnedValue method_log(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
268 static ReturnedValue method_info(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
269 static ReturnedValue method_profile(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
270 static ReturnedValue method_profileEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
271 static ReturnedValue method_time(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
272 static ReturnedValue method_timeEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
273 static ReturnedValue method_count(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
274 static ReturnedValue method_trace(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
275 static ReturnedValue method_warn(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
276 static ReturnedValue method_assert(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
277 static ReturnedValue method_exception(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
278
279};
280
281struct Q_QML_EXPORT GlobalExtensions {
283
284#if QT_CONFIG(translation)
286 static ReturnedValue method_qsTranslate(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
288 static ReturnedValue method_qsTr(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
289 static ReturnedValue method_qsTrNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
290 static ReturnedValue method_qsTrId(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
291 static ReturnedValue method_qsTrIdNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
292#endif
293 static ReturnedValue method_gc(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
294
295 // on String:prototype
296 static ReturnedValue method_string_arg(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
297
298};
299
301{
302 V4_OBJECT2(QQmlBindingFunction, JavaScriptFunctionObject)
303
305 const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
306
307 Heap::JavaScriptFunctionObject *bindingFunction() const { return d()->bindingFunction; }
308 QQmlSourceLocation currentLocation() const; // from caller stack trace
309};
310
311inline bool FunctionObject::isBinding() const
312{
313 return d()->vtable() == QQmlBindingFunction::staticVTable();
314}
315
316}
317
318QT_END_NAMESPACE
319
320#endif // QQMLBUILTINFUNCTIONS_P_H
DECLARE_HEAP_OBJECT(QmlContext, ExecutionContext)
Definition qjsvalue.h:23
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
DEFINE_OBJECT_VTABLE(QQmlBindingFunction)
static QByteArray convertVariantList(const QVariantList &data, QV4::ExecutionEngine *engine)
static QTime dateTimeToTime(const QDateTime &dateTime)
static QString serializeArray(Object *array, ExecutionEngine *v4, QSet< QV4::Heap::Object * > &alreadySeen)
void addParameters(QJSEngine *e, QJSValue &result, int i, T parameter, Others... others)
static QVariant constructFromJSValue(QJSEngine *e, QMetaType type, T... parameters)
void addParameters< double >(QJSEngine *, QJSValue &result, int i, double parameter)
static std::optional< QDate > dateFromString(const QString &string, QV4::ExecutionEngine *engine)
\qmlmethod string Qt::formatDate(datetime date, variant format, variant localeFormatOption)
static QV4::ReturnedValue throwInvalidCharacter(QV4::ExecutionEngine *engine)
DEFINE_OBJECT_VTABLE(ConsoleObject)
static ReturnedValue writeToConsole(const FunctionObject *b, const Value *argv, int argc, ConsoleLogTypes logType, bool printStack=false)
void addParameters(QJSEngine *e, QJSValue &result, int i, T parameter)
static QString jsStack(QV4::ExecutionEngine *engine)
static std::optional< QTime > timeFromString(const QString &string, QV4::ExecutionEngine *engine)
\qmlmethod string Qt::formatTime(datetime time, variant format, variant localeFormatOption)
static std::optional< QDateTime > dateTimeFromString(const QString &string, QV4::ExecutionEngine *engine)
\qmlmethod string Qt::formatDateTime(datetime dateTime, variant format, variant localeFormatOption)
static Q_DECL_COLD_FUNCTION void throw_invalid_compilation_mode(QV4::ExecutionEngine *engine, QQmlComponent::CompilationMode mode)
static QVariant colorVariantFromJSValue(const QJSValue &color, bool *ok)
Q_DECLARE_LOGGING_CATEGORY(lcQml)
Q_DECLARE_LOGGING_CATEGORY(lcJs)
static ReturnedValue method_profile(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_warn(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_log(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_count(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_info(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_timeEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_exception(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_assert(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_time(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_profileEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_trace(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
QQmlSourceLocation currentLocation() const
Heap::JavaScriptFunctionObject * bindingFunction() const