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/QtWidgets>
52#include <QtCore/qmath.h>
53#include <QtCore/qrandom.h>
54#include <QtCore/qstate.h>
55
56class Pixmap : public QObject, public QGraphicsPixmapItem
57{
58 Q_OBJECT
59 Q_PROPERTY(QPointF pos READ pos WRITE setPos)
60public:
61 Pixmap(const QPixmap &pix)
62 : QObject(), QGraphicsPixmapItem(pix)
63 {
64 setCacheMode(mode: DeviceCoordinateCache);
65 }
66};
67
68class Button : public QGraphicsWidget
69{
70 Q_OBJECT
71public:
72 Button(const QPixmap &pixmap, QGraphicsItem *parent = nullptr)
73 : QGraphicsWidget(parent), _pix(pixmap)
74 {
75 setAcceptHoverEvents(true);
76 setCacheMode(mode: DeviceCoordinateCache);
77 }
78
79 QRectF boundingRect() const override
80 {
81 return QRectF(-65, -65, 130, 130);
82 }
83
84 QPainterPath shape() const override
85 {
86 QPainterPath path;
87 path.addEllipse(rect: boundingRect());
88 return path;
89 }
90
91 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) override
92 {
93 bool down = option->state & QStyle::State_Sunken;
94 QRectF r = boundingRect();
95 QLinearGradient grad(r.topLeft(), r.bottomRight());
96 grad.setColorAt(pos: down ? 1 : 0, color: option->state & QStyle::State_MouseOver ? Qt::white : Qt::lightGray);
97 grad.setColorAt(pos: down ? 0 : 1, color: Qt::darkGray);
98 painter->setPen(Qt::darkGray);
99 painter->setBrush(grad);
100 painter->drawEllipse(r);
101 QLinearGradient grad2(r.topLeft(), r.bottomRight());
102 grad.setColorAt(pos: down ? 1 : 0, color: Qt::darkGray);
103 grad.setColorAt(pos: down ? 0 : 1, color: Qt::lightGray);
104 painter->setPen(Qt::NoPen);
105 painter->setBrush(grad);
106 if (down)
107 painter->translate(dx: 2, dy: 2);
108 painter->drawEllipse(r: r.adjusted(xp1: 5, yp1: 5, xp2: -5, yp2: -5));
109 painter->drawPixmap(x: -_pix.width()/2, y: -_pix.height()/2, pm: _pix);
110 }
111
112signals:
113 void pressed();
114
115protected:
116 void mousePressEvent(QGraphicsSceneMouseEvent *) override
117 {
118 emit pressed();
119 update();
120 }
121
122 void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override
123 {
124 update();
125 }
126
127private:
128 QPixmap _pix;
129};
130
131class View : public QGraphicsView
132{
133public:
134 View(QGraphicsScene *scene) : QGraphicsView(scene) { }
135
136protected:
137 void resizeEvent(QResizeEvent *event) override
138 {
139 QGraphicsView::resizeEvent(event);
140 fitInView(rect: sceneRect(), aspectRadioMode: Qt::KeepAspectRatio);
141 }
142};
143
144int main(int argc, char **argv)
145{
146 Q_INIT_RESOURCE(animatedtiles);
147
148 QApplication app(argc, argv);
149
150 QPixmap kineticPix(":/images/kinetic.png");
151 QPixmap bgPix(":/images/Time-For-Lunch-2.jpg");
152
153 QGraphicsScene scene(-350, -350, 700, 700);
154
155 QList<Pixmap *> items;
156 for (int i = 0; i < 64; ++i) {
157 Pixmap *item = new Pixmap(kineticPix);
158 item->setOffset(ax: -kineticPix.width()/2, ay: -kineticPix.height()/2);
159 item->setZValue(i);
160 items << item;
161 scene.addItem(item);
162 }
163
164 // Buttons
165 QGraphicsItem *buttonParent = new QGraphicsRectItem;
166 Button *ellipseButton = new Button(QPixmap(":/images/ellipse.png"), buttonParent);
167 Button *figure8Button = new Button(QPixmap(":/images/figure8.png"), buttonParent);
168 Button *randomButton = new Button(QPixmap(":/images/random.png"), buttonParent);
169 Button *tiledButton = new Button(QPixmap(":/images/tile.png"), buttonParent);
170 Button *centeredButton = new Button(QPixmap(":/images/centered.png"), buttonParent);
171
172 ellipseButton->setPos(ax: -100, ay: -100);
173 figure8Button->setPos(ax: 100, ay: -100);
174 randomButton->setPos(ax: 0, ay: 0);
175 tiledButton->setPos(ax: -100, ay: 100);
176 centeredButton->setPos(ax: 100, ay: 100);
177
178 scene.addItem(item: buttonParent);
179 buttonParent->setTransform(matrix: QTransform::fromScale(dx: 0.75, dy: 0.75), combine: true);
180 buttonParent->setPos(ax: 200, ay: 200);
181 buttonParent->setZValue(65);
182
183 // States
184 QState *rootState = new QState;
185 QState *ellipseState = new QState(rootState);
186 QState *figure8State = new QState(rootState);
187 QState *randomState = new QState(rootState);
188 QState *tiledState = new QState(rootState);
189 QState *centeredState = new QState(rootState);
190
191 // Values
192 for (int i = 0; i < items.count(); ++i) {
193 Pixmap *item = items.at(i);
194 // Ellipse
195 ellipseState->assignProperty(object: item, name: "pos",
196 value: QPointF(qCos(v: (i / 63.0) * 6.28) * 250,
197 qSin(v: (i / 63.0) * 6.28) * 250));
198
199 // Figure 8
200 figure8State->assignProperty(object: item, name: "pos",
201 value: QPointF(qSin(v: (i / 63.0) * 6.28) * 250,
202 qSin(v: ((i * 2)/63.0) * 6.28) * 250));
203
204 // Random
205 randomState->assignProperty(object: item, name: "pos",
206 value: QPointF(-250 + QRandomGenerator::global()->bounded(highest: 500),
207 -250 + QRandomGenerator::global()->bounded(highest: 500)));
208
209 // Tiled
210 tiledState->assignProperty(object: item, name: "pos",
211 value: QPointF(((i % 8) - 4) * kineticPix.width() + kineticPix.width() / 2,
212 ((i / 8) - 4) * kineticPix.height() + kineticPix.height() / 2));
213
214 // Centered
215 centeredState->assignProperty(object: item, name: "pos", value: QPointF());
216 }
217
218 // Ui
219 View *view = new View(&scene);
220 view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Animated Tiles"));
221 view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
222 view->setBackgroundBrush(bgPix);
223 view->setCacheMode(QGraphicsView::CacheBackground);
224 view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
225 view->show();
226
227 QStateMachine states;
228 states.addState(state: rootState);
229 states.setInitialState(rootState);
230 rootState->setInitialState(centeredState);
231
232 QParallelAnimationGroup *group = new QParallelAnimationGroup;
233 for (int i = 0; i < items.count(); ++i) {
234 QPropertyAnimation *anim = new QPropertyAnimation(items[i], "pos");
235 anim->setDuration(750 + i * 25);
236 anim->setEasingCurve(QEasingCurve::InOutBack);
237 group->addAnimation(animation: anim);
238 }
239 QAbstractTransition *trans = rootState->addTransition(obj: ellipseButton, signal: &Button::pressed, target: ellipseState);
240 trans->addAnimation(animation: group);
241
242 trans = rootState->addTransition(obj: figure8Button, signal: &Button::pressed, target: figure8State);
243 trans->addAnimation(animation: group);
244
245 trans = rootState->addTransition(obj: randomButton, signal: &Button::pressed, target: randomState);
246 trans->addAnimation(animation: group);
247
248 trans = rootState->addTransition(obj: tiledButton, signal: &Button::pressed, target: tiledState);
249 trans->addAnimation(animation: group);
250
251 trans = rootState->addTransition(obj: centeredButton, signal: &Button::pressed, target: centeredState);
252 trans->addAnimation(animation: group);
253
254 QTimer timer;
255 timer.start(msec: 125);
256 timer.setSingleShot(true);
257 trans = rootState->addTransition(obj: &timer, signal: &QTimer::timeout, target: ellipseState);
258 trans->addAnimation(animation: group);
259
260 states.start();
261
262#ifdef QT_KEYPAD_NAVIGATION
263 QApplication::setNavigationMode(Qt::NavigationModeCursorAuto);
264#endif
265 return app.exec();
266}
267
268#include "main.moc"
269

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