source: trunk/examples/animation/moveblocks/main.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: 10.2 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 <QtCore>
43#include <QtGui>
44
45//![15]
46class StateSwitchEvent: public QEvent
47{
48public:
49 StateSwitchEvent()
50 : QEvent(Type(StateSwitchType))
51 {
52 }
53
54 StateSwitchEvent(int rand)
55 : QEvent(Type(StateSwitchType)),
56 m_rand(rand)
57 {
58 }
59
60 enum { StateSwitchType = QEvent::User + 256 };
61
62 int rand() const { return m_rand; }
63
64private:
65 int m_rand;
66};
67//![15]
68
69//![16]
70class QGraphicsRectWidget : public QGraphicsWidget
71{
72public:
73 void paint(QPainter *painter, const QStyleOptionGraphicsItem *,
74 QWidget *)
75 {
76 painter->fillRect(rect(), Qt::blue);
77 }
78};
79//![16]
80
81class StateSwitchTransition: public QAbstractTransition
82{
83public:
84 StateSwitchTransition(int rand)
85 : QAbstractTransition(),
86 m_rand(rand)
87 {
88 }
89
90protected:
91//![14]
92 virtual bool eventTest(QEvent *event)
93 {
94 return (event->type() == QEvent::Type(StateSwitchEvent::StateSwitchType))
95 && (static_cast<StateSwitchEvent *>(event)->rand() == m_rand);
96 }
97//![14]
98
99 virtual void onTransition(QEvent *) {}
100
101private:
102 int m_rand;
103};
104
105//![10]
106class StateSwitcher : public QState
107{
108 Q_OBJECT
109public:
110 StateSwitcher(QStateMachine *machine)
111 : QState(machine), m_stateCount(0), m_lastIndex(0)
112 { }
113//![10]
114
115//![11]
116 virtual void onEntry(QEvent *)
117 {
118 int n;
119 while ((n = (qrand() % m_stateCount + 1)) == m_lastIndex)
120 { }
121 m_lastIndex = n;
122 machine()->postEvent(new StateSwitchEvent(n));
123 }
124 virtual void onExit(QEvent *) {}
125//![11]
126
127//![12]
128 void addState(QState *state, QAbstractAnimation *animation) {
129 StateSwitchTransition *trans = new StateSwitchTransition(++m_stateCount);
130 trans->setTargetState(state);
131 addTransition(trans);
132 trans->addAnimation(animation);
133 }
134//![12]
135
136private:
137 int m_stateCount;
138 int m_lastIndex;
139};
140
141//![13]
142QState *createGeometryState(QObject *w1, const QRect &rect1,
143 QObject *w2, const QRect &rect2,
144 QObject *w3, const QRect &rect3,
145 QObject *w4, const QRect &rect4,
146 QState *parent)
147{
148 QState *result = new QState(parent);
149 result->assignProperty(w1, "geometry", rect1);
150 result->assignProperty(w1, "geometry", rect1);
151 result->assignProperty(w2, "geometry", rect2);
152 result->assignProperty(w3, "geometry", rect3);
153 result->assignProperty(w4, "geometry", rect4);
154
155 return result;
156}
157//![13]
158
159int main(int argc, char **argv)
160{
161 QApplication app(argc, argv);
162
163#if 0
164 QWidget window;
165 QPalette palette;
166 palette.setBrush(QPalette::Window, Qt::black);
167 window.setPalette(palette);
168 QPushButton *button1 = new QPushButton("A", &window);
169 QPushButton *button2 = new QPushButton("B", &window);
170 QPushButton *button3 = new QPushButton("C", &window);
171 QPushButton *button4 = new QPushButton("D", &window);
172
173 button1->setObjectName("button1");
174 button2->setObjectName("button2");
175 button3->setObjectName("button3");
176 button4->setObjectName("button4");
177#else
178//![1]
179 QGraphicsRectWidget *button1 = new QGraphicsRectWidget;
180 QGraphicsRectWidget *button2 = new QGraphicsRectWidget;
181 QGraphicsRectWidget *button3 = new QGraphicsRectWidget;
182 QGraphicsRectWidget *button4 = new QGraphicsRectWidget;
183 button2->setZValue(1);
184 button3->setZValue(2);
185 button4->setZValue(3);
186 QGraphicsScene scene(0, 0, 300, 300);
187 scene.setBackgroundBrush(Qt::black);
188 scene.addItem(button1);
189 scene.addItem(button2);
190 scene.addItem(button3);
191 scene.addItem(button4);
192//![1]
193 QGraphicsView window(&scene);
194 window.setFrameStyle(0);
195 window.setAlignment(Qt::AlignLeft | Qt::AlignTop);
196 window.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
197 window.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
198#endif
199//![2]
200 QStateMachine machine;
201
202 QState *group = new QState();
203 group->setObjectName("group");
204 QTimer timer;
205 timer.setInterval(1250);
206 timer.setSingleShot(true);
207 QObject::connect(group, SIGNAL(entered()), &timer, SLOT(start()));
208//![2]
209
210//![3]
211 QState *state1;
212 QState *state2;
213 QState *state3;
214 QState *state4;
215 QState *state5;
216 QState *state6;
217 QState *state7;
218
219 state1 = createGeometryState(button1, QRect(100, 0, 50, 50),
220 button2, QRect(150, 0, 50, 50),
221 button3, QRect(200, 0, 50, 50),
222 button4, QRect(250, 0, 50, 50),
223 group);
224//![3]
225 state2 = createGeometryState(button1, QRect(250, 100, 50, 50),
226 button2, QRect(250, 150, 50, 50),
227 button3, QRect(250, 200, 50, 50),
228 button4, QRect(250, 250, 50, 50),
229 group);
230 state3 = createGeometryState(button1, QRect(150, 250, 50, 50),
231 button2, QRect(100, 250, 50, 50),
232 button3, QRect(50, 250, 50, 50),
233 button4, QRect(0, 250, 50, 50),
234 group);
235 state4 = createGeometryState(button1, QRect(0, 150, 50, 50),
236 button2, QRect(0, 100, 50, 50),
237 button3, QRect(0, 50, 50, 50),
238 button4, QRect(0, 0, 50, 50),
239 group);
240 state5 = createGeometryState(button1, QRect(100, 100, 50, 50),
241 button2, QRect(150, 100, 50, 50),
242 button3, QRect(100, 150, 50, 50),
243 button4, QRect(150, 150, 50, 50),
244 group);
245 state6 = createGeometryState(button1, QRect(50, 50, 50, 50),
246 button2, QRect(200, 50, 50, 50),
247 button3, QRect(50, 200, 50, 50),
248 button4, QRect(200, 200, 50, 50),
249 group);
250//![4]
251 state7 = createGeometryState(button1, QRect(0, 0, 50, 50),
252 button2, QRect(250, 0, 50, 50),
253 button3, QRect(0, 250, 50, 50),
254 button4, QRect(250, 250, 50, 50),
255 group);
256 group->setInitialState(state1);
257//![4]
258
259//![5]
260 QParallelAnimationGroup animationGroup;
261 QSequentialAnimationGroup *subGroup;
262
263 QPropertyAnimation *anim = new QPropertyAnimation(button4, "geometry");
264 anim->setDuration(1000);
265 anim->setEasingCurve(QEasingCurve::OutElastic);
266 animationGroup.addAnimation(anim);
267//![5]
268
269//![6]
270 subGroup = new QSequentialAnimationGroup(&animationGroup);
271 subGroup->addPause(100);
272 anim = new QPropertyAnimation(button3, "geometry");
273 anim->setDuration(1000);
274 anim->setEasingCurve(QEasingCurve::OutElastic);
275 subGroup->addAnimation(anim);
276//![6]
277
278 subGroup = new QSequentialAnimationGroup(&animationGroup);
279 subGroup->addPause(150);
280 anim = new QPropertyAnimation(button2, "geometry");
281 anim->setDuration(1000);
282 anim->setEasingCurve(QEasingCurve::OutElastic);
283 subGroup->addAnimation(anim);
284
285 subGroup = new QSequentialAnimationGroup(&animationGroup);
286 subGroup->addPause(200);
287 anim = new QPropertyAnimation(button1, "geometry");
288 anim->setDuration(1000);
289 anim->setEasingCurve(QEasingCurve::OutElastic);
290 subGroup->addAnimation(anim);
291
292//![7]
293 StateSwitcher *stateSwitcher = new StateSwitcher(&machine);
294 stateSwitcher->setObjectName("stateSwitcher");
295 group->addTransition(&timer, SIGNAL(timeout()), stateSwitcher);
296 stateSwitcher->addState(state1, &animationGroup);
297 stateSwitcher->addState(state2, &animationGroup);
298//![7]
299 stateSwitcher->addState(state3, &animationGroup);
300 stateSwitcher->addState(state4, &animationGroup);
301 stateSwitcher->addState(state5, &animationGroup);
302 stateSwitcher->addState(state6, &animationGroup);
303//![8]
304 stateSwitcher->addState(state7, &animationGroup);
305//![8]
306
307//![9]
308 machine.addState(group);
309 machine.setInitialState(group);
310 machine.start();
311//![9]
312
313 window.resize(300, 300);
314 window.show();
315
316 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
317
318 return app.exec();
319}
320
321#include "main.moc"
Note: See TracBrowser for help on using the repository browser.