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 "qquickanimationcontroller_p.h"
41#include <QtQml/qqmlinfo.h>
42#include <private/qqmlengine_p.h>
43
44QT_BEGIN_NAMESPACE
45
46
47class QQuickAnimationControllerPrivate : public QObjectPrivate, QAnimationJobChangeListener
48{
49 Q_DECLARE_PUBLIC(QQuickAnimationController)
50public:
51 QQuickAnimationControllerPrivate()
52 : progress(0.0), animation(nullptr), animationInstance(nullptr), finalized(false) {}
53 void animationFinished(QAbstractAnimationJob *job) override;
54 void animationCurrentTimeChanged(QAbstractAnimationJob *job, int currentTime) override;
55
56
57 qreal progress;
58 QQuickAbstractAnimation *animation;
59 QAbstractAnimationJob *animationInstance;
60 bool finalized:1;
61
62};
63
64void QQuickAnimationControllerPrivate::animationFinished(QAbstractAnimationJob *job)
65{
66 Q_Q(QQuickAnimationController);
67 Q_ASSERT(animationInstance && animationInstance == job);
68 Q_UNUSED(job);
69
70 animationInstance->removeAnimationChangeListener(listener: this, QAbstractAnimationJob::Completion | QAbstractAnimationJob::CurrentTime);
71
72 if (animationInstance->direction() == QAbstractAnimationJob::Forward && progress != 1) {
73 progress = 1;
74 emit q->progressChanged();
75 } else if (animationInstance->direction() == QAbstractAnimationJob::Backward && progress != 0) {
76 progress = 0;
77 emit q->progressChanged();
78 }
79
80}
81
82void QQuickAnimationControllerPrivate::animationCurrentTimeChanged(QAbstractAnimationJob *job, int currentTime)
83{
84 Q_Q(QQuickAnimationController);
85 Q_ASSERT(animationInstance && animationInstance == job);
86 Q_UNUSED(job);
87 const qreal newProgress = currentTime * 1.0 / animationInstance->duration();
88 if (progress != newProgress) {
89 progress = newProgress;
90 emit q->progressChanged();
91 }
92}
93
94/*!
95 \qmltype AnimationController
96 \instantiates QQuickAnimationController
97 \inqmlmodule QtQuick
98 \ingroup qtquick-animation-control
99 \brief Enables manual control of animations.
100
101 Normally animations are driven by an internal timer, but the AnimationController
102 allows the given \a animation to be driven by a \a progress value explicitly.
103*/
104
105
106QQuickAnimationController::QQuickAnimationController(QObject *parent)
107: QObject(*(new QQuickAnimationControllerPrivate), parent)
108{
109}
110
111QQuickAnimationController::~QQuickAnimationController()
112{
113 Q_D(QQuickAnimationController);
114 delete d->animationInstance;
115}
116
117/*!
118 \qmlproperty real QtQuick::AnimationController::progress
119 This property holds the animation progress value.
120
121 The valid \c progress value is 0.0 to 1.0, setting values less than 0 will be converted to 0,
122 setting values great than 1 will be converted to 1.
123*/
124qreal QQuickAnimationController::progress() const
125{
126 Q_D(const QQuickAnimationController);
127 return d->progress;
128}
129
130void QQuickAnimationController::setProgress(qreal progress)
131{
132 Q_D(QQuickAnimationController);
133 progress = qBound(min: qreal(0), val: progress, max: qreal(1));
134
135 if (progress != d->progress) {
136 d->progress = progress;
137 updateProgress();
138 emit progressChanged();
139 }
140}
141
142/*!
143 \qmlproperty Animation QtQuick::AnimationController::animation
144 \default
145
146 This property holds the animation to be controlled by the AnimationController.
147
148 Note:An animation controlled by AnimationController will always have its
149 \c running and \c paused properties set to true. It can not be manually
150 started or stopped (much like an animation in a Behavior can not be manually started or stopped).
151*/
152QQuickAbstractAnimation *QQuickAnimationController::animation() const
153{
154 Q_D(const QQuickAnimationController);
155 return d->animation;
156}
157
158void QQuickAnimationController::classBegin()
159{
160 QQmlEnginePrivate *engPriv = QQmlEnginePrivate::get(e: qmlEngine(this));
161 engPriv->registerFinalizeCallback(obj: this, index: this->metaObject()->indexOfSlot(slot: "componentFinalized()"));
162}
163
164
165void QQuickAnimationController::setAnimation(QQuickAbstractAnimation *animation)
166{
167 Q_D(QQuickAnimationController);
168
169 if (animation != d->animation) {
170 if (animation) {
171 if (animation->userControlDisabled()) {
172 qmlWarning(me: this) << "QQuickAnimationController::setAnimation: the animation is controlled by others, can't be used in AnimationController.";
173 return;
174 }
175 animation->setDisableUserControl();
176 }
177
178 if (d->animation)
179 d->animation->setEnableUserControl();
180
181 d->animation = animation;
182 reload();
183 emit animationChanged();
184 }
185}
186
187/*!
188 \qmlmethod QtQuick::AnimationController::reload()
189 \brief Reloads the animation properties
190
191 If the animation properties changed, calling this method to reload the animation definations.
192*/
193void QQuickAnimationController::reload()
194{
195 Q_D(QQuickAnimationController);
196 if (!d->finalized)
197 return;
198
199 if (!d->animation) {
200 d->animationInstance = nullptr;
201 } else {
202 QQuickStateActions actions;
203 QQmlProperties properties;
204 QAbstractAnimationJob *oldInstance = d->animationInstance;
205 d->animationInstance = d->animation->transition(actions, modified&: properties, direction: QQuickAbstractAnimation::Forward);
206 if (oldInstance && oldInstance != d->animationInstance)
207 delete oldInstance;
208 if (d->animationInstance) {
209 d->animationInstance->setLoopCount(1);
210 d->animationInstance->setDisableUserControl();
211 d->animationInstance->start();
212 d->animationInstance->pause();
213 updateProgress();
214 }
215 }
216}
217
218void QQuickAnimationController::updateProgress()
219{
220 Q_D(QQuickAnimationController);
221 if (!d->animationInstance)
222 return;
223
224 d->animationInstance->setDisableUserControl();
225 d->animationInstance->start();
226 QQmlAnimationTimer::instance()->unregisterAnimation(animation: d->animationInstance);
227 d->animationInstance->setCurrentTime(d->progress * d->animationInstance->duration());
228}
229
230void QQuickAnimationController::componentFinalized()
231{
232 Q_D(QQuickAnimationController);
233 d->finalized = true;
234 reload();
235}
236
237/*!
238 \qmlmethod QtQuick::AnimationController::completeToBeginning()
239 \brief Finishes running the controlled animation in a backwards direction.
240
241 After calling this method, the animation runs normally from the current progress point
242 in a backwards direction to the beginning state.
243
244 The animation controller's progress value will be automatically updated while the animation is running.
245
246 \sa completeToEnd(), progress
247*/
248void QQuickAnimationController::completeToBeginning()
249{
250 Q_D(QQuickAnimationController);
251 if (!d->animationInstance)
252 return;
253
254 if (d->progress == 0)
255 return;
256
257 d->animationInstance->addAnimationChangeListener(listener: d, QAbstractAnimationJob::Completion | QAbstractAnimationJob::CurrentTime);
258 d->animationInstance->setDirection(QAbstractAnimationJob::Backward);
259
260 //Disable and then enable user control to trigger the animation instance's state change
261 d->animationInstance->setDisableUserControl();
262 d->animationInstance->setEnableUserControl();
263 d->animationInstance->start();
264}
265
266/*!
267 \qmlmethod QtQuick::AnimationController::completeToEnd()
268 \brief Finishes running the controlled animation in a forwards direction.
269
270 After calling this method, the animation runs normally from the current progress point
271 in a forwards direction to the end state.
272
273 The animation controller's progress value will be automatically updated while the animation is running.
274
275 \sa completeToBeginning(), progress
276*/
277void QQuickAnimationController::completeToEnd()
278{
279 Q_D(QQuickAnimationController);
280 if (!d->animationInstance)
281 return;
282
283 if (d->progress == 1)
284 return;
285
286 d->animationInstance->addAnimationChangeListener(listener: d, QAbstractAnimationJob::Completion | QAbstractAnimationJob::CurrentTime);
287 d->animationInstance->setDirection(QAbstractAnimationJob::Forward);
288
289 //Disable and then enable user control to trigger the animation instance's state change
290 d->animationInstance->setDisableUserControl();
291 d->animationInstance->setEnableUserControl();
292 d->animationInstance->start();
293}
294
295
296
297QT_END_NAMESPACE
298
299
300#include "moc_qquickanimationcontroller_p.cpp"
301

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