Initial bundle support
[profile/ivi/qtdeclarative.git] / src / quick / util / qquickanimationcontroller.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickanimationcontroller_p.h"
43 #include <QtQml/qqmlinfo.h>
44 #include <private/qqmlengine_p.h>
45
46 QT_BEGIN_NAMESPACE
47
48
49 class QQuickAnimationControllerPrivate : public QObjectPrivate
50 {
51     Q_DECLARE_PUBLIC(QQuickAnimationController)
52 public:
53     QQuickAnimationControllerPrivate()
54         : progress(0.0), animation(0), animationInstance(0), finalized(false) {}
55
56     qreal progress;
57     QQuickAbstractAnimation *animation;
58     QAbstractAnimationJob *animationInstance;
59     bool finalized:1;
60
61 };
62
63 /*!
64     \qmlclass AnimationController QQuickAnimationController
65     \inqmlmodule QtQuick 2
66     \ingroup qml-animation-transition
67     \brief The AnimationController element allows you to control animations manually.
68
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.
71 */
72
73
74 QQuickAnimationController::QQuickAnimationController(QObject *parent)
75 : QObject(*(new QQuickAnimationControllerPrivate), parent)
76 {
77 }
78
79 QQuickAnimationController::~QQuickAnimationController()
80 {
81     Q_D(QQuickAnimationController);
82     delete d->animationInstance;
83 }
84
85 /*!
86     \qmlproperty real QtQuick2::AnimationController::progress
87     This property holds the animation progress value.
88
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.
91 */
92 qreal QQuickAnimationController::progress() const
93 {
94     Q_D(const QQuickAnimationController);
95     return d->progress;
96 }
97
98 void QQuickAnimationController::setProgress(qreal progress)
99 {
100     Q_D(QQuickAnimationController);
101     progress = qBound(qreal(0), progress, qreal(1));
102
103     if (progress != d->progress) {
104         d->progress = progress;
105         updateProgress();
106         emit progressChanged();
107     }
108 }
109
110 /*!
111     \qmlproperty real QtQuick2::AnimationController::animation
112     \default
113
114     This property holds the animation to be controlled by the AnimationController.
115
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).
119 */
120 QQuickAbstractAnimation *QQuickAnimationController::animation() const
121 {
122     Q_D(const QQuickAnimationController);
123     return d->animation;
124 }
125
126 void QQuickAnimationController::classBegin()
127 {
128     QQmlEnginePrivate *engPriv = QQmlEnginePrivate::get(qmlEngine(this));
129     engPriv->registerFinalizeCallback(this, this->metaObject()->indexOfSlot("componentFinalized()"));
130 }
131
132
133 void QQuickAnimationController::setAnimation(QQuickAbstractAnimation *animation)
134 {
135     Q_D(QQuickAnimationController);
136
137     if (animation != d->animation) {
138         if (animation) {
139             if (animation->userControlDisabled()) {
140                 qmlInfo(this) << "QQuickAnimationController::setAnimation: the animation is controlled by others, can't be used in AnimationController.";
141                 return;
142             }
143             animation->setDisableUserControl();
144         }
145
146         if (d->animation)
147             d->animation->setEnableUserControl();
148
149         d->animation = animation;
150         reload();
151         emit animationChanged();
152     }
153 }
154
155 /*!
156     \qmlmethod QtQuick2::AnimationController::reload()
157     \brief Reloads the animation properties.
158
159     If the animation properties changed, calling this method to reload the animation definations.
160 */
161 void QQuickAnimationController::reload()
162 {
163     Q_D(QQuickAnimationController);
164     if (!d->finalized)
165         return;
166
167     if (!d->animation) {
168         d->animationInstance = 0;
169     } else {
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)
175             delete oldInstance;
176         d->animationInstance->setLoopCount(1);
177         d->animationInstance->setDisableUserControl();
178         d->animationInstance->start();
179         d->animationInstance->pause();
180         updateProgress();
181     }
182 }
183
184 void QQuickAnimationController::updateProgress()
185 {
186     Q_D(QQuickAnimationController);
187     if (!d->animationInstance)
188         return;
189
190     d->animationInstance->setDisableUserControl();
191     d->animationInstance->start();
192     QQmlAnimationTimer::unregisterAnimation(d->animationInstance);
193     d->animationInstance->setCurrentTime(d->progress * d->animationInstance->duration());
194 }
195
196 void QQuickAnimationController::componentFinalized()
197 {
198     Q_D(QQuickAnimationController);
199     d->finalized = true;
200     reload();
201 }
202
203
204 QT_END_NAMESPACE
205
206