1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickanimationcontroller_p.h"
43 #include <QtQml/qqmlinfo.h>
44 #include <private/qqmlengine_p.h>
49 class QQuickAnimationControllerPrivate : public QObjectPrivate
51 Q_DECLARE_PUBLIC(QQuickAnimationController)
53 QQuickAnimationControllerPrivate()
54 : progress(0.0), animation(0), animationInstance(0), finalized(false) {}
57 QQuickAbstractAnimation *animation;
58 QAbstractAnimationJob *animationInstance;
64 \qmlclass AnimationController QQuickAnimationController
65 \inqmlmodule QtQuick 2
66 \ingroup qml-animation-transition
67 \brief The AnimationController element allows you to control animations manually.
69 Normally animations are driven by an internal timer, but the AnimationController
70 allows the given \a animation to be driven by a \a progress value explicitly.
74 QQuickAnimationController::QQuickAnimationController(QObject *parent)
75 : QObject(*(new QQuickAnimationControllerPrivate), parent)
79 QQuickAnimationController::~QQuickAnimationController()
81 Q_D(QQuickAnimationController);
82 delete d->animationInstance;
86 \qmlproperty real QtQuick2::AnimationController::progress
87 This property holds the animation progress value.
89 The valid \c progress value is 0.0 to 1.0, setting values less than 0 will be converted to 0,
90 setting values great than 1 will be converted to 1.
92 qreal QQuickAnimationController::progress() const
94 Q_D(const QQuickAnimationController);
98 void QQuickAnimationController::setProgress(qreal progress)
100 Q_D(QQuickAnimationController);
101 progress = qBound(qreal(0), progress, qreal(1));
103 if (progress != d->progress) {
104 d->progress = progress;
106 emit progressChanged();
111 \qmlproperty real QtQuick2::AnimationController::animation
114 This property holds the animation to be controlled by the AnimationController.
116 Note:An animation controlled by AnimationController will always have its
117 \c running and \c paused properties set to true. It can not be manually
118 started or stopped (much like an animation in a Behavior can not be manually started or stopped).
120 QQuickAbstractAnimation *QQuickAnimationController::animation() const
122 Q_D(const QQuickAnimationController);
126 void QQuickAnimationController::classBegin()
128 QQmlEnginePrivate *engPriv = QQmlEnginePrivate::get(qmlEngine(this));
129 engPriv->registerFinalizeCallback(this, this->metaObject()->indexOfSlot("componentFinalized()"));
133 void QQuickAnimationController::setAnimation(QQuickAbstractAnimation *animation)
135 Q_D(QQuickAnimationController);
137 if (animation != d->animation) {
139 if (animation->userControlDisabled()) {
140 qmlInfo(this) << "QQuickAnimationController::setAnimation: the animation is controlled by others, can't be used in AnimationController.";
143 animation->setDisableUserControl();
147 d->animation->setEnableUserControl();
149 d->animation = animation;
151 emit animationChanged();
156 \qmlmethod QtQuick2::AnimationController::reload()
157 \brief Reloads the animation properties.
159 If the animation properties changed, calling this method to reload the animation definations.
161 void QQuickAnimationController::reload()
163 Q_D(QQuickAnimationController);
168 d->animationInstance = 0;
170 QQuickStateActions actions;
171 QQmlProperties properties;
172 QAbstractAnimationJob *oldInstance = d->animationInstance;
173 d->animationInstance = d->animation->transition(actions, properties, QQuickAbstractAnimation::Forward);
174 if (oldInstance && oldInstance != d->animationInstance)
176 d->animationInstance->setLoopCount(1);
177 d->animationInstance->start();
178 d->animationInstance->pause();
183 void QQuickAnimationController::updateProgress()
185 Q_D(QQuickAnimationController);
186 if (!d->animationInstance)
189 d->animationInstance->start();
190 QQmlAnimationTimer::unregisterAnimation(d->animationInstance);
191 d->animationInstance->setCurrentTime(d->progress * d->animationInstance->duration());
194 void QQuickAnimationController::componentFinalized()
196 Q_D(QQuickAnimationController);