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