1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qdeclarativetransitionmanager_p_p.h"
44 #include "qdeclarativetransition_p.h"
45 #include "qdeclarativestate_p_p.h"
46 #include "qdeclarativestate_p.h"
48 #include <private/qdeclarativebinding_p.h>
49 #include <private/qdeclarativeglobal_p.h>
50 #include <private/qdeclarativeproperty_p.h>
52 #include <QtCore/qdebug.h>
56 DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG);
58 class QDeclarativeTransitionManagerPrivate
61 QDeclarativeTransitionManagerPrivate()
65 typedef QList<QDeclarativeSimpleAction> SimpleActionList;
66 QDeclarativeState *state;
67 QDeclarativeGuard<QDeclarativeTransition> transition;
68 QDeclarativeStateOperation::ActionList bindingsList;
69 SimpleActionList completeList;
72 QDeclarativeTransitionManager::QDeclarativeTransitionManager()
73 : d(new QDeclarativeTransitionManagerPrivate)
77 void QDeclarativeTransitionManager::setState(QDeclarativeState *s)
82 QDeclarativeTransitionManager::~QDeclarativeTransitionManager()
87 void QDeclarativeTransitionManager::complete()
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());
96 d->completeList.clear();
99 static_cast<QDeclarativeStatePrivate*>(QObjectPrivate::get(d->state))->complete();
102 void QDeclarativeTransitionManagerPrivate::applyBindings()
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();
111 action.event->execute();
116 bindingsList.clear();
119 void QDeclarativeTransitionManager::transition(const QList<QDeclarativeAction> &list,
120 QDeclarativeTransition *transition)
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();
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.
143 // This doesn't catch everything, and it might be a little fragile in
144 // some cases - but whatcha going to do?
146 // Note that we only fast forward if both a transition and bindings are
147 // present, as it is unneccessary (and potentially expensive) otherwise.
149 if (transition && !d->bindingsList.isEmpty()) {
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);
162 action.event->execute(QDeclarativeActionEvent::FastForward);
166 // Read all the end values for binding changes
167 for (int ii = 0; ii < applyList.size(); ++ii) {
168 QDeclarativeAction *action = &applyList[ii];
170 action->event->saveTargetValues();
173 const QDeclarativeProperty &prop = action->property;
174 if (!action->toBinding.isNull() || !action->toValue.isValid()) {
175 action->toValue = prop.read();
179 // Revert back to the original values
180 foreach(const QDeclarativeAction &action, applyList) {
182 if (action.event->isReversable()) {
183 action.event->clearBindings();
184 action.event->rewind();
185 action.event->clearBindings(); //### shouldn't be needed
190 if (action.toBinding)
191 QDeclarativePropertyPrivate::setBinding(action.property, 0); // Make sure this is disabled during the transition
193 QDeclarativePropertyPrivate::write(action.property, action.fromValue, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
198 QList<QDeclarativeProperty> touched;
199 d->transition = transition;
200 d->transition->prepare(applyList, touched, this);
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);
208 if (action.actionDone) {
209 applyList.removeAt(ii);
215 if (touched.contains(action.property)) {
216 if (action.toValue != action.fromValue)
218 QDeclarativeSimpleAction(action, QDeclarativeSimpleAction::EndState);
220 applyList.removeAt(ii);
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
232 foreach(const QDeclarativeAction &action, applyList) {
233 if (action.event && !action.event->changesBindings()) {
234 if (action.event->isReversable() && action.reverseEvent)
235 action.event->reverse();
237 action.event->execute();
238 } else if (!action.event && !action.toBinding) {
239 action.property.write(action.toValue);
242 #ifndef QT_NO_DEBUG_STREAM
243 if (stateChangeDebug()) {
244 foreach(const QDeclarativeAction &action, applyList) {
246 qWarning() << " No transition for event:" << action.event->typeName();
248 qWarning() << " No transition for:" << action.property.object()
249 << action.property.name() << "From:" << action.fromValue
250 << "To:" << action.toValue;
258 void QDeclarativeTransitionManager::cancel()
261 // ### this could potentially trigger a complete in rare circumstances
262 d->transition->stop();
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?
278 d->bindingsList.clear();
279 d->completeList.clear();