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