1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qtquick2_p.h"
41#include <private/qqmlengine_p.h>
42#include <private/qquickvaluetypes_p.h>
43#include <private/qquickitemsmodule_p.h>
44#include <private/qquickaccessiblefactory_p.h>
45
46#include <private/qqmldebugconnector_p.h>
47#include <private/qqmldebugserviceinterfaces_p.h>
48#include <private/qqmldebugstatesdelegate_p.h>
49#include <private/qqmlbinding_p.h>
50#include <private/qqmlcontext_p.h>
51#include <private/qquickapplication_p.h>
52#include <QtQuick/private/qquickpropertychanges_p.h>
53#include <QtQuick/private/qquickstate_p.h>
54#include <qqmlproperty.h>
55#include <QtCore/QPointer>
56
57#if QT_CONFIG(shortcut)
58Q_DECLARE_METATYPE(QKeySequence::StandardKey)
59#endif
60
61QT_BEGIN_NAMESPACE
62
63#if !QT_CONFIG(qml_debug)
64
65class QQmlQtQuick2DebugStatesDelegate : public QQmlDebugStatesDelegate {};
66
67#else
68
69class QQmlQtQuick2DebugStatesDelegate : public QQmlDebugStatesDelegate
70{
71public:
72 QQmlQtQuick2DebugStatesDelegate();
73 ~QQmlQtQuick2DebugStatesDelegate();
74 void buildStatesList(bool cleanList, const QList<QPointer<QObject> > &instances) override;
75 void updateBinding(QQmlContext *context,
76 const QQmlProperty &property,
77 const QVariant &expression, bool isLiteralValue,
78 const QString &fileName, int line, int column,
79 bool *isBaseState) override;
80 bool setBindingForInvalidProperty(QObject *object,
81 const QString &propertyName,
82 const QVariant &expression,
83 bool isLiteralValue) override;
84 void resetBindingForInvalidProperty(QObject *object,
85 const QString &propertyName) override;
86
87private:
88 void buildStatesList(QObject *obj);
89
90 QList<QPointer<QQuickState> > m_allStates;
91};
92
93QQmlQtQuick2DebugStatesDelegate::QQmlQtQuick2DebugStatesDelegate()
94{
95}
96
97QQmlQtQuick2DebugStatesDelegate::~QQmlQtQuick2DebugStatesDelegate()
98{
99}
100
101void QQmlQtQuick2DebugStatesDelegate::buildStatesList(bool cleanList,
102 const QList<QPointer<QObject> > &instances)
103{
104 if (cleanList)
105 m_allStates.clear();
106
107 //only root context has all instances
108 for (int ii = 0; ii < instances.count(); ++ii) {
109 buildStatesList(obj: instances.at(i: ii));
110 }
111}
112
113void QQmlQtQuick2DebugStatesDelegate::buildStatesList(QObject *obj)
114{
115 if (QQuickState *state = qobject_cast<QQuickState *>(object: obj)) {
116 m_allStates.append(t: state);
117 }
118
119 QObjectList children = obj->children();
120 for (int ii = 0; ii < children.count(); ++ii) {
121 buildStatesList(obj: children.at(i: ii));
122 }
123}
124
125void QQmlQtQuick2DebugStatesDelegate::updateBinding(QQmlContext *context,
126 const QQmlProperty &property,
127 const QVariant &expression, bool isLiteralValue,
128 const QString &fileName, int line, int column,
129 bool *inBaseState)
130{
131 Q_UNUSED(column);
132 typedef QPointer<QQuickState> QuickStatePointer;
133 QObject *object = property.object();
134 QString propertyName = property.name();
135 for (const QuickStatePointer& statePointer : qAsConst(t&: m_allStates)) {
136 if (QQuickState *state = statePointer.data()) {
137 // here we assume that the revert list on itself defines the base state
138 if (state->isStateActive() && state->containsPropertyInRevertList(target: object, name: propertyName)) {
139 *inBaseState = false;
140
141 QQmlBinding *newBinding = nullptr;
142 if (!isLiteralValue) {
143 newBinding = QQmlBinding::create(&QQmlPropertyPrivate::get(p: property)->core,
144 expression.toString(), object,
145 QQmlContextData::get(context), url: fileName,
146 lineNumber: line);
147 newBinding->setTarget(property);
148 }
149
150 state->changeBindingInRevertList(target: object, name: propertyName, binding: newBinding);
151
152 if (isLiteralValue)
153 state->changeValueInRevertList(target: object, name: propertyName, revertValue: expression);
154 }
155 }
156 }
157}
158
159bool QQmlQtQuick2DebugStatesDelegate::setBindingForInvalidProperty(QObject *object,
160 const QString &propertyName,
161 const QVariant &expression,
162 bool isLiteralValue)
163{
164 if (QQuickPropertyChanges *propertyChanges = qobject_cast<QQuickPropertyChanges *>(object)) {
165 if (isLiteralValue)
166 propertyChanges->changeValue(name: propertyName, value: expression);
167 else
168 propertyChanges->changeExpression(name: propertyName, expression: expression.toString());
169 return true;
170 } else {
171 return false;
172 }
173}
174
175void QQmlQtQuick2DebugStatesDelegate::resetBindingForInvalidProperty(QObject *object, const QString &propertyName)
176{
177 if (QQuickPropertyChanges *propertyChanges = qobject_cast<QQuickPropertyChanges *>(object)) {
178 propertyChanges->removeProperty(name: propertyName);
179 }
180}
181
182#endif // QT_CONFIG(qml_debug)
183
184void QQmlQtQuick2Module::defineModule()
185{
186 QQuick_initializeProviders();
187
188#if QT_CONFIG(shortcut)
189 qRegisterMetaType<QKeySequence::StandardKey>();
190#endif
191
192 QQuickValueTypes::registerValueTypes();
193 QQuickItemsModule::defineModule();
194
195#if QT_CONFIG(accessibility)
196 QAccessible::installFactory(&qQuickAccessibleFactory);
197#endif
198
199 QQmlEngineDebugService *debugService = QQmlDebugConnector::service<QQmlEngineDebugService>();
200 if (debugService)
201 debugService->setStatesDelegate(new QQmlQtQuick2DebugStatesDelegate);
202}
203
204void QQmlQtQuick2Module::undefineModule()
205{
206 QQuick_deinitializeProviders();
207}
208
209QT_END_NAMESPACE
210
211

source code of qtdeclarative/src/quick/qtquick2.cpp