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 <QtWidgets>
52
53class Pixmap : public QGraphicsObject
54{
55 Q_OBJECT
56public:
57 Pixmap(const QPixmap &pix) : QGraphicsObject(), p(pix)
58 {
59 }
60
61 void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
62 {
63 painter->drawPixmap(p: QPointF(), pm: p);
64 }
65
66 QRectF boundingRect() const override
67 {
68 return QRectF( QPointF(0, 0), p.size());
69 }
70
71private:
72 QPixmap p;
73};
74
75class GraphicsView : public QGraphicsView
76{
77public:
78 GraphicsView(QGraphicsScene *scene) : QGraphicsView(scene)
79 {
80 }
81
82 void resizeEvent(QResizeEvent *) override
83 {
84 fitInView(rect: sceneRect(), aspectRadioMode: Qt::KeepAspectRatio);
85 }
86};
87
88int main(int argc, char *argv[])
89{
90 Q_INIT_RESOURCE(states);
91
92 QApplication app(argc, argv);
93
94 // Text edit and button
95 QTextEdit *edit = new QTextEdit;
96 edit->setText("asdf lkjha yuoiqwe asd iuaysd u iasyd uiy "
97 "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy "
98 "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy "
99 "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy!");
100
101 QPushButton *button = new QPushButton;
102 QGraphicsProxyWidget *buttonProxy = new QGraphicsProxyWidget;
103 buttonProxy->setWidget(button);
104 QGraphicsProxyWidget *editProxy = new QGraphicsProxyWidget;
105 editProxy->setWidget(edit);
106
107 QGroupBox *box = new QGroupBox;
108 box->setFlat(true);
109 box->setTitle("Options");
110
111 QVBoxLayout *layout2 = new QVBoxLayout;
112 box->setLayout(layout2);
113 layout2->addWidget(new QRadioButton("Herring"));
114 layout2->addWidget(new QRadioButton("Blue Parrot"));
115 layout2->addWidget(new QRadioButton("Petunias"));
116 layout2->addStretch();
117
118 QGraphicsProxyWidget *boxProxy = new QGraphicsProxyWidget;
119 boxProxy->setWidget(box);
120
121 // Parent widget
122 QGraphicsWidget *widget = new QGraphicsWidget;
123 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical, widget);
124 layout->addItem(item: editProxy);
125 layout->addItem(item: buttonProxy);
126 widget->setLayout(layout);
127
128 Pixmap *p1 = new Pixmap(QPixmap(":/digikam.png"));
129 Pixmap *p2 = new Pixmap(QPixmap(":/akregator.png"));
130 Pixmap *p3 = new Pixmap(QPixmap(":/accessories-dictionary.png"));
131 Pixmap *p4 = new Pixmap(QPixmap(":/k3b.png"));
132 Pixmap *p5 = new Pixmap(QPixmap(":/help-browser.png"));
133 Pixmap *p6 = new Pixmap(QPixmap(":/kchart.png"));
134
135 QGraphicsScene scene(0, 0, 400, 300);
136 scene.setBackgroundBrush(scene.palette().window());
137 scene.addItem(item: widget);
138 scene.addItem(item: boxProxy);
139 scene.addItem(item: p1);
140 scene.addItem(item: p2);
141 scene.addItem(item: p3);
142 scene.addItem(item: p4);
143 scene.addItem(item: p5);
144 scene.addItem(item: p6);
145
146 QStateMachine machine;
147 QState *state1 = new QState(&machine);
148 QState *state2 = new QState(&machine);
149 QState *state3 = new QState(&machine);
150 machine.setInitialState(state1);
151
152 // State 1
153 state1->assignProperty(object: button, name: "text", value: "Switch to state 2");
154 state1->assignProperty(object: widget, name: "geometry", value: QRectF(0, 0, 400, 150));
155 state1->assignProperty(object: box, name: "geometry", value: QRect(-200, 150, 200, 150));
156 state1->assignProperty(object: p1, name: "pos", value: QPointF(68, 200)); // 185));
157 state1->assignProperty(object: p2, name: "pos", value: QPointF(168, 200)); // 185));
158 state1->assignProperty(object: p3, name: "pos", value: QPointF(268, 200)); // 185));
159 state1->assignProperty(object: p4, name: "pos", value: QPointF(68 - 150, 48 - 150));
160 state1->assignProperty(object: p5, name: "pos", value: QPointF(168, 48 - 150));
161 state1->assignProperty(object: p6, name: "pos", value: QPointF(268 + 150, 48 - 150));
162 state1->assignProperty(object: p1, name: "rotation", value: qreal(0));
163 state1->assignProperty(object: p2, name: "rotation", value: qreal(0));
164 state1->assignProperty(object: p3, name: "rotation", value: qreal(0));
165 state1->assignProperty(object: p4, name: "rotation", value: qreal(-270));
166 state1->assignProperty(object: p5, name: "rotation", value: qreal(-90));
167 state1->assignProperty(object: p6, name: "rotation", value: qreal(270));
168 state1->assignProperty(object: boxProxy, name: "opacity", value: qreal(0));
169 state1->assignProperty(object: p1, name: "opacity", value: qreal(1));
170 state1->assignProperty(object: p2, name: "opacity", value: qreal(1));
171 state1->assignProperty(object: p3, name: "opacity", value: qreal(1));
172 state1->assignProperty(object: p4, name: "opacity", value: qreal(0));
173 state1->assignProperty(object: p5, name: "opacity", value: qreal(0));
174 state1->assignProperty(object: p6, name: "opacity", value: qreal(0));
175
176 // State 2
177 state2->assignProperty(object: button, name: "text", value: "Switch to state 3");
178 state2->assignProperty(object: widget, name: "geometry", value: QRectF(200, 150, 200, 150));
179 state2->assignProperty(object: box, name: "geometry", value: QRect(9, 150, 190, 150));
180 state2->assignProperty(object: p1, name: "pos", value: QPointF(68 - 150, 185 + 150));
181 state2->assignProperty(object: p2, name: "pos", value: QPointF(168, 185 + 150));
182 state2->assignProperty(object: p3, name: "pos", value: QPointF(268 + 150, 185 + 150));
183 state2->assignProperty(object: p4, name: "pos", value: QPointF(64, 48));
184 state2->assignProperty(object: p5, name: "pos", value: QPointF(168, 48));
185 state2->assignProperty(object: p6, name: "pos", value: QPointF(268, 48));
186 state2->assignProperty(object: p1, name: "rotation", value: qreal(-270));
187 state2->assignProperty(object: p2, name: "rotation", value: qreal(90));
188 state2->assignProperty(object: p3, name: "rotation", value: qreal(270));
189 state2->assignProperty(object: p4, name: "rotation", value: qreal(0));
190 state2->assignProperty(object: p5, name: "rotation", value: qreal(0));
191 state2->assignProperty(object: p6, name: "rotation", value: qreal(0));
192 state2->assignProperty(object: boxProxy, name: "opacity", value: qreal(1));
193 state2->assignProperty(object: p1, name: "opacity", value: qreal(0));
194 state2->assignProperty(object: p2, name: "opacity", value: qreal(0));
195 state2->assignProperty(object: p3, name: "opacity", value: qreal(0));
196 state2->assignProperty(object: p4, name: "opacity", value: qreal(1));
197 state2->assignProperty(object: p5, name: "opacity", value: qreal(1));
198 state2->assignProperty(object: p6, name: "opacity", value: qreal(1));
199
200 // State 3
201 state3->assignProperty(object: button, name: "text", value: "Switch to state 1");
202 state3->assignProperty(object: p1, name: "pos", value: QPointF(0, 5));
203 state3->assignProperty(object: p2, name: "pos", value: QPointF(0, 5 + 64 + 5));
204 state3->assignProperty(object: p3, name: "pos", value: QPointF(5, 5 + (64 + 5) + 64));
205 state3->assignProperty(object: p4, name: "pos", value: QPointF(5 + 64 + 5, 5));
206 state3->assignProperty(object: p5, name: "pos", value: QPointF(5 + 64 + 5, 5 + 64 + 5));
207 state3->assignProperty(object: p6, name: "pos", value: QPointF(5 + 64 + 5, 5 + (64 + 5) + 64));
208 state3->assignProperty(object: widget, name: "geometry", value: QRectF(138, 5, 400 - 138, 200));
209 state3->assignProperty(object: box, name: "geometry", value: QRect(5, 205, 400, 90));
210 state3->assignProperty(object: p1, name: "opacity", value: qreal(1));
211 state3->assignProperty(object: p2, name: "opacity", value: qreal(1));
212 state3->assignProperty(object: p3, name: "opacity", value: qreal(1));
213 state3->assignProperty(object: p4, name: "opacity", value: qreal(1));
214 state3->assignProperty(object: p5, name: "opacity", value: qreal(1));
215 state3->assignProperty(object: p6, name: "opacity", value: qreal(1));
216
217 QAbstractTransition *t1 = state1->addTransition(obj: button, signal: &QAbstractButton::clicked, target: state2);
218 QSequentialAnimationGroup *animation1SubGroup = new QSequentialAnimationGroup;
219 animation1SubGroup->addPause(msecs: 250);
220 animation1SubGroup->addAnimation(animation: new QPropertyAnimation(box, "geometry"));
221 t1->addAnimation(animation: animation1SubGroup);
222 t1->addAnimation(animation: new QPropertyAnimation(widget, "geometry"));
223 t1->addAnimation(animation: new QPropertyAnimation(p1, "pos"));
224 t1->addAnimation(animation: new QPropertyAnimation(p2, "pos"));
225 t1->addAnimation(animation: new QPropertyAnimation(p3, "pos"));
226 t1->addAnimation(animation: new QPropertyAnimation(p4, "pos"));
227 t1->addAnimation(animation: new QPropertyAnimation(p5, "pos"));
228 t1->addAnimation(animation: new QPropertyAnimation(p6, "pos"));
229 t1->addAnimation(animation: new QPropertyAnimation(p1, "rotation"));
230 t1->addAnimation(animation: new QPropertyAnimation(p2, "rotation"));
231 t1->addAnimation(animation: new QPropertyAnimation(p3, "rotation"));
232 t1->addAnimation(animation: new QPropertyAnimation(p4, "rotation"));
233 t1->addAnimation(animation: new QPropertyAnimation(p5, "rotation"));
234 t1->addAnimation(animation: new QPropertyAnimation(p6, "rotation"));
235 t1->addAnimation(animation: new QPropertyAnimation(p1, "opacity"));
236 t1->addAnimation(animation: new QPropertyAnimation(p2, "opacity"));
237 t1->addAnimation(animation: new QPropertyAnimation(p3, "opacity"));
238 t1->addAnimation(animation: new QPropertyAnimation(p4, "opacity"));
239 t1->addAnimation(animation: new QPropertyAnimation(p5, "opacity"));
240 t1->addAnimation(animation: new QPropertyAnimation(p6, "opacity"));
241
242 QAbstractTransition *t2 = state2->addTransition(obj: button, signal: &QAbstractButton::clicked, target: state3);
243 t2->addAnimation(animation: new QPropertyAnimation(box, "geometry"));
244 t2->addAnimation(animation: new QPropertyAnimation(widget, "geometry"));
245 t2->addAnimation(animation: new QPropertyAnimation(p1, "pos"));
246 t2->addAnimation(animation: new QPropertyAnimation(p2, "pos"));
247 t2->addAnimation(animation: new QPropertyAnimation(p3, "pos"));
248 t2->addAnimation(animation: new QPropertyAnimation(p4, "pos"));
249 t2->addAnimation(animation: new QPropertyAnimation(p5, "pos"));
250 t2->addAnimation(animation: new QPropertyAnimation(p6, "pos"));
251 t2->addAnimation(animation: new QPropertyAnimation(p1, "rotation"));
252 t2->addAnimation(animation: new QPropertyAnimation(p2, "rotation"));
253 t2->addAnimation(animation: new QPropertyAnimation(p3, "rotation"));
254 t2->addAnimation(animation: new QPropertyAnimation(p4, "rotation"));
255 t2->addAnimation(animation: new QPropertyAnimation(p5, "rotation"));
256 t2->addAnimation(animation: new QPropertyAnimation(p6, "rotation"));
257 t2->addAnimation(animation: new QPropertyAnimation(p1, "opacity"));
258 t2->addAnimation(animation: new QPropertyAnimation(p2, "opacity"));
259 t2->addAnimation(animation: new QPropertyAnimation(p3, "opacity"));
260 t2->addAnimation(animation: new QPropertyAnimation(p4, "opacity"));
261 t2->addAnimation(animation: new QPropertyAnimation(p5, "opacity"));
262 t2->addAnimation(animation: new QPropertyAnimation(p6, "opacity"));
263
264 QAbstractTransition *t3 = state3->addTransition(obj: button, signal: &QAbstractButton::clicked, target: state1);
265 t3->addAnimation(animation: new QPropertyAnimation(box, "geometry"));
266 t3->addAnimation(animation: new QPropertyAnimation(widget, "geometry"));
267 t3->addAnimation(animation: new QPropertyAnimation(p1, "pos"));
268 t3->addAnimation(animation: new QPropertyAnimation(p2, "pos"));
269 t3->addAnimation(animation: new QPropertyAnimation(p3, "pos"));
270 t3->addAnimation(animation: new QPropertyAnimation(p4, "pos"));
271 t3->addAnimation(animation: new QPropertyAnimation(p5, "pos"));
272 t3->addAnimation(animation: new QPropertyAnimation(p6, "pos"));
273 t3->addAnimation(animation: new QPropertyAnimation(p1, "rotation"));
274 t3->addAnimation(animation: new QPropertyAnimation(p2, "rotation"));
275 t3->addAnimation(animation: new QPropertyAnimation(p3, "rotation"));
276 t3->addAnimation(animation: new QPropertyAnimation(p4, "rotation"));
277 t3->addAnimation(animation: new QPropertyAnimation(p5, "rotation"));
278 t3->addAnimation(animation: new QPropertyAnimation(p6, "rotation"));
279 t3->addAnimation(animation: new QPropertyAnimation(p1, "opacity"));
280 t3->addAnimation(animation: new QPropertyAnimation(p2, "opacity"));
281 t3->addAnimation(animation: new QPropertyAnimation(p3, "opacity"));
282 t3->addAnimation(animation: new QPropertyAnimation(p4, "opacity"));
283 t3->addAnimation(animation: new QPropertyAnimation(p5, "opacity"));
284 t3->addAnimation(animation: new QPropertyAnimation(p6, "opacity"));
285
286 machine.start();
287
288 GraphicsView view(&scene);
289
290 view.show();
291
292 return app.exec();
293}
294
295#include "main.moc"
296

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