8c855d5a4a70f6aa3dfe1248b6f1dcce180dd62c
[profile/ivi/qtdeclarative.git] / src / quick / qtquick2.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qtquick2_p.h"
43 #include <private/qdeclarativeengine_p.h>
44 #include <private/qdeclarativeutilmodule_p.h>
45 #include <private/qdeclarativevaluetype_p.h>
46 #include <private/qquickitemsmodule_p.h>
47 #include <private/qquickparticlesmodule_p.h>
48 #include <private/qquickwindowmodule_p.h>
49
50 #include <private/qdeclarativeenginedebugservice_p.h>
51 #include <private/qdeclarativedebugstatesdelegate_p.h>
52 #include <private/qdeclarativebinding_p.h>
53 #include <private/qdeclarativecontext_p.h>
54 #include <QtQuick/private/qdeclarativepropertychanges_p.h>
55 #include <QtQuick/private/qdeclarativestate_p.h>
56 #include <qdeclarativeproperty.h>
57 #include <QtCore/QWeakPointer>
58
59 QT_BEGIN_NAMESPACE
60
61 class QDeclarativeQtQuick2DebugStatesDelegate : public QDeclarativeDebugStatesDelegate
62 {
63 public:
64     QDeclarativeQtQuick2DebugStatesDelegate();
65     virtual ~QDeclarativeQtQuick2DebugStatesDelegate();
66     virtual void buildStatesList(QDeclarativeContext *ctxt, bool cleanList);
67     virtual void updateBinding(QDeclarativeContext *context,
68                                const QDeclarativeProperty &property,
69                                const QVariant &expression, bool isLiteralValue,
70                                const QString &fileName, int line, int column,
71                                bool *isBaseState);
72     virtual bool setBindingForInvalidProperty(QObject *object,
73                                               const QString &propertyName,
74                                               const QVariant &expression,
75                                               bool isLiteralValue);
76     virtual void resetBindingForInvalidProperty(QObject *object,
77                                                 const QString &propertyName);
78
79 private:
80     void buildStatesList(QObject *obj);
81
82     QList<QWeakPointer<QDeclarativeState> > m_allStates;
83 };
84
85 QDeclarativeQtQuick2DebugStatesDelegate::QDeclarativeQtQuick2DebugStatesDelegate()
86 {
87 }
88
89 QDeclarativeQtQuick2DebugStatesDelegate::~QDeclarativeQtQuick2DebugStatesDelegate()
90 {
91 }
92
93 void QDeclarativeQtQuick2DebugStatesDelegate::buildStatesList(QDeclarativeContext *ctxt, bool cleanList)
94 {
95     if (cleanList)
96         m_allStates.clear();
97
98     QDeclarativeContextPrivate *ctxtPriv = QDeclarativeContextPrivate::get(ctxt);
99     for (int ii = 0; ii < ctxtPriv->instances.count(); ++ii) {
100         buildStatesList(ctxtPriv->instances.at(ii));
101     }
102
103     QDeclarativeContextData *child = QDeclarativeContextData::get(ctxt)->childContexts;
104     while (child) {
105         buildStatesList(child->asQDeclarativeContext());
106         child = child->nextChild;
107     }
108 }
109
110 void QDeclarativeQtQuick2DebugStatesDelegate::buildStatesList(QObject *obj)
111 {
112     if (QDeclarativeState *state = qobject_cast<QDeclarativeState *>(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 QDeclarativeQtQuick2DebugStatesDelegate::updateBinding(QDeclarativeContext *context,
123                                                             const QDeclarativeProperty &property,
124                                                             const QVariant &expression, bool isLiteralValue,
125                                                             const QString &fileName, int line, int column,
126                                                             bool *inBaseState)
127 {
128     QObject *object = property.object();
129     QString propertyName = property.name();
130     foreach (QWeakPointer<QDeclarativeState> statePointer, m_allStates) {
131         if (QDeclarativeState *state = statePointer.data()) {
132             // here we assume that the revert list on itself defines the base state
133             if (state->isStateActive() && state->containsPropertyInRevertList(object, propertyName)) {
134                 *inBaseState = false;
135
136                 QDeclarativeBinding *newBinding = 0;
137                 if (!isLiteralValue) {
138                     newBinding = new QDeclarativeBinding(expression.toString(), object, context);
139                     newBinding->setTarget(property);
140                     newBinding->setNotifyOnValueChanged(true);
141                     newBinding->setSourceLocation(fileName, line, column);
142                 }
143
144                 state->changeBindingInRevertList(object, propertyName, newBinding);
145
146                 if (isLiteralValue)
147                     state->changeValueInRevertList(object, propertyName, expression);
148             }
149         }
150     }
151 }
152
153 bool QDeclarativeQtQuick2DebugStatesDelegate::setBindingForInvalidProperty(QObject *object,
154                                                                            const QString &propertyName,
155                                                                            const QVariant &expression,
156                                                                            bool isLiteralValue)
157 {
158     if (QDeclarativePropertyChanges *propertyChanges = qobject_cast<QDeclarativePropertyChanges *>(object)) {
159         if (isLiteralValue)
160             propertyChanges->changeValue(propertyName, expression);
161         else
162             propertyChanges->changeExpression(propertyName, expression.toString());
163         return true;
164     } else {
165         return false;
166     }
167 }
168
169 void QDeclarativeQtQuick2DebugStatesDelegate::resetBindingForInvalidProperty(QObject *object, const QString &propertyName)
170 {
171     if (QDeclarativePropertyChanges *propertyChanges = qobject_cast<QDeclarativePropertyChanges *>(object)) {
172         propertyChanges->removeProperty(propertyName);
173     }
174 }
175
176
177 void QDeclarativeQtQuick2Module::defineModule()
178 {
179     QDeclarativeUtilModule::defineModule();
180     QDeclarativeEnginePrivate::defineModule();
181     QQuickItemsModule::defineModule();
182     QQuickParticlesModule::defineModule();
183     QQuickWindowModule::defineModule();
184     QDeclarativeValueTypeFactory::registerValueTypes();
185
186     if (QDeclarativeEngineDebugService::isDebuggingEnabled()) {
187         QDeclarativeEngineDebugService::instance()->setStatesDelegate(
188                     new QDeclarativeQtQuick2DebugStatesDelegate);
189     }
190 }
191
192 QT_END_NAMESPACE
193