1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include <QtCore/qstate.h>
|
---|
43 |
|
---|
44 | class Pixmap : public QObject, public QGraphicsPixmapItem
|
---|
45 | {
|
---|
46 | Q_OBJECT
|
---|
47 | Q_PROPERTY(QPointF pos READ pos WRITE setPos)
|
---|
48 | public:
|
---|
49 | Pixmap(const QPixmap &pix)
|
---|
50 | : QObject(), QGraphicsPixmapItem(pix)
|
---|
51 | {
|
---|
52 | setCacheMode(DeviceCoordinateCache);
|
---|
53 | }
|
---|
54 | };
|
---|
55 |
|
---|
56 | class Button : public QGraphicsWidget
|
---|
57 | {
|
---|
58 | Q_OBJECT
|
---|
59 | public:
|
---|
60 | Button(const QPixmap &pixmap, QGraphicsItem *parent = 0)
|
---|
61 | : QGraphicsWidget(parent), _pix(pixmap)
|
---|
62 | {
|
---|
63 | setAcceptHoverEvents(true);
|
---|
64 | setCacheMode(DeviceCoordinateCache);
|
---|
65 | }
|
---|
66 |
|
---|
67 | QRectF boundingRect() const
|
---|
68 | {
|
---|
69 | return QRectF(-65, -65, 130, 130);
|
---|
70 | }
|
---|
71 |
|
---|
72 | QPainterPath shape() const
|
---|
73 | {
|
---|
74 | QPainterPath path;
|
---|
75 | path.addEllipse(boundingRect());
|
---|
76 | return path;
|
---|
77 | }
|
---|
78 |
|
---|
79 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
---|
80 | {
|
---|
81 | bool down = option->state & QStyle::State_Sunken;
|
---|
82 | QRectF r = boundingRect();
|
---|
83 | QLinearGradient grad(r.topLeft(), r.bottomRight());
|
---|
84 | grad.setColorAt(down ? 1 : 0, option->state & QStyle::State_MouseOver ? Qt::white : Qt::lightGray);
|
---|
85 | grad.setColorAt(down ? 0 : 1, Qt::darkGray);
|
---|
86 | painter->setPen(Qt::darkGray);
|
---|
87 | painter->setBrush(grad);
|
---|
88 | painter->drawEllipse(r);
|
---|
89 | QLinearGradient grad2(r.topLeft(), r.bottomRight());
|
---|
90 | grad.setColorAt(down ? 1 : 0, Qt::darkGray);
|
---|
91 | grad.setColorAt(down ? 0 : 1, Qt::lightGray);
|
---|
92 | painter->setPen(Qt::NoPen);
|
---|
93 | painter->setBrush(grad);
|
---|
94 | if (down)
|
---|
95 | painter->translate(2, 2);
|
---|
96 | painter->drawEllipse(r.adjusted(5, 5, -5, -5));
|
---|
97 | painter->drawPixmap(-_pix.width()/2, -_pix.height()/2, _pix);
|
---|
98 | }
|
---|
99 |
|
---|
100 | signals:
|
---|
101 | void pressed();
|
---|
102 |
|
---|
103 | protected:
|
---|
104 | void mousePressEvent(QGraphicsSceneMouseEvent *)
|
---|
105 | {
|
---|
106 | emit pressed();
|
---|
107 | update();
|
---|
108 | }
|
---|
109 |
|
---|
110 | void mouseReleaseEvent(QGraphicsSceneMouseEvent *)
|
---|
111 | {
|
---|
112 | update();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private:
|
---|
116 | QPixmap _pix;
|
---|
117 | };
|
---|
118 |
|
---|
119 | class View : public QGraphicsView
|
---|
120 | {
|
---|
121 | public:
|
---|
122 | View(QGraphicsScene *scene) : QGraphicsView(scene) { }
|
---|
123 |
|
---|
124 | protected:
|
---|
125 | void resizeEvent(QResizeEvent *event)
|
---|
126 | {
|
---|
127 | QGraphicsView::resizeEvent(event);
|
---|
128 | fitInView(sceneRect(), Qt::KeepAspectRatio);
|
---|
129 | }
|
---|
130 | };
|
---|
131 |
|
---|
132 | int main(int argc, char **argv)
|
---|
133 | {
|
---|
134 | Q_INIT_RESOURCE(animatedtiles);
|
---|
135 |
|
---|
136 | QApplication app(argc, argv);
|
---|
137 |
|
---|
138 | QPixmap kineticPix(":/images/kinetic.png");
|
---|
139 | QPixmap bgPix(":/images/Time-For-Lunch-2.jpg");
|
---|
140 |
|
---|
141 | QGraphicsScene scene(-350, -350, 700, 700);
|
---|
142 |
|
---|
143 | QList<Pixmap *> items;
|
---|
144 | for (int i = 0; i < 64; ++i) {
|
---|
145 | Pixmap *item = new Pixmap(kineticPix);
|
---|
146 | item->setOffset(-kineticPix.width()/2, -kineticPix.height()/2);
|
---|
147 | item->setZValue(i);
|
---|
148 | items << item;
|
---|
149 | scene.addItem(item);
|
---|
150 | }
|
---|
151 |
|
---|
152 | // Buttons
|
---|
153 | QGraphicsItem *buttonParent = new QGraphicsRectItem;
|
---|
154 | Button *ellipseButton = new Button(QPixmap(":/images/ellipse.png"), buttonParent);
|
---|
155 | Button *figure8Button = new Button(QPixmap(":/images/figure8.png"), buttonParent);
|
---|
156 | Button *randomButton = new Button(QPixmap(":/images/random.png"), buttonParent);
|
---|
157 | Button *tiledButton = new Button(QPixmap(":/images/tile.png"), buttonParent);
|
---|
158 | Button *centeredButton = new Button(QPixmap(":/images/centered.png"), buttonParent);
|
---|
159 |
|
---|
160 | ellipseButton->setPos(-100, -100);
|
---|
161 | figure8Button->setPos(100, -100);
|
---|
162 | randomButton->setPos(0, 0);
|
---|
163 | tiledButton->setPos(-100, 100);
|
---|
164 | centeredButton->setPos(100, 100);
|
---|
165 |
|
---|
166 | scene.addItem(buttonParent);
|
---|
167 | buttonParent->scale(0.75, 0.75);
|
---|
168 | buttonParent->setPos(200, 200);
|
---|
169 | buttonParent->setZValue(65);
|
---|
170 |
|
---|
171 | // States
|
---|
172 | QState *rootState = new QState;
|
---|
173 | QState *ellipseState = new QState(rootState);
|
---|
174 | QState *figure8State = new QState(rootState);
|
---|
175 | QState *randomState = new QState(rootState);
|
---|
176 | QState *tiledState = new QState(rootState);
|
---|
177 | QState *centeredState = new QState(rootState);
|
---|
178 |
|
---|
179 | // Values
|
---|
180 | for (int i = 0; i < items.count(); ++i) {
|
---|
181 | Pixmap *item = items.at(i);
|
---|
182 | // Ellipse
|
---|
183 | ellipseState->assignProperty(item, "pos",
|
---|
184 | QPointF(cos((i / 63.0) * 6.28) * 250,
|
---|
185 | sin((i / 63.0) * 6.28) * 250));
|
---|
186 |
|
---|
187 | // Figure 8
|
---|
188 | figure8State->assignProperty(item, "pos",
|
---|
189 | QPointF(sin((i / 63.0) * 6.28) * 250,
|
---|
190 | sin(((i * 2)/63.0) * 6.28) * 250));
|
---|
191 |
|
---|
192 | // Random
|
---|
193 | randomState->assignProperty(item, "pos",
|
---|
194 | QPointF(-250 + qrand() % 500,
|
---|
195 | -250 + qrand() % 500));
|
---|
196 |
|
---|
197 | // Tiled
|
---|
198 | tiledState->assignProperty(item, "pos",
|
---|
199 | QPointF(((i % 8) - 4) * kineticPix.width() + kineticPix.width() / 2,
|
---|
200 | ((i / 8) - 4) * kineticPix.height() + kineticPix.height() / 2));
|
---|
201 |
|
---|
202 | // Centered
|
---|
203 | centeredState->assignProperty(item, "pos", QPointF());
|
---|
204 | }
|
---|
205 |
|
---|
206 | // Ui
|
---|
207 | View *view = new View(&scene);
|
---|
208 | view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Animated Tiles"));
|
---|
209 | view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
|
---|
210 | view->setBackgroundBrush(bgPix);
|
---|
211 | view->setCacheMode(QGraphicsView::CacheBackground);
|
---|
212 | view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
|
---|
213 | view->show();
|
---|
214 |
|
---|
215 | QStateMachine states;
|
---|
216 | states.addState(rootState);
|
---|
217 | states.setInitialState(rootState);
|
---|
218 | rootState->setInitialState(centeredState);
|
---|
219 |
|
---|
220 | QParallelAnimationGroup *group = new QParallelAnimationGroup;
|
---|
221 | for (int i = 0; i < items.count(); ++i) {
|
---|
222 | QPropertyAnimation *anim = new QPropertyAnimation(items[i], "pos");
|
---|
223 | anim->setDuration(750 + i * 25);
|
---|
224 | anim->setEasingCurve(QEasingCurve::InOutBack);
|
---|
225 | group->addAnimation(anim);
|
---|
226 | }
|
---|
227 | QAbstractTransition *trans = rootState->addTransition(ellipseButton, SIGNAL(pressed()), ellipseState);
|
---|
228 | trans->addAnimation(group);
|
---|
229 |
|
---|
230 | trans = rootState->addTransition(figure8Button, SIGNAL(pressed()), figure8State);
|
---|
231 | trans->addAnimation(group);
|
---|
232 |
|
---|
233 | trans = rootState->addTransition(randomButton, SIGNAL(pressed()), randomState);
|
---|
234 | trans->addAnimation(group);
|
---|
235 |
|
---|
236 | trans = rootState->addTransition(tiledButton, SIGNAL(pressed()), tiledState);
|
---|
237 | trans->addAnimation(group);
|
---|
238 |
|
---|
239 | trans = rootState->addTransition(centeredButton, SIGNAL(pressed()), centeredState);
|
---|
240 | trans->addAnimation(group);
|
---|
241 |
|
---|
242 | QTimer timer;
|
---|
243 | timer.start(125);
|
---|
244 | timer.setSingleShot(true);
|
---|
245 | trans = rootState->addTransition(&timer, SIGNAL(timeout()), ellipseState);
|
---|
246 | trans->addAnimation(group);
|
---|
247 |
|
---|
248 | states.start();
|
---|
249 |
|
---|
250 | #ifdef QT_KEYPAD_NAVIGATION
|
---|
251 | QApplication::setNavigationMode(Qt::NavigationModeCursorAuto);
|
---|
252 | #endif
|
---|
253 | return app.exec();
|
---|
254 | }
|
---|
255 |
|
---|
256 | #include "main.moc"
|
---|