1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickanimatednode_p.h"
38
39#include <QtQuick/qquickitem.h>
40#include <QtQuick/qquickwindow.h>
41
42// based on qtdeclarative/examples/quick/scenegraph/threadedanimation
43
44QT_BEGIN_NAMESPACE
45
46QQuickAnimatedNode::QQuickAnimatedNode(QQuickItem *target)
47 : m_window(target->window())
48{
49}
50
51bool QQuickAnimatedNode::isRunning() const
52{
53 return m_running;
54}
55
56int QQuickAnimatedNode::currentTime() const
57{
58 int time = m_currentTime;
59 if (m_running)
60 time += m_timer.elapsed();
61 return time;
62}
63
64void QQuickAnimatedNode::setCurrentTime(int time)
65{
66 m_currentTime = time;
67 m_timer.restart();
68}
69
70int QQuickAnimatedNode::duration() const
71{
72 return m_duration;
73}
74
75void QQuickAnimatedNode::setDuration(int duration)
76{
77 m_duration = duration;
78}
79
80int QQuickAnimatedNode::loopCount() const
81{
82 return m_loopCount;
83}
84
85void QQuickAnimatedNode::setLoopCount(int count)
86{
87 m_loopCount = count;
88}
89
90void QQuickAnimatedNode::sync(QQuickItem *target)
91{
92 Q_UNUSED(target);
93}
94
95QQuickWindow *QQuickAnimatedNode::window() const
96{
97 return m_window;
98}
99
100void QQuickAnimatedNode::start(int duration)
101{
102 if (m_running)
103 return;
104
105 m_running = true;
106 m_currentLoop = 0;
107 m_timer.restart();
108 if (duration > 0)
109 m_duration = duration;
110
111 connect(sender: m_window, signal: &QQuickWindow::beforeRendering, receiver: this, slot: &QQuickAnimatedNode::advance, type: Qt::DirectConnection);
112 connect(sender: m_window, signal: &QQuickWindow::frameSwapped, receiver: this, slot: &QQuickAnimatedNode::update, type: Qt::DirectConnection);
113
114 // If we're inside a QQuickWidget, this call is necessary to ensure the widget
115 // gets updated for the first time.
116 m_window->update();
117
118 emit started();
119}
120
121void QQuickAnimatedNode::restart()
122{
123 stop();
124 start();
125}
126
127void QQuickAnimatedNode::stop()
128{
129 if (!m_running)
130 return;
131
132 m_running = false;
133 disconnect(sender: m_window, signal: &QQuickWindow::beforeRendering, receiver: this, slot: &QQuickAnimatedNode::advance);
134 disconnect(sender: m_window, signal: &QQuickWindow::frameSwapped, receiver: this, slot: &QQuickAnimatedNode::update);
135 emit stopped();
136}
137
138void QQuickAnimatedNode::updateCurrentTime(int time)
139{
140 Q_UNUSED(time);
141}
142
143void QQuickAnimatedNode::advance()
144{
145 int time = currentTime();
146 if (time > m_duration) {
147 time = 0;
148 setCurrentTime(0);
149
150 if (m_loopCount > 0 && ++m_currentLoop >= m_loopCount) {
151 time = m_duration; // complete
152 stop();
153 }
154 }
155 updateCurrentTime(time);
156
157 // If we're inside a QQuickWidget, this call is necessary to ensure the widget gets updated.
158 m_window->update();
159}
160
161void QQuickAnimatedNode::update()
162{
163 if (m_running)
164 m_window->update();
165}
166
167QT_END_NAMESPACE
168

source code of qtquickcontrols2/src/quickcontrols2/qquickanimatednode.cpp