1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "animation.h"
52
53#include <QIODevice>
54#include <QDataStream>
55
56class Frame
57{
58public:
59 Frame() = default;
60
61 int nodeCount() const
62 {
63 return m_nodePositions.size();
64 }
65
66 void setNodeCount(int nodeCount)
67 {
68 m_nodePositions.resize(asize: nodeCount);
69 }
70
71 QPointF nodePos(int idx) const
72 {
73 return m_nodePositions.at(i: idx);
74 }
75
76 void setNodePos(int idx, const QPointF &pos)
77 {
78 m_nodePositions[idx] = pos;
79 }
80
81private:
82 QVector<QPointF> m_nodePositions;
83};
84
85Animation::Animation() : m_currentFrame(0)
86{
87 m_frames.append(t: new Frame);
88}
89
90Animation::~Animation()
91{
92 qDeleteAll(c: m_frames);
93}
94
95void Animation::setTotalFrames(int totalFrames)
96{
97 while (m_frames.size() < totalFrames)
98 m_frames.append(t: new Frame);
99
100 while (totalFrames < m_frames.size())
101 delete m_frames.takeLast();
102
103 setCurrentFrame(m_currentFrame);
104}
105
106int Animation::totalFrames() const
107{
108 return m_frames.size();
109}
110
111void Animation::setCurrentFrame(int currentFrame)
112{
113 m_currentFrame = qBound(min: 0, val: currentFrame, max: totalFrames() - 1);
114}
115
116int Animation::currentFrame() const
117{
118 return m_currentFrame;
119}
120
121void Animation::setNodeCount(int nodeCount)
122{
123 Frame *frame = m_frames.at(i: m_currentFrame);
124 frame->setNodeCount(nodeCount);
125}
126
127int Animation::nodeCount() const
128{
129 Frame *frame = m_frames.at(i: m_currentFrame);
130 return frame->nodeCount();
131}
132
133void Animation::setNodePos(int idx, const QPointF &pos)
134{
135 Frame *frame = m_frames.at(i: m_currentFrame);
136 frame->setNodePos(idx, pos);
137}
138
139QPointF Animation::nodePos(int idx) const
140{
141 Frame *frame = m_frames.at(i: m_currentFrame);
142 return frame->nodePos(idx);
143}
144
145QString Animation::name() const
146{
147 return m_name;
148}
149
150void Animation::setName(const QString &name)
151{
152 m_name = name;
153}
154
155void Animation::save(QIODevice *device) const
156{
157 QDataStream stream(device);
158 stream << m_name;
159 stream << m_frames.size();
160 for (const Frame *frame : qAsConst(t: m_frames)) {
161 stream << frame->nodeCount();
162 for (int i = 0; i < frame->nodeCount(); ++i)
163 stream << frame->nodePos(idx: i);
164 }
165}
166
167void Animation::load(QIODevice *device)
168{
169 qDeleteAll(c: m_frames);
170 m_frames.clear();
171
172 QDataStream stream(device);
173 stream >> m_name;
174
175 int frameCount;
176 stream >> frameCount;
177
178 for (int i = 0; i < frameCount; ++i) {
179 int nodeCount;
180 stream >> nodeCount;
181
182 Frame *frame = new Frame;
183 frame->setNodeCount(nodeCount);
184
185 for (int j = 0; j < nodeCount; ++j) {
186 QPointF pos;
187 stream >> pos;
188 frame->setNodePos(idx: j, pos);
189 }
190
191 m_frames.append(t: frame);
192 }
193}
194

source code of qtbase/examples/widgets/animation/stickman/animation.cpp