1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 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: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 [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtCore>
|
---|
43 | #include <QtGui>
|
---|
44 |
|
---|
45 | //![15]
|
---|
46 | class StateSwitchEvent: public QEvent
|
---|
47 | {
|
---|
48 | public:
|
---|
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 |
|
---|
64 | private:
|
---|
65 | int m_rand;
|
---|
66 | };
|
---|
67 | //![15]
|
---|
68 |
|
---|
69 | //![16]
|
---|
70 | class QGraphicsRectWidget : public QGraphicsWidget
|
---|
71 | {
|
---|
72 | public:
|
---|
73 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *,
|
---|
74 | QWidget *)
|
---|
75 | {
|
---|
76 | painter->fillRect(rect(), Qt::blue);
|
---|
77 | }
|
---|
78 | };
|
---|
79 | //![16]
|
---|
80 |
|
---|
81 | class StateSwitchTransition: public QAbstractTransition
|
---|
82 | {
|
---|
83 | public:
|
---|
84 | StateSwitchTransition(int rand)
|
---|
85 | : QAbstractTransition(),
|
---|
86 | m_rand(rand)
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected:
|
---|
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 |
|
---|
101 | private:
|
---|
102 | int m_rand;
|
---|
103 | };
|
---|
104 |
|
---|
105 | //![10]
|
---|
106 | class StateSwitcher : public QState
|
---|
107 | {
|
---|
108 | Q_OBJECT
|
---|
109 | public:
|
---|
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 |
|
---|
136 | private:
|
---|
137 | int m_stateCount;
|
---|
138 | int m_lastIndex;
|
---|
139 | };
|
---|
140 |
|
---|
141 | //![13]
|
---|
142 | QState *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(w2, "geometry", rect2);
|
---|
151 | result->assignProperty(w3, "geometry", rect3);
|
---|
152 | result->assignProperty(w4, "geometry", rect4);
|
---|
153 |
|
---|
154 | return result;
|
---|
155 | }
|
---|
156 | //![13]
|
---|
157 |
|
---|
158 | int main(int argc, char **argv)
|
---|
159 | {
|
---|
160 | QApplication app(argc, argv);
|
---|
161 |
|
---|
162 | #if 0
|
---|
163 | QWidget window;
|
---|
164 | QPalette palette;
|
---|
165 | palette.setBrush(QPalette::Window, Qt::black);
|
---|
166 | window.setPalette(palette);
|
---|
167 | QPushButton *button1 = new QPushButton("A", &window);
|
---|
168 | QPushButton *button2 = new QPushButton("B", &window);
|
---|
169 | QPushButton *button3 = new QPushButton("C", &window);
|
---|
170 | QPushButton *button4 = new QPushButton("D", &window);
|
---|
171 |
|
---|
172 | button1->setObjectName("button1");
|
---|
173 | button2->setObjectName("button2");
|
---|
174 | button3->setObjectName("button3");
|
---|
175 | button4->setObjectName("button4");
|
---|
176 | #else
|
---|
177 | //![1]
|
---|
178 | QGraphicsRectWidget *button1 = new QGraphicsRectWidget;
|
---|
179 | QGraphicsRectWidget *button2 = new QGraphicsRectWidget;
|
---|
180 | QGraphicsRectWidget *button3 = new QGraphicsRectWidget;
|
---|
181 | QGraphicsRectWidget *button4 = new QGraphicsRectWidget;
|
---|
182 | button2->setZValue(1);
|
---|
183 | button3->setZValue(2);
|
---|
184 | button4->setZValue(3);
|
---|
185 | QGraphicsScene scene(0, 0, 300, 300);
|
---|
186 | scene.setBackgroundBrush(Qt::black);
|
---|
187 | scene.addItem(button1);
|
---|
188 | scene.addItem(button2);
|
---|
189 | scene.addItem(button3);
|
---|
190 | scene.addItem(button4);
|
---|
191 | //![1]
|
---|
192 | QGraphicsView window(&scene);
|
---|
193 | window.setFrameStyle(0);
|
---|
194 | window.setAlignment(Qt::AlignLeft | Qt::AlignTop);
|
---|
195 | window.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
196 | window.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
197 | #endif
|
---|
198 | //![2]
|
---|
199 | QStateMachine machine;
|
---|
200 |
|
---|
201 | QState *group = new QState();
|
---|
202 | group->setObjectName("group");
|
---|
203 | QTimer timer;
|
---|
204 | timer.setInterval(1250);
|
---|
205 | timer.setSingleShot(true);
|
---|
206 | QObject::connect(group, SIGNAL(entered()), &timer, SLOT(start()));
|
---|
207 | //![2]
|
---|
208 |
|
---|
209 | //![3]
|
---|
210 | QState *state1;
|
---|
211 | QState *state2;
|
---|
212 | QState *state3;
|
---|
213 | QState *state4;
|
---|
214 | QState *state5;
|
---|
215 | QState *state6;
|
---|
216 | QState *state7;
|
---|
217 |
|
---|
218 | state1 = createGeometryState(button1, QRect(100, 0, 50, 50),
|
---|
219 | button2, QRect(150, 0, 50, 50),
|
---|
220 | button3, QRect(200, 0, 50, 50),
|
---|
221 | button4, QRect(250, 0, 50, 50),
|
---|
222 | group);
|
---|
223 | //![3]
|
---|
224 | state2 = createGeometryState(button1, QRect(250, 100, 50, 50),
|
---|
225 | button2, QRect(250, 150, 50, 50),
|
---|
226 | button3, QRect(250, 200, 50, 50),
|
---|
227 | button4, QRect(250, 250, 50, 50),
|
---|
228 | group);
|
---|
229 | state3 = createGeometryState(button1, QRect(150, 250, 50, 50),
|
---|
230 | button2, QRect(100, 250, 50, 50),
|
---|
231 | button3, QRect(50, 250, 50, 50),
|
---|
232 | button4, QRect(0, 250, 50, 50),
|
---|
233 | group);
|
---|
234 | state4 = createGeometryState(button1, QRect(0, 150, 50, 50),
|
---|
235 | button2, QRect(0, 100, 50, 50),
|
---|
236 | button3, QRect(0, 50, 50, 50),
|
---|
237 | button4, QRect(0, 0, 50, 50),
|
---|
238 | group);
|
---|
239 | state5 = createGeometryState(button1, QRect(100, 100, 50, 50),
|
---|
240 | button2, QRect(150, 100, 50, 50),
|
---|
241 | button3, QRect(100, 150, 50, 50),
|
---|
242 | button4, QRect(150, 150, 50, 50),
|
---|
243 | group);
|
---|
244 | state6 = createGeometryState(button1, QRect(50, 50, 50, 50),
|
---|
245 | button2, QRect(200, 50, 50, 50),
|
---|
246 | button3, QRect(50, 200, 50, 50),
|
---|
247 | button4, QRect(200, 200, 50, 50),
|
---|
248 | group);
|
---|
249 | //![4]
|
---|
250 | state7 = createGeometryState(button1, QRect(0, 0, 50, 50),
|
---|
251 | button2, QRect(250, 0, 50, 50),
|
---|
252 | button3, QRect(0, 250, 50, 50),
|
---|
253 | button4, QRect(250, 250, 50, 50),
|
---|
254 | group);
|
---|
255 | group->setInitialState(state1);
|
---|
256 | //![4]
|
---|
257 |
|
---|
258 | //![5]
|
---|
259 | QParallelAnimationGroup animationGroup;
|
---|
260 | QSequentialAnimationGroup *subGroup;
|
---|
261 |
|
---|
262 | QPropertyAnimation *anim = new QPropertyAnimation(button4, "geometry");
|
---|
263 | anim->setDuration(1000);
|
---|
264 | anim->setEasingCurve(QEasingCurve::OutElastic);
|
---|
265 | animationGroup.addAnimation(anim);
|
---|
266 | //![5]
|
---|
267 |
|
---|
268 | //![6]
|
---|
269 | subGroup = new QSequentialAnimationGroup(&animationGroup);
|
---|
270 | subGroup->addPause(100);
|
---|
271 | anim = new QPropertyAnimation(button3, "geometry");
|
---|
272 | anim->setDuration(1000);
|
---|
273 | anim->setEasingCurve(QEasingCurve::OutElastic);
|
---|
274 | subGroup->addAnimation(anim);
|
---|
275 | //![6]
|
---|
276 |
|
---|
277 | subGroup = new QSequentialAnimationGroup(&animationGroup);
|
---|
278 | subGroup->addPause(150);
|
---|
279 | anim = new QPropertyAnimation(button2, "geometry");
|
---|
280 | anim->setDuration(1000);
|
---|
281 | anim->setEasingCurve(QEasingCurve::OutElastic);
|
---|
282 | subGroup->addAnimation(anim);
|
---|
283 |
|
---|
284 | subGroup = new QSequentialAnimationGroup(&animationGroup);
|
---|
285 | subGroup->addPause(200);
|
---|
286 | anim = new QPropertyAnimation(button1, "geometry");
|
---|
287 | anim->setDuration(1000);
|
---|
288 | anim->setEasingCurve(QEasingCurve::OutElastic);
|
---|
289 | subGroup->addAnimation(anim);
|
---|
290 |
|
---|
291 | //![7]
|
---|
292 | StateSwitcher *stateSwitcher = new StateSwitcher(&machine);
|
---|
293 | stateSwitcher->setObjectName("stateSwitcher");
|
---|
294 | group->addTransition(&timer, SIGNAL(timeout()), stateSwitcher);
|
---|
295 | stateSwitcher->addState(state1, &animationGroup);
|
---|
296 | stateSwitcher->addState(state2, &animationGroup);
|
---|
297 | //![7]
|
---|
298 | stateSwitcher->addState(state3, &animationGroup);
|
---|
299 | stateSwitcher->addState(state4, &animationGroup);
|
---|
300 | stateSwitcher->addState(state5, &animationGroup);
|
---|
301 | stateSwitcher->addState(state6, &animationGroup);
|
---|
302 | //![8]
|
---|
303 | stateSwitcher->addState(state7, &animationGroup);
|
---|
304 | //![8]
|
---|
305 |
|
---|
306 | //![9]
|
---|
307 | machine.addState(group);
|
---|
308 | machine.setInitialState(group);
|
---|
309 | machine.start();
|
---|
310 | //![9]
|
---|
311 |
|
---|
312 | window.resize(300, 300);
|
---|
313 | window.show();
|
---|
314 |
|
---|
315 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
|
---|
316 |
|
---|
317 | return app.exec();
|
---|
318 | }
|
---|
319 |
|
---|
320 | #include "main.moc"
|
---|