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
qquickglobal.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 BasysKom GmbH.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#include <QtQuick/private/qquickvaluetypes_p.h>
7#include <QtQuick/private/qquickapplication_p.h>
8#include <QtQuick/private/qquickstate_p.h>
9#include <QtQuick/private/qquickpropertychanges_p.h>
10#include <QtQuick/private/qquickitemsmodule_p.h>
11#if QT_CONFIG(accessibility)
12# include <QtQuick/private/qquickaccessiblefactory_p.h>
13#endif
14#include <QtGui/QGuiApplication>
15#include <QtGui/qdesktopservices.h>
16#include <QtGui/qfontdatabase.h>
17#include <QtGui/qstylehints.h>
18
19#include <QtQml/private/qqmlbinding_p.h>
20#include <QtQml/private/qqmldebugserviceinterfaces_p.h>
21#include <QtQml/private/qqmldebugstatesdelegate_p.h>
22#include <QtQml/private/qqmlglobal_p.h>
23#include <QtQml/private/qv4engine_p.h>
24#include <QtQml/private/qv4object_p.h>
25#include <QtQml/private/qqmlanybinding_p.h>
26
27#include <QtQml/qqmlextensionplugin.h>
28
29#include <QtCore/qiterable.h>
30#include <QtCore/qpointer.h>
31
32#ifdef Q_CC_MSVC
33// MSVC2010 warns about 'unused variable t', even if it's used in t->~T()
34# pragma warning( disable : 4189 )
35#endif
36
37QT_BEGIN_NAMESPACE
38
39#if QT_CONFIG(qml_debug)
40
41class QQmlQtQuick2DebugStatesDelegate : public QQmlDebugStatesDelegate
42{
43public:
44 QQmlQtQuick2DebugStatesDelegate();
45 ~QQmlQtQuick2DebugStatesDelegate();
46 void buildStatesList(bool cleanList, const QList<QPointer<QObject> > &instances) override;
47 void updateBinding(QQmlContext *context,
48 const QQmlProperty &property,
49 const QVariant &expression, bool isLiteralValue,
50 const QString &fileName, int line, int column,
51 bool *isBaseState) override;
52 bool setBindingForInvalidProperty(QObject *object,
53 const QString &propertyName,
54 const QVariant &expression,
55 bool isLiteralValue) override;
56 void resetBindingForInvalidProperty(QObject *object,
57 const QString &propertyName) override;
58
59private:
60 void buildStatesList(QObject *obj);
61
62 QList<QPointer<QQuickState> > m_allStates;
63};
64
65QQmlQtQuick2DebugStatesDelegate::QQmlQtQuick2DebugStatesDelegate()
66{
67}
68
69QQmlQtQuick2DebugStatesDelegate::~QQmlQtQuick2DebugStatesDelegate()
70{
71}
72
73void QQmlQtQuick2DebugStatesDelegate::buildStatesList(bool cleanList,
74 const QList<QPointer<QObject> > &instances)
75{
76 if (cleanList)
77 m_allStates.clear();
78
79 //only root context has all instances
80 for (int ii = 0; ii < instances.size(); ++ii) {
81 buildStatesList(instances.at(ii));
82 }
83}
84
85void QQmlQtQuick2DebugStatesDelegate::buildStatesList(QObject *obj)
86{
87 if (QQuickState *state = qobject_cast<QQuickState *>(obj)) {
88 m_allStates.append(state);
89 }
90
91 QObjectList children = obj->children();
92 for (int ii = 0; ii < children.size(); ++ii) {
93 buildStatesList(children.at(ii));
94 }
95}
96
97void QQmlQtQuick2DebugStatesDelegate::updateBinding(QQmlContext *context,
98 const QQmlProperty &property,
99 const QVariant &expression, bool isLiteralValue,
100 const QString &fileName, int line, int column,
101 bool *inBaseState)
102{
103 Q_UNUSED(column);
104 typedef QPointer<QQuickState> QuickStatePointer;
105 QObject *object = property.object();
106 QString propertyName = property.name();
107 for (const QuickStatePointer& statePointer : std::as_const(m_allStates)) {
108 if (QQuickState *state = statePointer.data()) {
109 // here we assume that the revert list on itself defines the base state
110 if (state->isStateActive() && state->containsPropertyInRevertList(object, propertyName)) {
111 *inBaseState = false;
112
113 QQmlAnyBinding newBinding;
114 if (!isLiteralValue) {
115 newBinding = QQmlAnyBinding::createFromCodeString(property,
116 expression.toString(), object,
117 QQmlContextData::get(context), fileName,
118 line);
119 }
120 state->changeBindingInRevertList(object, propertyName, newBinding);
121
122 if (isLiteralValue)
123 state->changeValueInRevertList(object, propertyName, expression);
124 }
125 }
126 }
127}
128
129bool QQmlQtQuick2DebugStatesDelegate::setBindingForInvalidProperty(QObject *object,
130 const QString &propertyName,
131 const QVariant &expression,
132 bool isLiteralValue)
133{
134 if (QQuickPropertyChanges *propertyChanges = qobject_cast<QQuickPropertyChanges *>(object)) {
135 if (isLiteralValue)
136 propertyChanges->changeValue(propertyName, expression);
137 else
138 propertyChanges->changeExpression(propertyName, expression.toString());
139 return true;
140 } else {
141 return false;
142 }
143}
144
145void QQmlQtQuick2DebugStatesDelegate::resetBindingForInvalidProperty(QObject *object, const QString &propertyName)
146{
147 if (QQuickPropertyChanges *propertyChanges = qobject_cast<QQuickPropertyChanges *>(object)) {
148 propertyChanges->removeProperty(propertyName);
149 }
150}
151
152static QQmlDebugStatesDelegate *statesDelegateFactory()
153{
154 return new QQmlQtQuick2DebugStatesDelegate;
155}
156
157#endif // QT_CONFIG(qml_debug)
158
159class QQuickColorProvider : public QQmlColorProvider
160{
161public:
162 QVariant colorFromString(const QString &s, bool *ok) override
163 {
164 QColor c = QColor::fromString(s);
165 if (c.isValid()) {
166 if (ok) *ok = true;
167 return QVariant(c);
168 }
169
170 if (ok) *ok = false;
171 return QVariant();
172 }
173
174 unsigned rgbaFromString(const QString &s, bool *ok) override
175 {
176 QColor c = QColor::fromString(s);
177 if (c.isValid()) {
178 if (ok) *ok = true;
179 return c.rgba();
180 }
181
182 if (ok) *ok = false;
183 return 0;
184 }
185
186 QString stringFromRgba(unsigned rgba)
187 {
188 QColor c(QColor::fromRgba(rgba));
189 if (c.isValid()) {
190 return QVariant(c).toString();
191 }
192
193 return QString();
194 }
195
196 QVariant fromRgbF(double r, double g, double b, double a) override
197 {
198 return QVariant(QColor::fromRgbF(r, g, b, a));
199 }
200
201 QVariant fromHslF(double h, double s, double l, double a) override
202 {
203 return QVariant(QColor::fromHslF(h, s, l, a));
204 }
205
206 QVariant fromHsvF(double h, double s, double v, double a) override
207 {
208 return QVariant(QColor::fromHsvF(h, s, v, a));
209 }
210
211 QVariant lighter(const QVariant &var, qreal factor) override
212 {
213 return QVariant::fromValue(
214 QQuickColorValueType(var.value<QColor>()).lighter(factor));
215 }
216
217 QVariant darker(const QVariant &var, qreal factor) override
218 {
219 return QVariant::fromValue(
220 QQuickColorValueType(var.value<QColor>()).darker(factor));
221 }
222
223 QVariant alpha(const QVariant &var, qreal value) override
224 {
225 return QVariant::fromValue(
226 QQuickColorValueType(var.value<QColor>()).alpha(value));
227 }
228
229 QVariant tint(const QVariant &baseVar, const QVariant &tintVar) override
230 {
231 return QVariant::fromValue(
232 QQuickColorValueType(baseVar.value<QColor>()).tint(tintVar.value<QColor>()));
233 }
234};
235
237{
238public:
239 QQuickApplication *application(QObject *parent) override
240 {
241 return new QQuickApplication(parent);
242 }
243
244#if QT_CONFIG(im)
246 {
249 return im;
250 }
251#endif
252
254 {
255 QStyleHints *sh = qGuiApp->styleHints();
256 QQmlEngine::setObjectOwnership(sh, QQmlEngine::CppOwnership);
257 return sh;
258 }
259
261 {
262 return QFontDatabase::families();
263 }
264
265 bool openUrlExternally(const QUrl &url) override
266 {
267#ifndef QT_NO_DESKTOPSERVICES
268 return QDesktopServices::openUrl(url);
269#else
270 Q_UNUSED(url);
271 return false;
272#endif
273 }
274
275 QString pluginName() const override
276 {
277 return QGuiApplication::platformName();
278 }
279};
280
281static QQuickColorProvider *getColorProvider()
282{
283 static QQuickColorProvider colorProvider;
284 return &colorProvider;
285}
286
288{
289 static QQuickGuiProvider guiProvider;
290 return &guiProvider;
291}
292
293// Don't let the linker remove the dependency on libQtQmlMeta
296
298{
299 volatile auto qtqml = &qml_register_types_QtQml;
300 Q_ASSERT(qtqml);
301
302 // This is used by QQuickPath, and on macOS it fails to automatically register.
303 qRegisterMetaType<QVector<QVector<QPointF>>>();
304
305 QQml_setColorProvider(getColorProvider());
306 QQml_setGuiProvider(getGuiProvider());
307
308 QQuickItemsModule::defineModule();
309
310#if QT_CONFIG(accessibility)
311 QAccessible::installFactory(&qQuickAccessibleFactory);
312#endif
313
314#if QT_CONFIG(qml_debug)
315 QQmlEngineDebugService::setStatesDelegateFactory(statesDelegateFactory);
316#endif
317}
318
320
321QT_END_NAMESPACE
QQuickApplication * application(QObject *parent) override
bool openUrlExternally(const QUrl &url) override
QString pluginName() const override
QStyleHints * styleHints() override
QStringList fontFamilies() override
static QQuickColorProvider * getColorProvider()
Q_GHS_KEEP_REFERENCE(qml_register_types_QtQml)
static QQuickGuiProvider * getGuiProvider()
void qml_register_types_QtQml()
void QQuick_initializeModule()
Q_GHS_KEEP_REFERENCE(QQuick_initializeModule)