1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquicktransitionmanager_p_p.h"
5
6#include "qquicktransition_p.h"
7#include "qquickstate_p_p.h"
8
9#include <private/qqmlbinding_p.h>
10#include <private/qqmlglobal_p.h>
11#include <private/qqmlproperty_p.h>
12
13#include <QtCore/qdebug.h>
14#include <private/qanimationjobutil_p.h>
15
16QT_BEGIN_NAMESPACE
17
18class QQuickTransitionManagerPrivate
19{
20public:
21 QQuickTransitionManagerPrivate()
22 : state(nullptr), transitionInstance(nullptr) {}
23
24 void applyBindings();
25 typedef QList<QQuickSimpleAction> SimpleActionList;
26 QQuickState *state;
27 QQuickTransitionInstance *transitionInstance;
28 QQuickStateOperation::ActionList bindingsList;
29 SimpleActionList completeList;
30};
31
32QQuickTransitionManager::QQuickTransitionManager()
33: d(new QQuickTransitionManagerPrivate)
34{
35}
36
37void QQuickTransitionManager::setState(QQuickState *s)
38{
39 d->state = s;
40}
41
42QQuickTransitionManager::~QQuickTransitionManager()
43{
44 delete d->transitionInstance;
45 d->transitionInstance = nullptr;
46 delete d; d = nullptr;
47}
48
49bool QQuickTransitionManager::isRunning() const
50{
51 return d->transitionInstance && d->transitionInstance->isRunning();
52}
53
54void QQuickTransitionManager::complete()
55{
56 d->applyBindings();
57
58 // Explicitly take a copy in case the write action triggers a script that modifies the list.
59 QQuickTransitionManagerPrivate::SimpleActionList completeListCopy = d->completeList;
60 for (const QQuickSimpleAction &action : std::as_const(t&: completeListCopy))
61 action.property().write(action.value());
62
63 d->completeList.clear();
64
65 if (d->state)
66 static_cast<QQuickStatePrivate*>(QObjectPrivate::get(o: d->state))->complete();
67
68 finished();
69}
70
71void QQuickTransitionManagerPrivate::applyBindings()
72{
73 for (const QQuickStateAction &action : std::as_const(t&: bindingsList)) {
74 if (auto binding = action.toBinding; binding) {
75 binding.installOn(target: action.property, mode: QQmlAnyBinding::RespectInterceptors);
76 } else if (action.event) {
77 if (action.reverseEvent)
78 action.event->reverse();
79 else
80 action.event->execute();
81 }
82
83 }
84
85 bindingsList.clear();
86}
87
88void QQuickTransitionManager::finished()
89{
90}
91
92void QQuickTransitionManager::transition(const QList<QQuickStateAction> &list,
93 QQuickTransition *transition,
94 QObject *defaultTarget)
95{
96 RETURN_IF_DELETED(cancel());
97
98 // The copy below is ON PURPOSE, because firing actions might involve scripts that modify the list.
99 QQuickStateOperation::ActionList applyList = list;
100
101 // Determine which actions are binding changes and disable any current bindings
102 for (const QQuickStateAction &action : std::as_const(t&: applyList)) {
103 if (action.toBinding)
104 d->bindingsList << action;
105 if (action.fromBinding) {
106 auto property = action.property;
107 QQmlAnyBinding::removeBindingFrom(prop&: property); // Disable current binding
108 }
109 if (action.event && action.event->changesBindings()) { //### assume isReversable()?
110 d->bindingsList << action;
111 action.event->clearBindings();
112 }
113 }
114
115 // Animated transitions need both the start and the end value for
116 // each property change. In the presence of bindings, the end values
117 // are non-trivial to calculate. As a "best effort" attempt, we first
118 // apply all the property and binding changes, then read all the actual
119 // final values, then roll back the changes and proceed as normal.
120 //
121 // This doesn't catch everything, and it might be a little fragile in
122 // some cases - but whatcha going to do?
123 if (transition && !d->bindingsList.isEmpty()) {
124
125 // Apply all the property and binding changes
126 for (const QQuickStateAction &action : std::as_const(t&: applyList)) {
127 if (auto binding = action.toBinding; binding) {
128 binding.installOn(target: action.property);
129 } else if (!action.event) {
130 QQmlPropertyPrivate::write(that: action.property, action.toValue, QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding);
131 } else if (action.event->isReversable()) {
132 if (action.reverseEvent)
133 action.event->reverse();
134 else
135 action.event->execute();
136 }
137 }
138
139 // Read all the end values for binding changes.
140 for (auto it = applyList.begin(), eit = applyList.end(); it != eit; ++it) {
141 if (it->event) {
142 it->event->saveTargetValues();
143 continue;
144 }
145 const QQmlProperty &prop = it->property;
146 if (it->toBinding || !it->toValue.isValid())
147 it->toValue = prop.read();
148 }
149
150 // Revert back to the original values
151 for (const QQuickStateAction &action : std::as_const(t&: applyList)) {
152 if (action.event) {
153 if (action.event->isReversable()) {
154 action.event->clearBindings();
155 action.event->rewind();
156 action.event->clearBindings(); //### shouldn't be needed
157 }
158 continue;
159 }
160
161 if (action.toBinding) {
162 auto property = action.property;
163 QQmlAnyBinding::removeBindingFrom(prop&: property); // Make sure this is disabled during the transition
164 }
165
166 QQmlPropertyPrivate::write(that: action.property, action.fromValue, QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding);
167 }
168 }
169
170 if (transition) {
171 QList<QQmlProperty> touched;
172 QQuickTransitionInstance *oldInstance = d->transitionInstance;
173 d->transitionInstance = transition->prepare(actions&: applyList, after&: touched, end: this, defaultTarget);
174 d->transitionInstance->start();
175 if (oldInstance && oldInstance != d->transitionInstance)
176 delete oldInstance;
177
178 // Modify the action list to remove actions handled in the transition
179 auto isHandledInTransition = [this, touched](const QQuickStateAction &action) {
180 if (action.event) {
181 return action.actionDone;
182 } else {
183 if (touched.contains(t: action.property)) {
184 if (action.toValue != action.fromValue)
185 d->completeList << QQuickSimpleAction(action, QQuickSimpleAction::EndState);
186 return true;
187 }
188 }
189 return false;
190 };
191
192 applyList.removeIf(pred: isHandledInTransition);
193 }
194
195 // Any actions remaining have not been handled by the transition and should
196 // be applied immediately. We skip applying bindings, as they are all
197 // applied at the end in applyBindings() to avoid any nastiness mid
198 // transition
199 for (const QQuickStateAction &action : std::as_const(t&: applyList)) {
200 if (action.event && !action.event->changesBindings()) {
201 if (action.event->isReversable() && action.reverseEvent)
202 action.event->reverse();
203 else
204 action.event->execute();
205 } else if (!action.event && !action.toBinding) {
206 action.property.write(action.toValue);
207 }
208 }
209 if (lcStates().isDebugEnabled()) {
210 for (const QQuickStateAction &action : std::as_const(t&: applyList)) {
211 if (action.event)
212 qCDebug(lcStates) << "no transition for event:" << action.event->type();
213 else
214 qCDebug(lcStates) << "no transition for:" << action.property.object()
215 << action.property.name() << "from:" << action.fromValue
216 << "to:" << action.toValue;
217 }
218 }
219
220 if (!transition)
221 complete();
222}
223
224void QQuickTransitionManager::cancel()
225{
226 if (d->transitionInstance && d->transitionInstance->isRunning())
227 RETURN_IF_DELETED(d->transitionInstance->stop());
228
229 for (const QQuickStateAction &action : std::as_const(t&: d->bindingsList)) {
230 if (action.toBinding && action.deletableToBinding) {
231 auto property = action.property;
232 QQmlAnyBinding::removeBindingFrom(prop&: property);
233 } else if (action.event) {
234 //### what do we do here?
235 }
236
237 }
238 d->bindingsList.clear();
239 d->completeList.clear();
240}
241
242QT_END_NAMESPACE
243

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