source: trunk/src/declarative/util/qdeclarativetransitionmanager.cpp@ 944

Last change on this file since 944 was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 10.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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 [email protected].
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
51QT_BEGIN_NAMESPACE
52
53DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG);
54
55class QDeclarativeTransitionManagerPrivate
56{
57public:
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
69QDeclarativeTransitionManager::QDeclarativeTransitionManager()
70: d(new QDeclarativeTransitionManagerPrivate)
71{
72}
73
74void QDeclarativeTransitionManager::setState(QDeclarativeState *s)
75{
76 d->state = s;
77}
78
79QDeclarativeTransitionManager::~QDeclarativeTransitionManager()
80{
81 delete d; d = 0;
82}
83
84void 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
99void 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
116void 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 }