Qt 4.8
qdeclarativetransitionmanager.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
52 
53 DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG);
54 
56 {
57 public:
59  : state(0) {}
60 
61  void applyBindings();
66  SimpleActionList completeList;
67 };
68 
71 {
72 }
73 
75 {
76  d->state = s;
77 }
78 
80 {
81  delete d; d = 0;
82 }
83 
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)
97 }
98 
100 {
101  foreach(const QDeclarativeAction &action, bindingsList) {
102  if (!action.toBinding.isNull()) {
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 
118 {
119  cancel();
120 
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()) {
150  } else if (!action.event) {
152  } else if (action.event->isReversable()) {
153  if (action.reverseEvent)
155  else
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 
188  }
189  }
190 
191  if (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 <<
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 
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) {
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 
double d
Definition: qnumeric_p.h:62
QDeclarativeProperty property
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDeclarativeTransitionManagerPrivate * d
void transition(const QList< QDeclarativeAction > &, QDeclarativeTransition *transition)
QDeclarativeGuard< QDeclarativeTransition > transition
virtual void destroy(DestroyMode mode=DisconnectBinding)
Destroy the binding.
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool isNull() const
Returns true if this object is holding a reference to a null pointer.
DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG)
T * data() const
static QObjectPrivate * get(QObject *o)
Definition: qobject_p.h:177
QList< QDeclarativeSimpleAction > SimpleActionList
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
QObject * object() const
Returns the QDeclarativeProperty&#39;s QObject.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
QDeclarativeStateOperation::ActionList bindingsList
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
Q_CORE_EXPORT void qWarning(const char *,...)
virtual void execute(Reason reason=ActualChange)
QWeakPointer< QDeclarativeAbstractBinding > toBinding
void clear()
Removes all items from the list.
Definition: qlist.h:764
static QDeclarativeAbstractBinding * setBinding(QObject *, int coreIndex, int valueTypeIndex, QDeclarativeAbstractBinding *, WriteFlags flags=DontRemoveBinding)
const QVariant & value() const
virtual QString typeName() const
void prepare(QDeclarativeStateOperation::ActionList &actions, QList< QDeclarativeProperty > &after, QDeclarativeTransitionManager *end)
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
void clear()
Clears this QWeakPointer object, dropping the reference that it may have had to the pointer...
const QDeclarativeProperty & property() const
The QDeclarativeProperty class abstracts accessing properties on objects created from QML...
bool write(const QVariant &) const
Sets the property value to value and returns true.
QDeclarativeActionEvent * event
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
QString name() const
Return the name of this QML property.
virtual void reverse(Reason reason=ActualChange)
QVariant read() const
Returns the property value.
static bool write(QObject *, const QDeclarativePropertyCache::Data &, const QVariant &, QDeclarativeContextData *, WriteFlags flags=0)
QDeclarativeAbstractBinding * fromBinding
void removeAt(int i)
Removes the item at index position i.
Definition: qlist.h:480