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