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