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
149 Q_INVOKABLE QByteArray btoa(const QByteArray &data) const;
150 Q_INVOKABLE QByteArray atob(const QByteArray &data) const;
151
152 Q_INVOKABLE QByteArray btoa(const QVariantList &data) const;
153 Q_INVOKABLE QByteArray atob(const QVariantList &data) const;
154
155 Q_INVOKABLE void quit() const;
156 Q_INVOKABLE void exit(int retCode) const;
157
158 Q_INVOKABLE QObject *createQmlObject(const QString &qml, QObject *parent,
159 const QUrl &url = QUrl(QStringLiteral("inline"))) const;
160 Q_INVOKABLE QQmlComponent *createComponent(const QUrl &url, QObject *parent) const;
161 Q_INVOKABLE QQmlComponent *createComponent(
162 const QUrl &url, QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous,
163 QObject *parent = nullptr) const;
164
165 Q_INVOKABLE QQmlComponent *createComponent(const QString &moduleUri,
166 const QString &typeName, QObject *parent) const;
167 Q_INVOKABLE QQmlComponent *createComponent(const QString &moduleUri, const QString &typeName,
168 QQmlComponent::CompilationMode mode = QQmlComponent::PreferSynchronous,
169 QObject *parent = nullptr) const;
170
171 Q_INVOKABLE QJSValue binding(const QJSValue &function) const;
172 Q_INVOKABLE void callLater(QQmlV4FunctionPtr args);
173
174 Q_INVOKABLE double enumStringToValue(const QJSManagedValue &enumType, const QString &string);
175 Q_INVOKABLE QString enumValueToString(const QJSManagedValue &enumType, double value);
176 Q_INVOKABLE QStringList enumValueToStrings(const QJSManagedValue &enumType, double value);
177
178#if QT_CONFIG(translation)
179 QString uiLanguage() const;
180 void setUiLanguage(const QString &uiLanguage);
181 QBindable<QString> uiLanguageBindable();
182 Q_SIGNAL void uiLanguageChanged();
183#endif
184
185 // Not const because created on first use, and parented to this.
186 QQmlPlatform *platform();
187 QQmlApplication *application();
188
189 QObject *inputMethod() const;
190 QObject *styleHints() const;
191
192private:
193 friend struct QV4::ExecutionEngine;
194
195 QtObject(QV4::ExecutionEngine *engine);
196
197 QQmlEngine *qmlEngine() const { return m_engine->qmlEngine(); }
198 QJSEngine *jsEngine() const { return m_engine->jsEngine(); }
199 QV4::ExecutionEngine *v4Engine() const { return m_engine; }
200
201 struct Contexts {
202 QQmlRefPointer<QQmlContextData> context;
203 QQmlRefPointer<QQmlContextData> effectiveContext;
204 };
205 Contexts getContexts() const;
206
207 template<typename Ret, typename HandleScoped, typename HandleUnscoped>
208 Ret retrieveFromEnum(const QJSManagedValue &enumType, HandleScoped &&handleScoped,
209 HandleUnscoped &&handleUnscoped, QV4::ExecutionEngine *engine)
210 {
211 Q_ASSERT(engine);
212
213 // It's fine to hold a bare pointer to the internals of a QJSManagedValue
214 // The managed value keeps a QV4::PersistentValue after all
215 // (unless it's default-constructed).
216 QV4::Value *internal = QJSManagedValuePrivate::member(&enumType);
217
218 QV4::Heap::QQmlEnumWrapper *enumWrapper = nullptr;
219 if (auto *wrapper = internal ? internal->as<QV4::QQmlEnumWrapper>() : nullptr) {
220 enumWrapper = wrapper->d();
221 } else {
222 engine->throwTypeError("Invalid first argument, expected enum"_L1);
223 return Ret();
224 }
225
226 bool ok;
227 const QQmlType type = enumWrapper->type();
228 const int enumIndex = enumWrapper->enumIndex;
229 auto *typeLoader = m_engine->typeLoader();
230 const auto value = enumWrapper->scoped
231 ? handleScoped(type, typeLoader, enumIndex, &ok)
232 : handleUnscoped(type, typeLoader, enumIndex, &ok);
233
234 if (!ok)
235 engine->throwReferenceError("Invalid second argument, entry"_L1);
236
237 return value;
238 }
239
240 QQmlPlatform *m_platform = nullptr;
241 QQmlApplication *m_application = nullptr;
242
243 QV4::ExecutionEngine *m_engine = nullptr;
244};
245
246namespace QV4 {
247
248namespace Heap {
249
251 void init();
252};
253
254#define QQmlBindingFunctionMembers(class, Member)
255 Member(class, Pointer, JavaScriptFunctionObject *, bindingFunction)
257 DECLARE_MARKOBJECTS(QQmlBindingFunction)
258 void init(const QV4::JavaScriptFunctionObject *bindingFunction);
259};
260
261}
262
264{
265 V4_OBJECT2(ConsoleObject, Object)
266
267 static ReturnedValue method_error(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
268 static ReturnedValue method_log(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
269 static ReturnedValue method_info(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
270 static ReturnedValue method_profile(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
271 static ReturnedValue method_profileEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
272 static ReturnedValue method_time(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
273 static ReturnedValue method_timeEnd(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
274 static ReturnedValue method_count(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
275 static ReturnedValue method_trace(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
276 static ReturnedValue method_warn(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
277 static ReturnedValue method_assert(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
278 static ReturnedValue method_exception(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
279
280};
281
282struct Q_QML_EXPORT GlobalExtensions {
284
285#if QT_CONFIG(translation)
287 static ReturnedValue method_qsTranslate(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
289 static ReturnedValue method_qsTr(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
290 static ReturnedValue method_qsTrNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
291 static ReturnedValue method_qsTrId(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
292 static ReturnedValue method_qsTrIdNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
293#endif
294 static ReturnedValue method_gc(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
295
296 // on String:prototype
297 static ReturnedValue method_string_arg(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc);
298
299};
300
302{
303 V4_OBJECT2(QQmlBindingFunction, JavaScriptFunctionObject)
304
306 const FunctionObject *f, const Value *thisObject, const Value *argv, int argc);
307
308 Heap::JavaScriptFunctionObject *bindingFunction() const { return d()->bindingFunction; }
309 QQmlSourceLocation currentLocation() const; // from caller stack trace
310};
311
312inline bool FunctionObject::isBinding() const
313{
314 return d()->vtable() == QQmlBindingFunction::staticVTable();
315}
316
317}
318
319QT_END_NAMESPACE
320
321#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