1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquicktransitionmanager_p_p.h"
41
42#include "qquicktransition_p.h"
43#include "qquickstate_p_p.h"
44
45#include <private/qqmlbinding_p.h>
46#include <private/qqmlglobal_p.h>
47#include <private/qqmlproperty_p.h>
48
49#include <QtCore/qdebug.h>
50#include <private/qanimationjobutil_p.h>
51
52QT_BEGIN_NAMESPACE
53
54DEFINE_BOOL_CONFIG_OPTION(stateChangeDebug, STATECHANGE_DEBUG);
55
56class QQuickTransitionManagerPrivate
57{
58public:
59 QQuickTransitionManagerPrivate()
60 : state(nullptr), transitionInstance(nullptr) {}
61
62 void applyBindings();
63 typedef QList<QQuickSimpleAction> SimpleActionList;
64 QQuickState *state;
65 QQuickTransitionInstance *transitionInstance;
66 QQuickStateOperation::ActionList bindingsList;
67 SimpleActionList completeList;
68};
69
70QQuickTransitionManager::QQuickTransitionManager()
71: d(new QQuickTransitionManagerPrivate)
72{
73}
74
75void QQuickTransitionManager::setState(QQuickState *s)
76{
77 d->state = s;
78}
79
80QQuickTransitionManager::~QQuickTransitionManager()
81{
82 delete d->transitionInstance;
83 d->transitionInstance = nullptr;
84 delete d; d = nullptr;
85}
86
87bool QQuickTransitionManager::isRunning() const
88{
89 return d->transitionInstance && d->transitionInstance->isRunning();
90}
91
92void QQuickTransitionManager::complete()
93{
94 d->applyBindings();
95
96 // Explicitly take a copy in case the write action triggers a script that modifies the list.
97 QQuickTransitionManagerPrivate::SimpleActionList completeListCopy = d->completeList;
98 for (const QQuickSimpleAction &action : qAsConst(t&: completeListCopy))
99 action.property().write(action.value());
100
101 d->completeList.clear();
102
103 if (d->state)
104 static_cast<QQuickStatePrivate*>(QObjectPrivate::get(o: d->state))->complete();
105
106 finished();
107}
108
109void QQuickTransitionManagerPrivate::applyBindings()
110{
111 for (const QQuickStateAction &action : qAsConst(t&: bindingsList)) {
112 if (action.toBinding) {
113 QQmlPropertyPrivate::setBinding(binding: action.toBinding.data());
114 } else if (action.event) {
115 if (action.reverseEvent)
116 action.event->reverse();
117 else
118 action.event->execute();
119 }
120
121 }
122
123 bindingsList.clear();
124}
125
126void QQuickTransitionManager::finished()
127{
128}
129
130void QQuickTransitionManager::transition(const QList<QQuickStateAction> &list,
131 QQuickTransition *transition,
132 QObject *defaultTarget)
133{
134 RETURN_IF_DELETED(cancel());
135
136 // The copy below is ON PURPOSE, because firing actions might involve scripts that modify the list.
137 QQuickStateOperation::ActionList applyList = list;
138
139 // Determine which actions are binding changes and disable any current bindings
140 for (const QQuickStateAction &action : qAsConst(t&: applyList)) {
141 if (action.toBinding)
142 d->bindingsList << action;
143 if (action.fromBinding)
144 QQmlPropertyPrivate::removeBinding(that: action.property); // Disable current binding
145 if (action.event && action.event->changesBindings()) { //### assume isReversable()?
146 d->bindingsList << action;
147 action.event->clearBindings();
148 }
149 }
150
151 // Animated transitions need both the start and the end value for
152 // each property change. In the presence of bindings, the end values
153 // are non-trivial to calculate. As a "best effort" attempt, we first
154 // apply all the property and binding changes, then read all the actual
155 // final values, then roll back the changes and proceed as normal.
156 //
157 // This doesn't catch everything, and it might be a little fragile in
158 // some cases - but whatcha going to do?
159 if (transition && !d->bindingsList.isEmpty()) {
160
161 // Apply all the property and binding changes
162 for (const QQuickStateAction &action : qAsConst(t&: applyList)) {
163 if (action.toBinding) {
164 QQmlPropertyPrivate::setBinding(binding: action.toBinding.data(), flags: QQmlPropertyPrivate::None, writeFlags: QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding);
165 } else if (!action.event) {
166 QQmlPropertyPrivate::write(that: action.property, action.toValue, QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding);
167 } else if (action.event->isReversable()) {
168 if (action.reverseEvent)
169 action.event->reverse();
170 else
171 action.event->execute();
172 }
173 }
174
175 // Read all the end values for binding changes.
176 for (auto it = applyList.begin(), eit = applyList.end(); it != eit; ++it) {
177 if (it->event) {
178 it->event->saveTargetValues();
179 continue;
180 }
181 const QQmlProperty &prop = it->property;
182 if (it->toBinding || !it->toValue.isValid())
183 it->toValue = prop.read();
184 }
185
186 // Revert back to the original values
187 for (const QQuickStateAction &action : qAsConst(t&: applyList)) {
188 if (action.event) {
189 if (action.event->isReversable()) {
190 action.event->clearBindings();
191 action.event->rewind();
192 action.event->clearBindings(); //### shouldn't be needed
193 }
194 continue;
195 }
196
197 if (action.toBinding)
198 QQmlPropertyPrivate::removeBinding(that: action.property); // Make sure this is disabled during the transition
199
200 QQmlPropertyPrivate::write(that: action.property, action.fromValue, QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding);
201 }
202 }
203
204 if (transition) {
205 QList<QQmlProperty> touched;
206 QQuickTransitionInstance *oldInstance = d->transitionInstance;
207 d->transitionInstance = transition->prepare(actions&: applyList, after&: touched, end: this, defaultTarget);
208 d->transitionInstance->start();
209 if (oldInstance && oldInstance != d->transitionInstance)
210 delete oldInstance;
211
212 // Modify the action list to remove actions handled in the transition
213 auto isHandledInTransition = [this, touched](const QQuickStateAction &action) {
214 if (action.event) {
215 return action.actionDone;
216 } else {
217 if (touched.contains(t: action.property)) {
218 if (action.toValue != action.fromValue)
219 d->completeList << QQuickSimpleAction(action, QQuickSimpleAction::EndState);
220 return true;
221 }
222 }
223 return false;
224 };
225 auto newEnd = std::remove_if(first: applyList.begin(), last: applyList.end(), pred: isHandledInTransition);
226 applyList.erase(afirst: newEnd, alast: applyList.end());
227 }
228
229 // Any actions remaining have not been handled by the transition and should
230 // be applied immediately. We skip applying bindings, as they are all
231 // applied at the end in applyBindings() to avoid any nastiness mid
232 // transition
233 for (const QQuickStateAction &action : qAsConst(t&: applyList)) {
234 if (action.event && !action.event->changesBindings()) {
235 if (action.event->isReversable() && action.reverseEvent)
236 action.event->reverse();
237 else
238 action.event->execute();
239 } else if (!action.event && !action.toBinding) {
240 action.property.write(action.toValue);
241 }
242 }
243#ifndef QT_NO_DEBUG_STREAM
244 if (stateChangeDebug()) {
245 for (const QQuickStateAction &action : qAsConst(t&: applyList)) {
246 if (action.event)
247 qWarning() << " No transition for event:" << action.event->type();
248 else
249 qWarning() << " No transition for:" << action.property.object()
250 << action.property.name() << "From:" << action.fromValue
251 << "To:" << action.toValue;
252 }
253 }
254#endif
255 if (!transition)
256 complete();
257}
258
259void QQuickTransitionManager::cancel()
260{
261 if (d->transitionInstance && d->transitionInstance->isRunning())
262 RETURN_IF_DELETED(d->transitionInstance->stop());
263
264 for (const QQuickStateAction &action : qAsConst(t&: d->bindingsList)) {
265 if (action.toBinding && action.deletableToBinding) {
266 QQmlPropertyPrivate::removeBinding(that: action.property);
267 } else if (action.event) {
268 //### what do we do here?
269 }
270
271 }
272 d->bindingsList.clear();
273 d->completeList.clear();
274}
275
276QT_END_NAMESPACE
277

source code of qtdeclarative/src/quick/util/qquicktransitionmanager.cpp