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

source code of qtbase/examples/widgets/animation/moveblocks/main.cpp