|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include <QtCore/qstate.h> |
|
45 |
|
46 #include "mainwindow.h" |
|
47 |
|
48 MainWindow::MainWindow(QWidget *parent) |
|
49 : QWidget(parent) |
|
50 { |
|
51 QPixmap kineticPix(":/images/kinetic.png"); |
|
52 QPixmap bgPix(":/images/Time-For-Lunch-2.jpg"); |
|
53 |
|
54 QGraphicsScene scene(-350, -350, 700, 700); |
|
55 |
|
56 for (int i = 0; i < 64; ++i) { |
|
57 Pixmap *item = new Pixmap(kineticPix); |
|
58 item->setOffset(-kineticPix.width()/2, -kineticPix.height()/2); |
|
59 item->setZValue(i); |
|
60 items << item; |
|
61 scene.addItem(item); |
|
62 } |
|
63 |
|
64 // Buttons |
|
65 QGraphicsItem *buttonParent = new QGraphicsRectItem; |
|
66 ellipseButton = new Button(QPixmap(":/images/ellipse.png"), buttonParent); |
|
67 figure8Button = new Button(QPixmap(":/images/figure8.png"), buttonParent); |
|
68 randomButton = new Button(QPixmap(":/images/random.png"), buttonParent); |
|
69 tiledButton = new Button(QPixmap(":/images/tile.png"), buttonParent); |
|
70 centeredButton = new Button(QPixmap(":/images/centered.png"), buttonParent); |
|
71 |
|
72 ellipseButton->setPos(-100, -100); |
|
73 figure8Button->setPos(100, -100); |
|
74 randomButton->setPos(0, 0); |
|
75 tiledButton->setPos(-100, 100); |
|
76 centeredButton->setPos(100, 100); |
|
77 |
|
78 scene.addItem(buttonParent); |
|
79 buttonParent->scale(0.75, 0.75); |
|
80 buttonParent->setPos(200, 200); |
|
81 buttonParent->setZValue(65); |
|
82 |
|
83 qDebug() << "qtp_animatedtiles: states next"; |
|
84 // States |
|
85 QState *rootState = new QState; |
|
86 QState *ellipseState = new QState(rootState); |
|
87 QState *figure8State = new QState(rootState); |
|
88 QState *randomState = new QState(rootState); |
|
89 QState *tiledState = new QState(rootState); |
|
90 QState *centeredState = new QState(rootState); |
|
91 |
|
92 qDebug() << "qtp_animatedtiles: values next"; |
|
93 // Values |
|
94 for (int i = 0; i < items.count(); ++i) { |
|
95 Pixmap *item = items.at(i); |
|
96 // Ellipse |
|
97 ellipseState->assignProperty(item, "pos", |
|
98 QPointF(cos((i / 63.0) * 6.28) * 250, |
|
99 sin((i / 63.0) * 6.28) * 250)); |
|
100 |
|
101 // Figure 8 |
|
102 figure8State->assignProperty(item, "pos", |
|
103 QPointF(sin((i / 63.0) * 6.28) * 250, |
|
104 sin(((i * 2)/63.0) * 6.28) * 250)); |
|
105 |
|
106 // Random |
|
107 randomState->assignProperty(item, "pos", |
|
108 QPointF(-250 + qrand() % 500, |
|
109 -250 + qrand() % 500)); |
|
110 |
|
111 // Tiled |
|
112 tiledState->assignProperty(item, "pos", |
|
113 QPointF(((i % 8) - 4) * kineticPix.width() + kineticPix.width() / 2, |
|
114 ((i / 8) - 4) * kineticPix.height() + kineticPix.height() / 2)); |
|
115 |
|
116 // Centered |
|
117 centeredState->assignProperty(item, "pos", QPointF()); |
|
118 } |
|
119 |
|
120 qDebug() << "qtp_animatedtiles: ui next"; |
|
121 // Ui |
|
122 View *view = new View(&scene); |
|
123 view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Animated Tiles")); |
|
124 view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); |
|
125 view->setBackgroundBrush(bgPix); |
|
126 view->setCacheMode(QGraphicsView::CacheBackground); |
|
127 view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
|
128 // view->show(); |
|
129 |
|
130 QStateMachine states; |
|
131 states.addState(rootState); |
|
132 states.setInitialState(rootState); |
|
133 rootState->setInitialState(centeredState); |
|
134 |
|
135 qDebug() << "qtp_animatedtiles: animgroup next"; |
|
136 QParallelAnimationGroup *group = new QParallelAnimationGroup; |
|
137 for (int i = 0; i < items.count(); ++i) { |
|
138 QPropertyAnimation *anim = new QPropertyAnimation(items[i], "pos"); |
|
139 anim->setDuration(750 + i * 25); |
|
140 anim->setEasingCurve(QEasingCurve::InOutBack); |
|
141 group->addAnimation(anim); |
|
142 } |
|
143 |
|
144 qDebug() << "qtp_animatedtiles: button signaling next"; |
|
145 QAbstractTransition *trans = rootState->addTransition(ellipseButton, SIGNAL(pressed()), ellipseState); |
|
146 trans->addAnimation(group); |
|
147 |
|
148 trans = rootState->addTransition(figure8Button, SIGNAL(pressed()), figure8State); |
|
149 trans->addAnimation(group); |
|
150 |
|
151 trans = rootState->addTransition(randomButton, SIGNAL(pressed()), randomState); |
|
152 trans->addAnimation(group); |
|
153 |
|
154 trans = rootState->addTransition(tiledButton, SIGNAL(pressed()), tiledState); |
|
155 trans->addAnimation(group); |
|
156 |
|
157 trans = rootState->addTransition(centeredButton, SIGNAL(pressed()), centeredState); |
|
158 trans->addAnimation(group); |
|
159 |
|
160 qDebug() << "qtp_animatedtiles: timer next"; |
|
161 QTimer timer; |
|
162 timer.start(125); |
|
163 timer.setSingleShot(true); |
|
164 trans = rootState->addTransition(&timer, SIGNAL(timeout()), ellipseState); |
|
165 trans->addAnimation(group); |
|
166 |
|
167 qDebug() << "qtp_animatedtiles: starting states"; |
|
168 states.start(); |
|
169 } |
|
170 |
|
171 void MainWindow::showPosition() |
|
172 { |
|
173 Pixmap *item = items.at(0); |
|
174 |
|
175 qDebug() << "qtp_animatedtiles: position of item[0] x: " << item->x() << " y: " << item->y(); |
|
176 } |
|
177 |