|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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) { |
|
103 QDeclarativePropertyPrivate::setBinding(action.property, action.toBinding); |
|
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) { |
|
149 QDeclarativePropertyPrivate::setBinding(action.property, action.toBinding, 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 || !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(); |
|
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 if (stateChangeDebug()) { |
|
237 foreach(const QDeclarativeAction &action, applyList) { |
|
238 if (action.event) |
|
239 qWarning() << " No transition for event:" << action.event->typeName(); |
|
240 else |
|
241 qWarning() << " No transition for:" << action.property.object() |
|
242 << action.property.name() << "From:" << action.fromValue |
|
243 << "To:" << action.toValue; |
|
244 } |
|
245 } |
|
246 if (!transition) |
|
247 d->applyBindings(); |
|
248 } |
|
249 |
|
250 void QDeclarativeTransitionManager::cancel() |
|
251 { |
|
252 if (d->transition) { |
|
253 // ### this could potentially trigger a complete in rare circumstances |
|
254 d->transition->stop(); |
|
255 d->transition = 0; |
|
256 } |
|
257 |
|
258 for(int i = 0; i < d->bindingsList.count(); ++i) { |
|
259 QDeclarativeAction action = d->bindingsList[i]; |
|
260 if (action.toBinding && action.deletableToBinding) { |
|
261 QDeclarativePropertyPrivate::setBinding(action.property, 0); |
|
262 action.toBinding->destroy(); |
|
263 action.toBinding = 0; |
|
264 action.deletableToBinding = false; |
|
265 } else if (action.event) { |
|
266 //### what do we do here? |
|
267 } |
|
268 |
|
269 } |
|
270 d->bindingsList.clear(); |
|
271 d->completeList.clear(); |
|
272 } |
|
273 |
|
274 QT_END_NAMESPACE |