1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "QtQuick1/private/qdeclarativetransitionmanager_p_p.h"
44 #include "QtQuick1/private/qdeclarativestate_p_p.h"
45 #include "QtQuick1/private/qdeclarativestate_p.h"
47 #include <QtDeclarative/private/qdeclarativebinding_p.h>
48 #include <QtDeclarative/private/qdeclarativeglobal_p.h>
49 #include <QtDeclarative/private/qdeclarativeproperty_p.h>
55 DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG);
57 class QDeclarative1TransitionManagerPrivate
60 QDeclarative1TransitionManagerPrivate()
64 typedef QList<QDeclarative1SimpleAction> SimpleActionList;
65 QDeclarative1State *state;
66 QDeclarativeGuard<QDeclarative1Transition> transition;
67 QDeclarative1StateOperation::ActionList bindingsList;
68 SimpleActionList completeList;
71 QDeclarative1TransitionManager::QDeclarative1TransitionManager()
72 : d(new QDeclarative1TransitionManagerPrivate)
76 void QDeclarative1TransitionManager::setState(QDeclarative1State *s)
81 QDeclarative1TransitionManager::~QDeclarative1TransitionManager()
86 void QDeclarative1TransitionManager::complete()
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());
95 d->completeList.clear();
98 static_cast<QDeclarative1StatePrivate*>(QObjectPrivate::get(d->state))->complete();
101 void QDeclarative1TransitionManagerPrivate::applyBindings()
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();
110 action.event->execute();
115 bindingsList.clear();
118 void QDeclarative1TransitionManager::transition(const QList<QDeclarative1Action> &list,
119 QDeclarative1Transition *transition)
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();
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.
142 // This doesn't catch everything, and it might be a little fragile in
143 // some cases - but whatcha going to do?
145 if (!d->bindingsList.isEmpty()) {
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);
158 action.event->execute(QDeclarative1ActionEvent::FastForward);
162 // Read all the end values for binding changes
163 for (int ii = 0; ii < applyList.size(); ++ii) {
164 QDeclarative1Action *action = &applyList[ii];
166 action->event->saveTargetValues();
169 const QDeclarativeProperty &prop = action->property;
170 if (!action->toBinding.isNull() || !action->toValue.isValid()) {
171 action->toValue = prop.read();
175 // Revert back to the original values
176 foreach(const QDeclarative1Action &action, applyList) {
178 if (action.event->isReversable()) {
179 action.event->clearBindings();
180 action.event->rewind();
181 action.event->clearBindings(); //### shouldn't be needed
186 if (action.toBinding)
187 QDeclarativePropertyPrivate::setBinding(action.property, 0); // Make sure this is disabled during the transition
189 QDeclarativePropertyPrivate::write(action.property, action.fromValue, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding);
194 QList<QDeclarativeProperty> touched;
195 d->transition = transition;
196 d->transition->prepare(applyList, touched, this);
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);
204 if (action.actionDone) {
205 applyList.removeAt(ii);
211 if (touched.contains(action.property)) {
212 if (action.toValue != action.fromValue)
214 QDeclarative1SimpleAction(action, QDeclarative1SimpleAction::EndState);
216 applyList.removeAt(ii);
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
228 foreach(const QDeclarative1Action &action, applyList) {
229 if (action.event && !action.event->changesBindings()) {
230 if (action.event->isReversable() && action.reverseEvent)
231 action.event->reverse();
233 action.event->execute();
234 } else if (!action.event && !action.toBinding) {
235 action.property.write(action.toValue);
238 #ifndef QT_NO_DEBUG_STREAM
239 if (stateChangeDebug()) {
240 foreach(const QDeclarative1Action &action, applyList) {
242 qWarning() << " No transition for event:" << action.event->typeName();
244 qWarning() << " No transition for:" << action.property.object()
245 << action.property.name() << "From:" << action.fromValue
246 << "To:" << action.toValue;
254 void QDeclarative1TransitionManager::cancel()
257 // ### this could potentially trigger a complete in rare circumstances
258 d->transition->stop();
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?
274 d->bindingsList.clear();
275 d->completeList.clear();