1// Copyright (C) 2017 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
4#include "qquickanimatednode_p.h"
5
6#include <QtQuick/qquickitem.h>
7#include <QtQuick/qquickwindow.h>
8
9// based on qtdeclarative/examples/quick/scenegraph/threadedanimation
10
11QT_BEGIN_NAMESPACE
12
13QQuickAnimatedNode::QQuickAnimatedNode(QQuickItem *target)
14 : m_window(target->window())
15{
16}
17
18bool QQuickAnimatedNode::isRunning() const
19{
20 return m_running;
21}
22
23int QQuickAnimatedNode::currentTime() const
24{
25 int time = m_currentTime;
26 if (m_running)
27 time += m_timer.elapsed();
28 return time;
29}
30
31void QQuickAnimatedNode::setCurrentTime(int time)
32{
33 m_currentTime = time;
34 m_timer.restart();
35}
36
37int QQuickAnimatedNode::duration() const
38{
39 return m_duration;
40}
41
42void QQuickAnimatedNode::setDuration(int duration)
43{
44 m_duration = duration;
45}
46
47int QQuickAnimatedNode::loopCount() const
48{
49 return m_loopCount;
50}
51
52void QQuickAnimatedNode::setLoopCount(int count)
53{
54 m_loopCount = count;
55}
56
57void QQuickAnimatedNode::sync(QQuickItem *target)
58{
59 Q_UNUSED(target);
60}
61
62QQuickWindow *QQuickAnimatedNode::window() const
63{
64 return m_window;
65}
66
67void QQuickAnimatedNode::start(int duration)
68{
69 if (m_running)
70 return;
71
72 m_running = true;
73 m_currentLoop = 0;
74 m_timer.restart();
75 if (duration > 0)
76 m_duration = duration;
77
78 connect(sender: m_window, signal: &QQuickWindow::beforeRendering, context: this, slot: &QQuickAnimatedNode::advance, type: Qt::DirectConnection);
79 connect(sender: m_window, signal: &QQuickWindow::frameSwapped, context: this, slot: &QQuickAnimatedNode::update, type: Qt::DirectConnection);
80
81 // If we're inside a QQuickWidget, this call is necessary to ensure the widget
82 // gets updated for the first time.
83 m_window->update();
84
85 emit started();
86}
87
88void QQuickAnimatedNode::restart()
89{
90 stop();
91 start();
92}
93
94void QQuickAnimatedNode::stop()
95{
96 if (!m_running)
97 return;
98
99 m_running = false;
100 disconnect(sender: m_window, signal: &QQuickWindow::beforeRendering, receiver: this, slot: &QQuickAnimatedNode::advance);
101 disconnect(sender: m_window, signal: &QQuickWindow::frameSwapped, receiver: this, slot: &QQuickAnimatedNode::update);
102 emit stopped();
103}
104
105void QQuickAnimatedNode::updateCurrentTime(int time)
106{
107 Q_UNUSED(time);
108}
109
110void QQuickAnimatedNode::advance()
111{
112 int time = currentTime();
113 if (time > m_duration) {
114 time = 0;
115 setCurrentTime(0);
116
117 if (m_loopCount > 0 && ++m_currentLoop >= m_loopCount) {
118 time = m_duration; // complete
119 stop();
120 }
121 }
122 updateCurrentTime(time);
123
124 // If we're inside a QQuickWidget, this call is necessary to ensure the widget gets updated.
125 m_window->update();
126}
127
128void QQuickAnimatedNode::update()
129{
130 if (m_running)
131 m_window->update();
132}
133
134QT_END_NAMESPACE
135
136#include "moc_qquickanimatednode_p.cpp"
137

source code of qtdeclarative/src/quickcontrolsimpl/qquickanimatednode.cpp