| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtDeclarative module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "private/qdeclarativetransitionmanager_p_p.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "private/qdeclarativestate_p_p.h" | 
|---|
| 45 | #include "private/qdeclarativestate_p.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include <qdeclarativebinding_p.h> | 
|---|
| 48 | #include <qdeclarativeglobal_p.h> | 
|---|
| 49 | #include <qdeclarativeproperty_p.h> | 
|---|
| 50 |  | 
|---|
| 51 | QT_BEGIN_NAMESPACE | 
|---|
| 52 |  | 
|---|
| 53 | DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG); | 
|---|
| 54 |  | 
|---|
| 55 | class QDeclarativeTransitionManagerPrivate | 
|---|
| 56 | { | 
|---|
| 57 | public: | 
|---|
| 58 | QDeclarativeTransitionManagerPrivate() | 
|---|
| 59 | : state(0), transition(0) {} | 
|---|
| 60 |  | 
|---|
| 61 | void applyBindings(); | 
|---|
| 62 | typedef QList<QDeclarativeSimpleAction> SimpleActionList; | 
|---|
| 63 | QDeclarativeState *state; | 
|---|
| 64 | QDeclarativeTransition *transition; | 
|---|
| 65 | QDeclarativeStateOperation::ActionList bindingsList; | 
|---|
| 66 | SimpleActionList completeList; | 
|---|
| 67 | }; | 
|---|
| 68 |  | 
|---|
| 69 | QDeclarativeTransitionManager::QDeclarativeTransitionManager() | 
|---|
| 70 | : d(new QDeclarativeTransitionManagerPrivate) | 
|---|
| 71 | { | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | void QDeclarativeTransitionManager::setState(QDeclarativeState *s) | 
|---|
| 75 | { | 
|---|
| 76 | d->state = s; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | QDeclarativeTransitionManager::~QDeclarativeTransitionManager() | 
|---|
| 80 | { | 
|---|
| 81 | delete d; d = 0; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void QDeclarativeTransitionManager::complete() | 
|---|
| 85 | { | 
|---|
| 86 | d->applyBindings(); | 
|---|
| 87 |  | 
|---|
| 88 | for (int ii = 0; ii < d->completeList.count(); ++ii) { | 
|---|
| 89 | const QDeclarativeProperty &prop = d->completeList.at(ii).property(); | 
|---|
| 90 | prop.write(d->completeList.at(ii).value()); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | d->completeList.clear(); | 
|---|
| 94 |  | 
|---|
| 95 | if (d->state) | 
|---|
| 96 | static_cast<QDeclarativeStatePrivate*>(QObjectPrivate::get(d->state))->complete(); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void QDeclarativeTransitionManagerPrivate::applyBindings() | 
|---|
| 100 | { | 
|---|
| 101 | foreach(const QDeclarativeAction &action, bindingsList) { | 
|---|
| 102 | if (!action.toBinding.isNull()) { | 
|---|
| 103 | QDeclarativePropertyPrivate::setBinding(action.property, action.toBinding.data()); | 
|---|
| 104 | } else if (action.event) { | 
|---|
| 105 | if (action.reverseEvent) | 
|---|
| 106 | action.event->reverse(); | 
|---|
| 107 | else | 
|---|
| 108 | action.event->execute(); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | bindingsList.clear(); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void QDeclarativeTransitionManager::transition(const QList<QDeclarativeAction> &list, | 
|---|
| 117 | QDeclarativeTransition *transition) | 
|---|
| 118 | { | 
|---|
| 119 | cancel(); | 
|---|
| 120 |  | 
|---|
| 121 | QDeclarativeStateOperation::ActionList applyList = list; | 
|---|
| 122 | // Determine which actions are binding changes. | 
|---|
| 123 | foreach(const QDeclarativeAction &action, applyList) { | 
|---|
| 124 | if (action.toBinding) | 
|---|
| 125 | d->bindingsList << action; | 
|---|
| 126 | if (action.fromBinding) | 
|---|
| 127 | QDeclarativePropertyPrivate::setBinding(action.property, 0); // Disable current binding | 
|---|
| 128 | if (action.event && action.event->changesBindings()) {  //### assume isReversable()? | 
|---|
| 129 | d->bindingsList << action; | 
|---|
| 130 | action.event->clearBindings(); | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | // Animated transitions need both the start and the end value for | 
|---|
| 135 | // each property change.  In the presence of bindings, the end values | 
|---|
| 136 | // are non-trivial to calculate.  As a "best effort" attempt, we first | 
|---|
| 137 | // apply all the property and binding changes, then read all the actual | 
|---|
| 138 | // final values, then roll back the changes and proceed as normal. | 
|---|
| 139 | // | 
|---|
| 140 | // This doesn't catch everything, and it might be a little fragile in | 
|---|
| 141 | // some cases - but whatcha going to do? | 
|---|
| 142 |  | 
|---|
| 143 | if (!d->bindingsList.isEmpty()) { | 
|---|
| 144 |  | 
|---|
| 145 | // Apply all the property and binding changes | 
|---|
| 146 | for (int ii = 0; ii < applyList.size(); ++ii) { | 
|---|
| 147 | const QDeclarativeAction &action = applyList.at(ii); | 
|---|
| 148 | if (!action.toBinding.isNull()) { | 
|---|
| 149 | QDeclarativePropertyPrivate::setBinding(action.property, action.toBinding.data(), QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding); | 
|---|
| 150 | } else if (!action.event) { | 
|---|
| 151 | QDeclarativePropertyPrivate::write(action.property, action.toValue, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding); | 
|---|
| 152 | } else if (action.event->isReversable()) { | 
|---|
| 153 | if (action.reverseEvent) | 
|---|
| 154 | action.event->reverse(QDeclarativeActionEvent::FastForward); | 
|---|
| 155 | else | 
|---|
| 156 | action.event->execute(QDeclarativeActionEvent::FastForward); | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | // Read all the end values for binding changes | 
|---|
| 161 | for (int ii = 0; ii < applyList.size(); ++ii) { | 
|---|
| 162 | QDeclarativeAction *action = &applyList[ii]; | 
|---|
| 163 | if (action->event) { | 
|---|
| 164 | action->event->saveTargetValues(); | 
|---|
| 165 | continue; | 
|---|
| 166 | } | 
|---|
| 167 | const QDeclarativeProperty &prop = action->property; | 
|---|
| 168 | if (!action->toBinding.isNull() || !action->toValue.isValid()) { | 
|---|
| 169 | action->toValue = prop.read(); | 
|---|
| 170 | } | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | // Revert back to the original values | 
|---|
| 174 | foreach(const QDeclarativeAction &action, applyList) { | 
|---|
| 175 | if (action.event) { | 
|---|
| 176 | if (action.event->isReversable()) { | 
|---|
| 177 | action.event->clearBindings(); | 
|---|
| 178 | action.event->rewind(); | 
|---|
| 179 | action.event->clearBindings();  //### shouldn't be needed | 
|---|
| 180 | } | 
|---|
| 181 | continue; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | if (action.toBinding) | 
|---|
| 185 | QDeclarativePropertyPrivate::setBinding(action.property, 0); // Make sure this is disabled during the transition | 
|---|
| 186 |  | 
|---|
| 187 | QDeclarativePropertyPrivate::write(action.property, action.fromValue, QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::DontRemoveBinding); | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | if (transition) { | 
|---|
| 192 | QList<QDeclarativeProperty> touched; | 
|---|
| 193 | d->transition = transition; | 
|---|
| 194 | d->transition->prepare(applyList, touched, this); | 
|---|
| 195 |  | 
|---|
| 196 | // Modify the action list to remove actions handled in the transition | 
|---|
| 197 | for (int ii = 0; ii < applyList.count(); ++ii) { | 
|---|
| 198 | const QDeclarativeAction &action = applyList.at(ii); | 
|---|
| 199 |  | 
|---|
| 200 | if (action.event) { | 
|---|
| 201 |  | 
|---|
| 202 | if (action.actionDone) { | 
|---|
| 203 | applyList.removeAt(ii); | 
|---|
| 204 | --ii; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | } else { | 
|---|
| 208 |  | 
|---|
| 209 | if (touched.contains(action.property)) { | 
|---|
| 210 | if (action.toValue != action.fromValue) | 
|---|
| 211 | d->completeList << | 
|---|
| 212 | QDeclarativeSimpleAction(action, QDeclarativeSimpleAction::EndState); | 
|---|
| 213 |  | 
|---|
| 214 | applyList.removeAt(ii); | 
|---|
| 215 | --ii; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | } | 
|---|
| 219 | } | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | // Any actions remaining have not been handled by the transition and should | 
|---|
| 223 | // be applied immediately.  We skip applying bindings, as they are all | 
|---|
| 224 | // applied at the end in applyBindings() to avoid any nastiness mid | 
|---|
| 225 | // transition | 
|---|
| 226 | foreach(const QDeclarativeAction &action, applyList) { | 
|---|
| 227 | if (action.event && !action.event->changesBindings()) { | 
|---|
| 228 | if (action.event->isReversable() && action.reverseEvent) | 
|---|
| 229 | action.event->reverse(); | 
|---|
| 230 | else | 
|---|
| 231 | action.event->execute(); | 
|---|
| 232 | } else if (!action.event && !action.toBinding) { | 
|---|
| 233 | action.property.write(action.toValue); | 
|---|
| 234 | } | 
|---|
| 235 | } | 
|---|
| 236 | #ifndef QT_NO_DEBUG_STREAM | 
|---|
| 237 | if (stateChangeDebug()) { | 
|---|
| 238 | foreach(const QDeclarativeAction &action, applyList) { | 
|---|
| 239 | if (action.event) | 
|---|
| 240 | qWarning() << "    No transition for event:" << action.event->typeName(); | 
|---|
| 241 | else | 
|---|
| 242 | qWarning() << "    No transition for:" << action.property.object() | 
|---|
| 243 | << action.property.name() << "From:" << action.fromValue | 
|---|
| 244 | << "To:" << action.toValue; | 
|---|
| 245 | } | 
|---|
| 246 | } | 
|---|
| 247 | #endif | 
|---|
| 248 | if (!transition) | 
|---|
| 249 | d->applyBindings(); | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | void QDeclarativeTransitionManager::cancel() | 
|---|
| 253 | { | 
|---|
| 254 | if (d->transition) { | 
|---|
| 255 | // ### this could potentially trigger a complete in rare circumstances | 
|---|
| 256 | d->transition->stop(); | 
|---|
| 257 | d->transition = 0; | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | for(int i = 0; i < d->bindingsList.count(); ++i) { | 
|---|
| 261 | QDeclarativeAction action = d->bindingsList[i]; | 
|---|
| 262 | if (!action.toBinding.isNull() && action.deletableToBinding) { | 
|---|
| 263 | QDeclarativePropertyPrivate::setBinding(action.property, 0); | 
|---|
| 264 | action.toBinding.data()->destroy(); | 
|---|
| 265 | action.toBinding.clear(); | 
|---|
| 266 | action.deletableToBinding = false; | 
|---|
| 267 | } else if (action.event) { | 
|---|
| 268 | //### what do we do here? | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | } | 
|---|
| 272 | d->bindingsList.clear(); | 
|---|
| 273 | d->completeList.clear(); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | QT_END_NAMESPACE | 
|---|