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