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