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