Update to 5.0.0-beta1
[profile/ivi/qtdeclarative.git] / src / qml / animations / qabstractanimationjob_p.h
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 #ifndef QABSTRACTANIMATIONJOB_P_H
43 #define QABSTRACTANIMATIONJOB_P_H
44
45 #include <private/qtqmlglobal_p.h>
46 #include <QtCore/QObject>
47 #include <QtCore/private/qabstractanimation_p.h>
48 #include "private/qpodvector_p.h"
49
50 QT_BEGIN_HEADER
51
52 QT_BEGIN_NAMESPACE
53
54 QT_MODULE(Qml)
55
56 class QAnimationGroupJob;
57 class QAnimationJobChangeListener;
58 class Q_QML_PRIVATE_EXPORT QAbstractAnimationJob
59 {
60     Q_DISABLE_COPY(QAbstractAnimationJob)
61 public:
62     enum Direction {
63         Forward,
64         Backward
65     };
66
67     enum State {
68         Stopped,
69         Paused,
70         Running
71     };
72
73     QAbstractAnimationJob();
74     virtual ~QAbstractAnimationJob();
75
76     //definition
77     inline QAnimationGroupJob *group() const {return m_group;}
78
79     inline int loopCount() const {return m_loopCount;}
80     void setLoopCount(int loopCount);
81
82     int totalDuration() const;
83     virtual int duration() const {return 0;}
84
85     inline QAbstractAnimationJob::Direction direction() const {return m_direction;}
86     void setDirection(QAbstractAnimationJob::Direction direction);
87
88     //state
89     inline int currentTime() const {return m_totalCurrentTime;}
90     inline int currentLoopTime() const {return m_currentTime;}
91     inline int currentLoop() const {return m_currentLoop;}
92     inline QAbstractAnimationJob::State state() const {return m_state;}
93     inline bool isRunning() { return m_state == Running; }
94     inline bool isStopped() { return m_state == Stopped; }
95     inline bool isPaused() { return m_state == Paused; }
96     void setDisableUserControl();
97     void setEnableUserControl();
98     bool userControlDisabled() const;
99
100     void setCurrentTime(int msecs);
101
102     void start();
103     void pause();
104     void resume();
105     void stop();
106
107     enum ChangeType {
108         Completion = 0x01,
109         StateChange = 0x02,
110         CurrentLoop = 0x04,
111         CurrentTime = 0x08
112     };
113     Q_DECLARE_FLAGS(ChangeTypes, ChangeType)
114
115     void addAnimationChangeListener(QAnimationJobChangeListener *listener, QAbstractAnimationJob::ChangeTypes);
116     void removeAnimationChangeListener(QAnimationJobChangeListener *listener, QAbstractAnimationJob::ChangeTypes);
117     QAbstractAnimationJob *nextSibling() const { return m_nextSibling; }
118     QAbstractAnimationJob *previousSibling() const { return m_previousSibling; }
119
120 protected:
121     virtual void updateCurrentTime(int) {}
122     virtual void updateState(QAbstractAnimationJob::State newState, QAbstractAnimationJob::State oldState);
123     virtual void updateDirection(QAbstractAnimationJob::Direction direction);
124     virtual void topLevelAnimationLoopChanged() {}
125
126     void setState(QAbstractAnimationJob::State state);
127
128     void finished();
129     void stateChanged(QAbstractAnimationJob::State newState, QAbstractAnimationJob::State oldState);
130     void currentLoopChanged();
131     void directionChanged(QAbstractAnimationJob::Direction);
132     void currentTimeChanged(int currentTime);
133
134     //definition
135     int m_loopCount;
136     QAnimationGroupJob *m_group;
137     QAbstractAnimationJob::Direction m_direction;
138
139     //state
140     QAbstractAnimationJob::State m_state;
141     int m_totalCurrentTime;
142     int m_currentTime;
143     int m_currentLoop;
144     //records the finish time for an uncontrolled animation (used by animation groups)
145     int m_uncontrolledFinishTime;
146
147     struct ChangeListener {
148         ChangeListener(QAnimationJobChangeListener *l, QAbstractAnimationJob::ChangeTypes t) : listener(l), types(t) {}
149         QAnimationJobChangeListener *listener;
150         QAbstractAnimationJob::ChangeTypes types;
151         bool operator==(const ChangeListener &other) const { return listener == other.listener && types == other.types; }
152     };
153     QPODVector<ChangeListener,1> changeListeners;
154
155     QAbstractAnimationJob *m_nextSibling;
156     QAbstractAnimationJob *m_previousSibling;
157
158     bool *m_wasDeleted;
159     bool m_hasRegisteredTimer:1;
160     bool m_isPause:1;
161     bool m_isGroup:1;
162     bool m_disableUserControl:1;
163     bool m_hasCurrentTimeChangeListeners:1;
164
165     friend class QQmlAnimationTimer;
166     friend class QAnimationGroupJob;
167 };
168
169 class Q_AUTOTEST_EXPORT QAnimationJobChangeListener
170 {
171 public:
172     virtual void animationFinished(QAbstractAnimationJob *) {}
173     virtual void animationStateChanged(QAbstractAnimationJob *, QAbstractAnimationJob::State, QAbstractAnimationJob::State) {}
174     virtual void animationCurrentLoopChanged(QAbstractAnimationJob *) {}
175     virtual void animationCurrentTimeChanged(QAbstractAnimationJob *, int) {}
176 };
177
178 class Q_QML_PRIVATE_EXPORT QQmlAnimationTimer : public QAbstractAnimationTimer
179 {
180     Q_OBJECT
181 private:
182     QQmlAnimationTimer();
183
184 public:
185     static QQmlAnimationTimer *instance();
186     static QQmlAnimationTimer *instance(bool create);
187
188     static void registerAnimation(QAbstractAnimationJob *animation, bool isTopLevel);
189     static void unregisterAnimation(QAbstractAnimationJob *animation);
190
191     /*
192         this is used for updating the currentTime of all animations in case the pause
193         timer is active or, otherwise, only of the animation passed as parameter.
194     */
195     static void ensureTimerUpdate();
196
197     /*
198         this will evaluate the need of restarting the pause timer in case there is still
199         some pause animations running.
200     */
201     static void updateAnimationTimer();
202
203     void restartAnimationTimer();
204     void updateAnimationsTime(qint64 timeStep);
205
206     int currentDelta() { return lastDelta; }
207
208     //useful for profiling/debugging
209     int runningAnimationCount() { return animations.count(); }
210
211 private Q_SLOTS:
212     void startAnimations();
213     void stopTimer();
214
215 private:
216     qint64 lastTick;
217     int lastDelta;
218     int currentAnimationIdx;
219     bool insideTick;
220     bool startAnimationPending;
221     bool stopTimerPending;
222
223     QList<QAbstractAnimationJob*> animations, animationsToStart;
224
225     // this is the count of running animations that are not a group neither a pause animation
226     int runningLeafAnimations;
227     QList<QAbstractAnimationJob*> runningPauseAnimations;
228
229     void registerRunningAnimation(QAbstractAnimationJob *animation);
230     void unregisterRunningAnimation(QAbstractAnimationJob *animation);
231
232     int closestPauseAnimationTimeToFinish();
233 };
234
235 Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractAnimationJob::ChangeTypes)
236
237 QT_END_NAMESPACE
238
239 QT_END_HEADER
240
241 #endif // QABSTRACTANIMATIONJOB_P_H