| 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 | |
| 60 | QT_REQUIRE_CONFIG(qml_animation); |
| 61 | |
| 62 | QT_BEGIN_NAMESPACE |
| 63 | |
| 64 | class QAnimationGroupJob; |
| 65 | class QAnimationJobChangeListener; |
| 66 | class QQmlAnimationTimer; |
| 67 | |
| 68 | class Q_QML_PRIVATE_EXPORT QAbstractAnimationJob |
| 69 | { |
| 70 | Q_DISABLE_COPY(QAbstractAnimationJob) |
| 71 | public: |
| 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; |
| 135 | protected: |
| 136 | virtual void updateCurrentTime(int) {} |
| 137 | virtual void updateLoopCount(int) {} |
| 138 | virtual void updateState(QAbstractAnimationJob::State newState, QAbstractAnimationJob::State oldState); |
| 139 | virtual void updateDirection(QAbstractAnimationJob::Direction direction); |
| 140 | virtual void topLevelAnimationLoopChanged() {} |
| 141 | |
| 142 | virtual void debugAnimation(QDebug d) const; |
| 143 | |
| 144 | void fireTopLevelAnimationLoopChanged(); |
| 145 | |
| 146 | void setState(QAbstractAnimationJob::State state); |
| 147 | |
| 148 | void finished(); |
| 149 | void stateChanged(QAbstractAnimationJob::State newState, QAbstractAnimationJob::State oldState); |
| 150 | void currentLoopChanged(); |
| 151 | void directionChanged(QAbstractAnimationJob::Direction); |
| 152 | void currentTimeChanged(int currentTime); |
| 153 | |
| 154 | //definition |
| 155 | int m_loopCount; |
| 156 | QAnimationGroupJob *m_group; |
| 157 | QAbstractAnimationJob::Direction m_direction; |
| 158 | |
| 159 | //state |
| 160 | QAbstractAnimationJob::State m_state; |
| 161 | int m_totalCurrentTime; |
| 162 | int m_currentTime; |
| 163 | int m_currentLoop; |
| 164 | //records the finish time for an uncontrolled animation (used by animation groups) |
| 165 | int m_uncontrolledFinishTime; |
| 166 | int m_currentLoopStartTime; // used together with m_uncontrolledFinishTime |
| 167 | |
| 168 | struct ChangeListener { |
| 169 | ChangeListener(QAnimationJobChangeListener *l, QAbstractAnimationJob::ChangeTypes t) : listener(l), types(t) {} |
| 170 | QAnimationJobChangeListener *listener; |
| 171 | QAbstractAnimationJob::ChangeTypes types; |
| 172 | bool operator==(const ChangeListener &other) const { return listener == other.listener && types == other.types; } |
| 173 | }; |
| 174 | std::vector<ChangeListener> changeListeners; |
| 175 | |
| 176 | QAbstractAnimationJob *m_nextSibling; |
| 177 | QAbstractAnimationJob *m_previousSibling; |
| 178 | QQmlAnimationTimer *m_timer = nullptr; |
| 179 | |
| 180 | bool m_hasRegisteredTimer:1; |
| 181 | bool m_isPause:1; |
| 182 | bool m_isGroup:1; |
| 183 | bool m_disableUserControl:1; |
| 184 | bool m_hasCurrentTimeChangeListeners:1; |
| 185 | bool m_isRenderThreadJob:1; |
| 186 | bool m_isRenderThreadProxy:1; |
| 187 | |
| 188 | friend class QQmlAnimationTimer; |
| 189 | friend class QAnimationGroupJob; |
| 190 | friend Q_QML_PRIVATE_EXPORT QDebug operator<<(QDebug, const QAbstractAnimationJob *job); |
| 191 | }; |
| 192 | |
| 193 | class Q_QML_PRIVATE_EXPORT QAnimationJobChangeListener |
| 194 | { |
| 195 | public: |
| 196 | virtual ~QAnimationJobChangeListener(); |
| 197 | virtual void animationFinished(QAbstractAnimationJob *) {} |
| 198 | virtual void animationStateChanged(QAbstractAnimationJob *, QAbstractAnimationJob::State, QAbstractAnimationJob::State) {} |
| 199 | virtual void animationCurrentLoopChanged(QAbstractAnimationJob *) {} |
| 200 | virtual void animationCurrentTimeChanged(QAbstractAnimationJob *, int) {} |
| 201 | }; |
| 202 | |
| 203 | class Q_QML_PRIVATE_EXPORT QQmlAnimationTimer : public QAbstractAnimationTimer |
| 204 | { |
| 205 | Q_OBJECT |
| 206 | private: |
| 207 | QQmlAnimationTimer(); |
| 208 | |
| 209 | public: |
| 210 | ~QQmlAnimationTimer(); // must be destructible by QThreadStorage |
| 211 | |
| 212 | static QQmlAnimationTimer *instance(); |
| 213 | static QQmlAnimationTimer *instance(bool create); |
| 214 | |
| 215 | void registerAnimation(QAbstractAnimationJob *animation, bool isTopLevel); |
| 216 | void unregisterAnimation(QAbstractAnimationJob *animation); |
| 217 | |
| 218 | /* |
| 219 | this is used for updating the currentTime of all animations in case the pause |
| 220 | timer is active or, otherwise, only of the animation passed as parameter. |
| 221 | */ |
| 222 | void ensureTimerUpdate(); |
| 223 | |
| 224 | /* |
| 225 | this will evaluate the need of restarting the pause timer in case there is still |
| 226 | some pause animations running. |
| 227 | */ |
| 228 | void updateAnimationTimer(); |
| 229 | |
| 230 | void restartAnimationTimer() override; |
| 231 | void updateAnimationsTime(qint64 timeStep) override; |
| 232 | |
| 233 | //useful for profiling/debugging |
| 234 | int runningAnimationCount() override { return animations.count(); } |
| 235 | |
| 236 | bool hasStartAnimationPending() const { return startAnimationPending; } |
| 237 | |
| 238 | public Q_SLOTS: |
| 239 | void startAnimations(); |
| 240 | void stopTimer(); |
| 241 | |
| 242 | private: |
| 243 | qint64 lastTick; |
| 244 | int currentAnimationIdx; |
| 245 | bool insideTick; |
| 246 | bool startAnimationPending; |
| 247 | bool stopTimerPending; |
| 248 | |
| 249 | QList<QAbstractAnimationJob*> animations, animationsToStart; |
| 250 | |
| 251 | // this is the count of running animations that are not a group neither a pause animation |
| 252 | int runningLeafAnimations; |
| 253 | QList<QAbstractAnimationJob*> runningPauseAnimations; |
| 254 | |
| 255 | void registerRunningAnimation(QAbstractAnimationJob *animation); |
| 256 | void unregisterRunningAnimation(QAbstractAnimationJob *animation); |
| 257 | void unsetJobTimer(QAbstractAnimationJob *animation); |
| 258 | |
| 259 | int closestPauseAnimationTimeToFinish(); |
| 260 | }; |
| 261 | |
| 262 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractAnimationJob::ChangeTypes) |
| 263 | |
| 264 | Q_QML_PRIVATE_EXPORT QDebug operator<<(QDebug, const QAbstractAnimationJob *job); |
| 265 | |
| 266 | QT_END_NAMESPACE |
| 267 | |
| 268 | #endif // QABSTRACTANIMATIONJOB_P_H |
| 269 | |