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

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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