378a4b8a54d30b1c0ec10c3b9547fa35ad41b678
[profile/ivi/qtdeclarative.git] / src / qtquick1 / util / qdeclarativetransitionmanager.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 "QtQuick1/private/qdeclarativetransitionmanager_p_p.h"
43
44 #include "QtQuick1/private/qdeclarativestate_p_p.h"
45 #include "QtQuick1/private/qdeclarativestate_p.h"
46
47 #include <QtDeclarative/private/qdeclarativebinding_p.h>
48 #include <QtDeclarative/private/qdeclarativeglobal_p.h>
49 #include <QtDeclarative/private/qdeclarativeproperty_p.h>
50
51 QT_BEGIN_NAMESPACE
52
53
54
55 DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG);
56
57 class QDeclarative1TransitionManagerPrivate
58 {
59 public:
60     QDeclarative1TransitionManagerPrivate()
61         : state(0) {}
62
63     void applyBindings();
64     typedef QList<QDeclarative1SimpleAction> SimpleActionList;
65     QDeclarative1State *state;
66     QDeclarativeGuard<QDeclarative1Transition> transition;
67     QDeclarative1StateOperation::ActionList bindingsList;
68     SimpleActionList completeList;
69 };
70
71 QDeclarative1TransitionManager::QDeclarative1TransitionManager()
72 : d(new QDeclarative1TransitionManagerPrivate)
73 {
74 }
75
76 void QDeclarative1TransitionManager::setState(QDeclarative1State *s)
77 {
78     d->state = s;
79 }
80
81 QDeclarative1TransitionManager::~QDeclarative1TransitionManager()
82 {
83     delete d; d = 0;
84 }
85
86 void QDeclarative1TransitionManager::complete() 
87 {
88     d->applyBindings();
89
90     for (int ii = 0; ii < d->completeList.count(); ++ii) {
91         const QDeclarativeProperty &prop = d->completeList.at(ii).property();
92         prop.write(d->completeList.at(ii).value());
93     }
94
95     d->completeList.clear();
96
97     if (d->state) 
98         static_cast<QDeclarative1StatePrivate*>(QObjectPrivate::get(d->state))->complete();
99 }
100
101 void QDeclarative1TransitionManagerPrivate::applyBindings()
102 {
103     foreach(const QDeclarative1Action &action, bindingsList) {
104         if (!action.toBinding.isNull()) {
105             QDeclarativePropertyPrivate::setBinding(action.property, action.toBinding.data());
106         } else if (action.event) {
107             if (action.reverseEvent)
108                 action.event->reverse();
109             else
110                 action.event->execute();
111         }
112
113     }
114
115     bindingsList.clear();
116 }
117
118 void QDeclarative1TransitionManager::transition(const QList<QDeclarative1Action> &list,
119                                       QDeclarative1Transition *transition)
120 {
121     cancel();
122
123     QDeclarative1StateOperation::ActionList applyList = list;
124     // Determine which actions are binding changes.
125     foreach(const QDeclarative1Action &action, applyList) {
126         if (action.toBinding)
127             d->bindingsList << action;
128         if (action.fromBinding)
129             QDeclarativePropertyPrivate::setBinding(action.property, 0); // Disable current binding
130         if (action.event && action.event->changesBindings()) {  //### assume isReversable()?
131             d->bindingsList << action;
132             action.event->clearBindings();
133         }
134     }
135
136     // Animated transitions need both the start and the end value for
137     // each property change.  In the presence of bindings, the end values
138     // are non-trivial to calculate.  As a "best effort" attempt, we first
139     // apply all the property and binding changes, then read all the actual
140     // final values, then roll back the changes and proceed as normal.
141     //
142     // This doesn't catch everything, and it might be a little fragile in
143     // some cases - but whatcha going to do?
144
145     if (!d->bindingsList.isEmpty()) {
146
147         // Apply all the property and binding changes
148         for (int ii = 0; ii < applyList.size(); ++ii) {
149             const QDeclarative1Action &action = applyList.at(ii);
150             if (!action.toBinding.isNull()) {
151                 QDeclarativePropertyPrivate::setBinding(action.property, action.toBinding.data(), QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
152             } else if (!action.event) {
153                 QDeclarativePropertyPrivate::write(action.property, action.toValue, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
154             } else if (action.event->isReversable()) {
155                 if (action.reverseEvent)
156                     action.event->reverse(QDeclarative1ActionEvent::FastForward);
157                 else
158                     action.event->execute(QDeclarative1ActionEvent::FastForward);
159             }
160         }
161
162         // Read all the end values for binding changes
163         for (int ii = 0; ii < applyList.size(); ++ii) {
164             QDeclarative1Action *action = &applyList[ii];
165             if (action->event) {
166                 action->event->saveTargetValues();
167                 continue;
168             }
169             const QDeclarativeProperty &prop = action->property;
170             if (!action->toBinding.isNull() || !action->toValue.isValid()) {
171                 action->toValue = prop.read();
172             }
173         }
174
175         // Revert back to the original values
176         foreach(const QDeclarative1Action &action, applyList) {
177             if (action.event) {
178                 if (action.event->isReversable()) {
179                     action.event->clearBindings();
180                     action.event->rewind();
181                     action.event->clearBindings();  //### shouldn't be needed
182                 }
183                 continue;
184             }
185
186             if (action.toBinding)
187                 QDeclarativePropertyPrivate::setBinding(action.property, 0); // Make sure this is disabled during the transition
188
189             QDeclarativePropertyPrivate::write(action.property, action.fromValue, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
190         }
191     }
192
193     if (transition) {
194         QList<QDeclarativeProperty> touched;
195         d->transition = transition;
196         d->transition->prepare(applyList, touched, this);
197
198         // Modify the action list to remove actions handled in the transition
199         for (int ii = 0; ii < applyList.count(); ++ii) {
200             const QDeclarative1Action &action = applyList.at(ii);
201
202             if (action.event) {
203
204                 if (action.actionDone) {
205                     applyList.removeAt(ii);
206                     --ii;
207                 }
208
209             } else {
210
211                 if (touched.contains(action.property)) {
212                     if (action.toValue != action.fromValue) 
213                         d->completeList << 
214                             QDeclarative1SimpleAction(action, QDeclarative1SimpleAction::EndState);
215
216                     applyList.removeAt(ii);
217                     --ii;
218                 }
219
220             }
221         }
222     }
223
224     // Any actions remaining have not been handled by the transition and should
225     // be applied immediately.  We skip applying bindings, as they are all
226     // applied at the end in applyBindings() to avoid any nastiness mid 
227     // transition
228     foreach(const QDeclarative1Action &action, applyList) {
229         if (action.event && !action.event->changesBindings()) {
230             if (action.event->isReversable() && action.reverseEvent)
231                 action.event->reverse();
232             else
233                 action.event->execute();
234         } else if (!action.event && !action.toBinding) {
235             action.property.write(action.toValue);
236         }
237     }
238 #ifndef QT_NO_DEBUG_STREAM
239     if (stateChangeDebug()) {
240         foreach(const QDeclarative1Action &action, applyList) {
241             if (action.event)
242                 qWarning() << "    No transition for event:" << action.event->typeName();
243             else
244                 qWarning() << "    No transition for:" << action.property.object()
245                            << action.property.name() << "From:" << action.fromValue 
246                            << "To:" << action.toValue;
247         }
248     }
249 #endif
250     if (!transition)
251         d->applyBindings();
252 }
253
254 void QDeclarative1TransitionManager::cancel()
255 {
256     if (d->transition) {
257         // ### this could potentially trigger a complete in rare circumstances
258         d->transition->stop();
259         d->transition = 0;
260     }
261
262     for(int i = 0; i < d->bindingsList.count(); ++i) {
263         QDeclarative1Action action = d->bindingsList[i];
264         if (!action.toBinding.isNull() && action.deletableToBinding) {
265             QDeclarativePropertyPrivate::setBinding(action.property, 0);
266             action.toBinding.data()->destroy();
267             action.toBinding.clear();
268             action.deletableToBinding = false;
269         } else if (action.event) {
270             //### what do we do here?
271         }
272
273     }
274     d->bindingsList.clear();
275     d->completeList.clear();
276 }
277
278
279
280 QT_END_NAMESPACE