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/qquickutilmodule_p.h> |
43 | #include <private/qquickvaluetypes_p.h> |
44 | #include <private/qquickitemsmodule_p.h> |
45 | #include <private/qquickaccessiblefactory_p.h> |
46 | |
47 | #include <private/qqmldebugconnector_p.h> |
48 | #include <private/qqmldebugserviceinterfaces_p.h> |
49 | #include <private/qqmldebugstatesdelegate_p.h> |
50 | #include <private/qqmlbinding_p.h> |
51 | #include <private/qqmlcontext_p.h> |
52 | #include <private/qquickapplication_p.h> |
53 | #include <QtQuick/private/qquickpropertychanges_p.h> |
54 | #include <QtQuick/private/qquickstate_p.h> |
55 | #include <qqmlproperty.h> |
56 | #include <QtCore/QPointer> |
57 | |
58 | QT_BEGIN_NAMESPACE |
59 | |
60 | #if !QT_CONFIG(qml_debug) |
61 | |
62 | class QQmlQtQuick2DebugStatesDelegate : public QQmlDebugStatesDelegate {}; |
63 | |
64 | #else |
65 | |
66 | class QQmlQtQuick2DebugStatesDelegate : public QQmlDebugStatesDelegate |
67 | { |
68 | public: |
69 | QQmlQtQuick2DebugStatesDelegate(); |
70 | ~QQmlQtQuick2DebugStatesDelegate(); |
71 | void buildStatesList(bool cleanList, const QList<QPointer<QObject> > &instances) override; |
72 | void updateBinding(QQmlContext *context, |
73 | const QQmlProperty &property, |
74 | const QVariant &expression, bool isLiteralValue, |
75 | const QString &fileName, int line, int column, |
76 | bool *isBaseState) override; |
77 | bool setBindingForInvalidProperty(QObject *object, |
78 | const QString &propertyName, |
79 | const QVariant &expression, |
80 | bool isLiteralValue) override; |
81 | void resetBindingForInvalidProperty(QObject *object, |
82 | const QString &propertyName) override; |
83 | |
84 | private: |
85 | void buildStatesList(QObject *obj); |
86 | |
87 | QList<QPointer<QQuickState> > m_allStates; |
88 | }; |
89 | |
90 | QQmlQtQuick2DebugStatesDelegate::QQmlQtQuick2DebugStatesDelegate() |
91 | { |
92 | } |
93 | |
94 | QQmlQtQuick2DebugStatesDelegate::~QQmlQtQuick2DebugStatesDelegate() |
95 | { |
96 | } |
97 | |
98 | void QQmlQtQuick2DebugStatesDelegate::buildStatesList(bool cleanList, |
99 | const QList<QPointer<QObject> > &instances) |
100 | { |
101 | if (cleanList) |
102 | m_allStates.clear(); |
103 | |
104 | //only root context has all instances |
105 | for (int ii = 0; ii < instances.count(); ++ii) { |
106 | buildStatesList(instances.at(ii)); |
107 | } |
108 | } |
109 | |
110 | void QQmlQtQuick2DebugStatesDelegate::buildStatesList(QObject *obj) |
111 | { |
112 | if (QQuickState *state = qobject_cast<QQuickState *>(obj)) { |
113 | m_allStates.append(state); |
114 | } |
115 | |
116 | QObjectList children = obj->children(); |
117 | for (int ii = 0; ii < children.count(); ++ii) { |
118 | buildStatesList(children.at(ii)); |
119 | } |
120 | } |
121 | |
122 | void QQmlQtQuick2DebugStatesDelegate::updateBinding(QQmlContext *context, |
123 | const QQmlProperty &property, |
124 | const QVariant &expression, bool isLiteralValue, |
125 | const QString &fileName, int line, int column, |
126 | bool *inBaseState) |
127 | { |
128 | Q_UNUSED(column); |
129 | typedef QPointer<QQuickState> QuickStatePointer; |
130 | QObject *object = property.object(); |
131 | QString propertyName = property.name(); |
132 | for (const QuickStatePointer& statePointer : qAsConst(m_allStates)) { |
133 | if (QQuickState *state = statePointer.data()) { |
134 | // here we assume that the revert list on itself defines the base state |
135 | if (state->isStateActive() && state->containsPropertyInRevertList(object, propertyName)) { |
136 | *inBaseState = false; |
137 | |
138 | QQmlBinding *newBinding = nullptr; |
139 | if (!isLiteralValue) { |
140 | newBinding = QQmlBinding::create(&QQmlPropertyPrivate::get(property)->core, |
141 | expression.toString(), object, |
142 | QQmlContextData::get(context), fileName, |
143 | line); |
144 | newBinding->setTarget(property); |
145 | } |
146 | |
147 | state->changeBindingInRevertList(object, propertyName, newBinding); |
148 | |
149 | if (isLiteralValue) |
150 | state->changeValueInRevertList(object, propertyName, expression); |
151 | } |
152 | } |
153 | } |
154 | } |
155 | |
156 | bool QQmlQtQuick2DebugStatesDelegate::setBindingForInvalidProperty(QObject *object, |
157 | const QString &propertyName, |
158 | const QVariant &expression, |
159 | bool isLiteralValue) |
160 | { |
161 | if (QQuickPropertyChanges *propertyChanges = qobject_cast<QQuickPropertyChanges *>(object)) { |
162 | if (isLiteralValue) |
163 | propertyChanges->changeValue(propertyName, expression); |
164 | else |
165 | propertyChanges->changeExpression(propertyName, expression.toString()); |
166 | return true; |
167 | } else { |
168 | return false; |
169 | } |
170 | } |
171 | |
172 | void QQmlQtQuick2DebugStatesDelegate::resetBindingForInvalidProperty(QObject *object, const QString &propertyName) |
173 | { |
174 | if (QQuickPropertyChanges *propertyChanges = qobject_cast<QQuickPropertyChanges *>(object)) { |
175 | propertyChanges->removeProperty(propertyName); |
176 | } |
177 | } |
178 | |
179 | #endif // QT_CONFIG(qml_debug) |
180 | |
181 | void QQmlQtQuick2Module::defineModule() |
182 | { |
183 | QQuick_initializeProviders(); |
184 | |
185 | QQuickUtilModule::defineModule(); |
186 | QQuickItemsModule::defineModule(); |
187 | |
188 | qmlRegisterUncreatableType<QQuickApplication>("QtQuick" ,2,0,"Application" , QQuickApplication::tr("Application is an abstract class" )); |
189 | |
190 | QQuickValueTypes::registerValueTypes(); |
191 | |
192 | #if QT_CONFIG(accessibility) |
193 | QAccessible::installFactory(&qQuickAccessibleFactory); |
194 | #endif |
195 | |
196 | QQmlEngineDebugService *debugService = QQmlDebugConnector::service<QQmlEngineDebugService>(); |
197 | if (debugService) |
198 | debugService->setStatesDelegate(new QQmlQtQuick2DebugStatesDelegate); |
199 | } |
200 | |
201 | void QQmlQtQuick2Module::undefineModule() |
202 | { |
203 | QQuick_deinitializeProviders(); |
204 | } |
205 | |
206 | QT_END_NAMESPACE |
207 | |
208 | |