Create plugins for Particles and Window submodules
[profile/ivi/qtdeclarative.git] / src / quick / qtquick2.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qtquick2_p.h"
43 #include <private/qqmlengine_p.h>
44 #include <private/qquickutilmodule_p.h>
45 #include <private/qqmlvaluetype_p.h>
46 #include <private/qquickitemsmodule_p.h>
47
48 #include <private/qqmlenginedebugservice_p.h>
49 #include <private/qqmldebugstatesdelegate_p.h>
50 #include <private/qqmlbinding_p.h>
51 #include <private/qqmlcontext_p.h>
52 #include <QtQuick/private/qquickpropertychanges_p.h>
53 #include <QtQuick/private/qquickstate_p.h>
54 #include <qqmlproperty.h>
55 #include <QtCore/QWeakPointer>
56
57 QT_BEGIN_NAMESPACE
58
59 class QQmlQtQuick2DebugStatesDelegate : public QQmlDebugStatesDelegate
60 {
61 public:
62     QQmlQtQuick2DebugStatesDelegate();
63     virtual ~QQmlQtQuick2DebugStatesDelegate();
64     virtual void buildStatesList(bool cleanList, const QList<QPointer<QObject> > &instances);
65     virtual void updateBinding(QQmlContext *context,
66                                const QQmlProperty &property,
67                                const QVariant &expression, bool isLiteralValue,
68                                const QString &fileName, int line, int column,
69                                bool *isBaseState);
70     virtual bool setBindingForInvalidProperty(QObject *object,
71                                               const QString &propertyName,
72                                               const QVariant &expression,
73                                               bool isLiteralValue);
74     virtual void resetBindingForInvalidProperty(QObject *object,
75                                                 const QString &propertyName);
76
77 private:
78     void buildStatesList(QObject *obj);
79
80     QList<QWeakPointer<QQuickState> > m_allStates;
81 };
82
83 QQmlQtQuick2DebugStatesDelegate::QQmlQtQuick2DebugStatesDelegate()
84 {
85 }
86
87 QQmlQtQuick2DebugStatesDelegate::~QQmlQtQuick2DebugStatesDelegate()
88 {
89 }
90
91 void QQmlQtQuick2DebugStatesDelegate::buildStatesList(bool cleanList,
92                                                       const QList<QPointer<QObject> > &instances)
93 {
94     if (cleanList)
95         m_allStates.clear();
96
97     //only root context has all instances
98     for (int ii = 0; ii < instances.count(); ++ii) {
99         buildStatesList(instances.at(ii));
100     }
101 }
102
103 void QQmlQtQuick2DebugStatesDelegate::buildStatesList(QObject *obj)
104 {
105     if (QQuickState *state = qobject_cast<QQuickState *>(obj)) {
106         m_allStates.append(state);
107     }
108
109     QObjectList children = obj->children();
110     for (int ii = 0; ii < children.count(); ++ii) {
111         buildStatesList(children.at(ii));
112     }
113 }
114
115 void QQmlQtQuick2DebugStatesDelegate::updateBinding(QQmlContext *context,
116                                                             const QQmlProperty &property,
117                                                             const QVariant &expression, bool isLiteralValue,
118                                                             const QString &fileName, int line, int column,
119                                                             bool *inBaseState)
120 {
121     QObject *object = property.object();
122     QString propertyName = property.name();
123     foreach (QWeakPointer<QQuickState> statePointer, m_allStates) {
124         if (QQuickState *state = statePointer.data()) {
125             // here we assume that the revert list on itself defines the base state
126             if (state->isStateActive() && state->containsPropertyInRevertList(object, propertyName)) {
127                 *inBaseState = false;
128
129                 QQmlBinding *newBinding = 0;
130                 if (!isLiteralValue) {
131                     newBinding = new QQmlBinding(expression.toString(), false, object,
132                                                  QQmlContextData::get(context), fileName,
133                                                  line, column);
134                     newBinding->setTarget(property);
135                     newBinding->setNotifyOnValueChanged(true);
136                 }
137
138                 state->changeBindingInRevertList(object, propertyName, newBinding);
139
140                 if (isLiteralValue)
141                     state->changeValueInRevertList(object, propertyName, expression);
142             }
143         }
144     }
145 }
146
147 bool QQmlQtQuick2DebugStatesDelegate::setBindingForInvalidProperty(QObject *object,
148                                                                            const QString &propertyName,
149                                                                            const QVariant &expression,
150                                                                            bool isLiteralValue)
151 {
152     if (QQuickPropertyChanges *propertyChanges = qobject_cast<QQuickPropertyChanges *>(object)) {
153         if (isLiteralValue)
154             propertyChanges->changeValue(propertyName, expression);
155         else
156             propertyChanges->changeExpression(propertyName, expression.toString());
157         return true;
158     } else {
159         return false;
160     }
161 }
162
163 void QQmlQtQuick2DebugStatesDelegate::resetBindingForInvalidProperty(QObject *object, const QString &propertyName)
164 {
165     if (QQuickPropertyChanges *propertyChanges = qobject_cast<QQuickPropertyChanges *>(object)) {
166         propertyChanges->removeProperty(propertyName);
167     }
168 }
169
170
171 void QQmlQtQuick2Module::defineModule()
172 {
173     QQuickUtilModule::defineModule();
174     QQmlEnginePrivate::defineModule();
175     QQuickItemsModule::defineModule();
176     QQmlValueTypeFactory::registerValueTypes();
177
178     if (QQmlEngineDebugService::isDebuggingEnabled()) {
179         QQmlEngineDebugService::instance()->setStatesDelegate(
180                     new QQmlQtQuick2DebugStatesDelegate);
181     }
182 }
183
184 QT_END_NAMESPACE
185