Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickframeanimation.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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
5
6#include <QtCore/qcoreapplication.h>
7#include <QtCore/qelapsedtimer.h>
8#include "private/qabstractanimationjob_p.h"
9#include <private/qobject_p.h>
10#include <qdebug.h>
11
13
15{
16 int duration() const override {
17 return 1;
18 }
19};
20
22{
23 Q_DECLARE_PUBLIC(QQuickFrameAnimation)
24public:
26
30
31 void maybeTick()
32 {
34 if (!running || paused)
35 return;
36
37 qint64 elapsedTimeNs = elapsedTimer.nsecsElapsed();
38 qint64 frameTimeNs = elapsedTimeNs - prevElapsedTimeNs;
39 if (prevFrameTimeNs != frameTimeNs) {
40 frameTime = qreal(frameTimeNs) / 1000000000.0;
41 Q_EMIT q->frameTimeChanged();
42 }
43
44 const qreal f = 0.1;
45 qreal newSmoothFrameTime = f * frameTime + (1.0 - f) * smoothFrameTime;
46 if (!qFuzzyCompare(newSmoothFrameTime, smoothFrameTime)) {
47 smoothFrameTime = newSmoothFrameTime;
48 Q_EMIT q->smoothFrameTimeChanged();
49 }
50
51 q->setElapsedTime(elapsedTime + frameTime);
52
53 const int frame = (firstTick && currentFrame > 0) ? 0 : currentFrame + 1;
54 q->setCurrentFrame(frame);
55
56 prevElapsedTimeNs = elapsedTimeNs;
57 prevFrameTimeNs = frameTimeNs;
58 firstTick = false;
59
60 Q_EMIT q->triggered();
61 }
62
63 // Handle the running/pausing state updates.
65 {
66 if (!componentComplete)
67 return;
68
69 if (running && !paused) {
70 if (firstTick) {
71 elapsedTime = 0;
72 elapsedTimer.start();
73 }
74 prevElapsedTimeNs = elapsedTimer.nsecsElapsed();
75 frameJob.start();
76 } else {
77 frameJob.stop();
78 }
79 }
80
81private:
82 QFrameAnimationJob frameJob;
83 QElapsedTimer elapsedTimer;
84 int currentFrame = 0;
85 qreal frameTime = 0.0;
86 qreal smoothFrameTime = 0.0;
87 qreal elapsedTime = 0.0;
88 qint64 prevFrameTimeNs = 0;
89 qint64 prevElapsedTimeNs = 0;
90 bool running = false;
91 bool paused = false;
92 bool componentComplete = false;
93 bool firstTick = true;
94};
95
147 : QObject(*(new QQuickFrameAnimationPrivate), parent)
148{
150 d->frameJob.addAnimationChangeListener(d, QAbstractAnimationJob::CurrentLoop);
151 d->frameJob.setLoopCount(-1);
152}
153
170{
171 Q_D(const QQuickFrameAnimation);
172 return d->running;
173}
174
176{
178 if (d->running != running) {
179 d->running = running;
180 d->firstTick = true;
182 d->updateState();
183 }
184}
185
196{
197 Q_D(const QQuickFrameAnimation);
198 return d->paused;
199}
200
202{
204 if (d->paused != paused) {
205 d->paused = paused;
207 d->updateState();
208 }
209}
210
249{
250 Q_D(const QQuickFrameAnimation);
251 return d->currentFrame;
252}
253
286{
287 Q_D(const QQuickFrameAnimation);
288 return d->frameTime;
289}
290
314{
315 Q_D(const QQuickFrameAnimation);
316 return d->smoothFrameTime;
317}
318
328{
329 Q_D(const QQuickFrameAnimation);
330 return d->elapsedTime;
331}
332
341{
342 setRunning(true);
343}
344
353{
354 setRunning(false);
355 setPaused(false);
356}
357
367{
368 stop();
369 start();
370}
371
380{
381 setPaused(true);
382}
383
392{
393 setPaused(false);
394}
395
410{
412 setElapsedTime(0);
413 setCurrentFrame(0);
414 d->prevElapsedTimeNs = 0;
415 d->elapsedTimer.start();
416}
417
422{
424 d->componentComplete = false;
425}
426
431{
433 d->componentComplete = true;
434 d->updateState();
435}
436
440void QQuickFrameAnimation::setCurrentFrame(int frame)
441{
443 if (d->currentFrame != frame) {
444 d->currentFrame = frame;
446 }
447}
448
452void QQuickFrameAnimation::setElapsedTime(qreal elapsedTime)
453{
455 if (!qFuzzyCompare(d->elapsedTime, elapsedTime)) {
456 d->elapsedTime = elapsedTime;
458 }
459}
460
462
463#include "moc_qquickframeanimation_p.cpp"
\inmodule QtCore
void start() noexcept
\typealias QElapsedTimer::Duration Synonym for std::chrono::nanoseconds.
qint64 nsecsElapsed() const noexcept
int duration() const override
\inmodule QtCore
Definition qobject.h:103
void animationCurrentLoopChanged(QAbstractAnimationJob *) override
bool isRunning() const
\qmlsignal QtQuick::FrameAnimation::triggered()
void resume()
\qmlmethod QtQuick::FrameAnimation::resume()
void setRunning(bool running)
void pause()
\qmlmethod QtQuick::FrameAnimation::pause()
QQuickFrameAnimation(QObject *parent=nullptr)
\qmltype FrameAnimation \instantiates QQuickFrameAnimation \inqmlmodule QtQuick
void reset()
\qmlmethod QtQuick::FrameAnimation::reset()
void restart()
\qmlmethod QtQuick::FrameAnimation::restart()
void start()
\qmlmethod QtQuick::FrameAnimation::start()
bool isPaused() const
\qmlproperty bool QtQuick::FrameAnimation::paused
void stop()
\qmlmethod QtQuick::FrameAnimation::stop()
Combined button and popup list for selecting options.
static Q_CONSTINIT QBasicAtomicInt running
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
GLfloat GLfloat f
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define Q_EMIT
long long qint64
Definition qtypes.h:60
double qreal
Definition qtypes.h:187
QFrame frame
[0]