| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QABSTRACTANIMATION_P_H |
| 6 | #define QABSTRACTANIMATION_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. |
| 13 | // This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <QtCore/qbasictimer.h> |
| 20 | #include <QtCore/qdatetime.h> |
| 21 | #include <QtCore/qelapsedtimer.h> |
| 22 | #include <private/qobject_p.h> |
| 23 | #include <private/qproperty_p.h> |
| 24 | #include <qabstractanimation.h> |
| 25 | |
| 26 | QT_REQUIRE_CONFIG(animation); |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | class QAnimationGroup; |
| 31 | class QAbstractAnimation; |
| 32 | class Q_CORE_EXPORT QAbstractAnimationPrivate : public QObjectPrivate |
| 33 | { |
| 34 | public: |
| 35 | QAbstractAnimationPrivate(); |
| 36 | virtual ~QAbstractAnimationPrivate(); |
| 37 | |
| 38 | static QAbstractAnimationPrivate *get(QAbstractAnimation *q) |
| 39 | { |
| 40 | return q->d_func(); |
| 41 | } |
| 42 | |
| 43 | Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QAbstractAnimationPrivate, QAbstractAnimation::State, |
| 44 | state, QAbstractAnimation::Stopped) |
| 45 | void setState(QAbstractAnimation::State state); |
| 46 | |
| 47 | void setDirection(QAbstractAnimation::Direction direction) |
| 48 | { |
| 49 | q_func()->setDirection(direction); |
| 50 | } |
| 51 | void emitDirectionChanged() { Q_EMIT q_func()->directionChanged(direction); } |
| 52 | Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QAbstractAnimationPrivate, QAbstractAnimation::Direction, |
| 53 | direction, &QAbstractAnimationPrivate::setDirection, |
| 54 | &QAbstractAnimationPrivate::emitDirectionChanged, |
| 55 | QAbstractAnimation::Forward) |
| 56 | |
| 57 | void setCurrentTime(int msecs) { q_func()->setCurrentTime(msecs); } |
| 58 | Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QAbstractAnimationPrivate, int, totalCurrentTime, |
| 59 | &QAbstractAnimationPrivate::setCurrentTime, 0) |
| 60 | int currentTime = 0; |
| 61 | |
| 62 | Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QAbstractAnimationPrivate, int, loopCount, 1) |
| 63 | |
| 64 | void emitCurrentLoopChanged() { Q_EMIT q_func()->currentLoopChanged(currentLoop); } |
| 65 | Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QAbstractAnimationPrivate, int, currentLoop, nullptr, |
| 66 | &QAbstractAnimationPrivate::emitCurrentLoopChanged, 0) |
| 67 | |
| 68 | bool deleteWhenStopped = false; |
| 69 | bool hasRegisteredTimer = false; |
| 70 | bool isPause = false; |
| 71 | bool isGroup = false; |
| 72 | |
| 73 | QAnimationGroup *group = nullptr; |
| 74 | |
| 75 | private: |
| 76 | Q_DECLARE_PUBLIC(QAbstractAnimation) |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | class QUnifiedTimer; |
| 81 | class QDefaultAnimationDriver : public QAnimationDriver |
| 82 | { |
| 83 | Q_OBJECT |
| 84 | public: |
| 85 | explicit QDefaultAnimationDriver(QUnifiedTimer *timer); |
| 86 | ~QDefaultAnimationDriver() override; |
| 87 | |
| 88 | protected: |
| 89 | void timerEvent(QTimerEvent *e) override; |
| 90 | |
| 91 | private Q_SLOTS: |
| 92 | void startTimer(); |
| 93 | void stopTimer(); |
| 94 | |
| 95 | private: |
| 96 | QBasicTimer m_timer; |
| 97 | QUnifiedTimer *m_unified_timer; |
| 98 | }; |
| 99 | |
| 100 | class Q_CORE_EXPORT QAnimationDriverPrivate : public QObjectPrivate |
| 101 | { |
| 102 | public: |
| 103 | QAnimationDriverPrivate(); |
| 104 | ~QAnimationDriverPrivate() override; |
| 105 | |
| 106 | QElapsedTimer timer; |
| 107 | bool running = false; |
| 108 | }; |
| 109 | |
| 110 | class Q_CORE_EXPORT QAbstractAnimationTimer : public QObject |
| 111 | { |
| 112 | Q_OBJECT |
| 113 | public: |
| 114 | QAbstractAnimationTimer(); |
| 115 | ~QAbstractAnimationTimer() override; |
| 116 | |
| 117 | virtual void updateAnimationsTime(qint64 delta) = 0; |
| 118 | virtual void restartAnimationTimer() = 0; |
| 119 | #define QT_QAbstractAnimationTimer_runningAnimationCount_IS_CONST |
| 120 | virtual qsizetype runningAnimationCount() const = 0; |
| 121 | |
| 122 | bool isRegistered = false; |
| 123 | bool isPaused = false; |
| 124 | int pauseDuration = 0; |
| 125 | }; |
| 126 | |
| 127 | class Q_CORE_EXPORT QUnifiedTimer : public QObject |
| 128 | { |
| 129 | Q_OBJECT |
| 130 | private: |
| 131 | QUnifiedTimer(); |
| 132 | |
| 133 | public: |
| 134 | ~QUnifiedTimer() override; |
| 135 | |
| 136 | static QUnifiedTimer *instance(); |
| 137 | static QUnifiedTimer *instance(bool create); |
| 138 | |
| 139 | static void startAnimationTimer(QAbstractAnimationTimer *timer); |
| 140 | static void stopAnimationTimer(QAbstractAnimationTimer *timer); |
| 141 | |
| 142 | static void pauseAnimationTimer(QAbstractAnimationTimer *timer, int duration); |
| 143 | static void resumeAnimationTimer(QAbstractAnimationTimer *timer); |
| 144 | |
| 145 | //defines the timing interval. Default is DEFAULT_TIMER_INTERVAL |
| 146 | void setTimingInterval(int interval); |
| 147 | |
| 148 | /* |
| 149 | this allows to have a consistent timer interval at each tick from the timer |
| 150 | not taking the real time that passed into account. |
| 151 | */ |
| 152 | void setConsistentTiming(bool consistent) { consistentTiming = consistent; } |
| 153 | |
| 154 | //these facilitate fine-tuning of complex animations |
| 155 | void setSlowModeEnabled(bool enabled) { slowMode = enabled; } |
| 156 | void setSlowdownFactor(qreal factor) { slowdownFactor = factor; } |
| 157 | |
| 158 | void installAnimationDriver(QAnimationDriver *driver); |
| 159 | void uninstallAnimationDriver(QAnimationDriver *driver); |
| 160 | bool canUninstallAnimationDriver(QAnimationDriver *driver); |
| 161 | |
| 162 | void restart(); |
| 163 | void maybeUpdateAnimationsToCurrentTime(); |
| 164 | void updateAnimationTimers(); |
| 165 | |
| 166 | //useful for profiling/debugging |
| 167 | qsizetype runningAnimationCount() const; |
| 168 | void registerProfilerCallback(void (*cb)(qint64)); |
| 169 | |
| 170 | void startAnimationDriver(); |
| 171 | void stopAnimationDriver(); |
| 172 | qint64 elapsed() const; |
| 173 | |
| 174 | protected: |
| 175 | void timerEvent(QTimerEvent *) override; |
| 176 | |
| 177 | private Q_SLOTS: |
| 178 | void startTimers(); |
| 179 | void stopTimer(); |
| 180 | |
| 181 | private: |
| 182 | friend class QDefaultAnimationDriver; |
| 183 | friend class QAnimationDriver; |
| 184 | |
| 185 | QAnimationDriver *driver; |
| 186 | QDefaultAnimationDriver defaultDriver; |
| 187 | |
| 188 | QBasicTimer pauseTimer; |
| 189 | |
| 190 | QElapsedTimer time; |
| 191 | |
| 192 | qint64 lastTick; |
| 193 | int timingInterval; |
| 194 | int currentAnimationIdx; |
| 195 | bool insideTick; |
| 196 | bool insideRestart; |
| 197 | bool consistentTiming; |
| 198 | bool slowMode; |
| 199 | bool startTimersPending; |
| 200 | bool stopTimerPending; |
| 201 | bool allowNegativeDelta; |
| 202 | |
| 203 | // This factor will be used to divide the DEFAULT_TIMER_INTERVAL at each tick |
| 204 | // when slowMode is enabled. Setting it to 0 or higher than DEFAULT_TIMER_INTERVAL (16) |
| 205 | // stops all animations. |
| 206 | qreal slowdownFactor; |
| 207 | |
| 208 | QList<QAbstractAnimationTimer*> animationTimers, animationTimersToStart; |
| 209 | QList<QAbstractAnimationTimer*> pausedAnimationTimers; |
| 210 | |
| 211 | void localRestart(); |
| 212 | int closestPausedAnimationTimerTimeToFinish(); |
| 213 | |
| 214 | void (*profilerCallback)(qint64); |
| 215 | |
| 216 | qint64 driverStartTime; // The time the animation driver was started |
| 217 | qint64 temporalDrift; // The delta between animation driver time and wall time. |
| 218 | }; |
| 219 | |
| 220 | class QAnimationTimer : public QAbstractAnimationTimer |
| 221 | { |
| 222 | Q_OBJECT |
| 223 | private: |
| 224 | QAnimationTimer(); |
| 225 | |
| 226 | public: |
| 227 | ~QAnimationTimer() override; |
| 228 | |
| 229 | static QAnimationTimer *instance(); |
| 230 | static QAnimationTimer *instance(bool create); |
| 231 | |
| 232 | static void registerAnimation(QAbstractAnimation *animation, bool isTopLevel); |
| 233 | static void unregisterAnimation(QAbstractAnimation *animation); |
| 234 | |
| 235 | /* |
| 236 | this is used for updating the currentTime of all animations in case the pause |
| 237 | timer is active or, otherwise, only of the animation passed as parameter. |
| 238 | */ |
| 239 | static void ensureTimerUpdate(); |
| 240 | |
| 241 | /* |
| 242 | this will evaluate the need of restarting the pause timer in case there is still |
| 243 | some pause animations running. |
| 244 | */ |
| 245 | static void updateAnimationTimer(); |
| 246 | |
| 247 | void restartAnimationTimer() override; |
| 248 | void updateAnimationsTime(qint64 delta) override; |
| 249 | |
| 250 | //useful for profiling/debugging |
| 251 | qsizetype runningAnimationCount() const override { return animations.size(); } |
| 252 | |
| 253 | private Q_SLOTS: |
| 254 | void startAnimations(); |
| 255 | void stopTimer(); |
| 256 | |
| 257 | private: |
| 258 | qint64 lastTick; |
| 259 | int currentAnimationIdx; |
| 260 | bool insideTick; |
| 261 | bool startAnimationPending; |
| 262 | bool stopTimerPending; |
| 263 | |
| 264 | QList<QAbstractAnimation*> animations, animationsToStart; |
| 265 | |
| 266 | // this is the count of running animations that are not a group neither a pause animation |
| 267 | int runningLeafAnimations; |
| 268 | QList<QAbstractAnimation*> runningPauseAnimations; |
| 269 | |
| 270 | void registerRunningAnimation(QAbstractAnimation *animation); |
| 271 | void unregisterRunningAnimation(QAbstractAnimation *animation); |
| 272 | |
| 273 | int closestPauseAnimationTimeToFinish(); |
| 274 | }; |
| 275 | |
| 276 | QT_END_NAMESPACE |
| 277 | |
| 278 | #endif //QABSTRACTANIMATION_P_H |
| 279 | |