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