source: trunk/examples/animation/stickman/animation.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "animation.h"
43
44#include <QPointF>
45#include <QIODevice>
46#include <QDataStream>
47
48class Frame
49{
50public:
51 Frame() {
52 }
53
54 int nodeCount() const
55 {
56 return m_nodePositions.size();
57 }
58
59 void setNodeCount(int nodeCount)
60 {
61 while (nodeCount > m_nodePositions.size())
62 m_nodePositions.append(QPointF());
63
64 while (nodeCount < m_nodePositions.size())
65 m_nodePositions.removeLast();
66 }
67
68 QPointF nodePos(int idx) const
69 {
70 return m_nodePositions.at(idx);
71 }
72
73 void setNodePos(int idx, const QPointF &pos)
74 {
75 m_nodePositions[idx] = pos;
76 }
77
78private:
79 QList<QPointF> m_nodePositions;
80};
81
82Animation::Animation()
83{
84 m_currentFrame = 0;
85 m_frames.append(new Frame);
86}
87
88Animation::~Animation()
89{
90 qDeleteAll(m_frames);
91}
92
93void Animation::setTotalFrames(int totalFrames)
94{
95 while (m_frames.size() < totalFrames)
96 m_frames.append(new Frame);
97
98 while (totalFrames < m_frames.size())
99 delete m_frames.takeLast();
100}
101
102int Animation::totalFrames() const
103{
104 return m_frames.size();
105}
106
107void Animation::setCurrentFrame(int currentFrame)
108{
109 m_currentFrame = qMax(qMin(currentFrame, totalFrames()-1), 0);
110}
111
112int Animation::currentFrame() const
113{
114 return m_currentFrame;
115}
116
117void Animation::setNodeCount(int nodeCount)
118{
119 Frame *frame = m_frames.at(m_currentFrame);
120 frame->setNodeCount(nodeCount);
121}
122
123int Animation::nodeCount() const
124{
125 Frame *frame = m_frames.at(m_currentFrame);
126 return frame->nodeCount();
127}
128
129void Animation::setNodePos(int idx, const QPointF &pos)
130{
131 Frame *frame = m_frames.at(m_currentFrame);
132 frame->setNodePos(idx, pos);
133}
134
135QPointF Animation::nodePos(int idx) const
136{
137 Frame *frame = m_frames.at(m_currentFrame);
138 return frame->nodePos(idx);
139}
140
141QString Animation::name() const
142{
143 return m_name;
144}
145
146void Animation::setName(const QString &name)
147{
148 m_name = name;
149}
150
151void Animation::save(QIODevice *device) const
152{
153 QDataStream stream(device);
154 stream << m_name;
155 stream << m_frames.size();
156 foreach (Frame *frame, m_frames) {
157 stream << frame->nodeCount();
158 for (int i=0; i<frame->nodeCount(); ++i)
159 stream << frame->nodePos(i);
160 }
161}
162
163void Animation::load(QIODevice *device)
164{
165 if (!m_frames.isEmpty())
166 qDeleteAll(m_frames);
167
168 m_frames.clear();
169
170 QDataStream stream(device);
171 stream >> m_name;
172
173 int frameCount;
174 stream >> frameCount;
175
176 for (int i=0; i<frameCount; ++i) {
177
178 int nodeCount;
179 stream >> nodeCount;
180
181 Frame *frame = new Frame;
182 frame->setNodeCount(nodeCount);
183
184 for (int j=0; j<nodeCount; ++j) {
185 QPointF pos;
186 stream >> pos;
187
188 frame->setNodePos(j, pos);
189 }
190
191 m_frames.append(frame);
192 }
193}
Note: See TracBrowser for help on using the repository browser.