4 ** All rights reserved. |
4 ** All rights reserved. |
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
6 ** |
6 ** |
7 ** This file is part of the examples of the Qt Toolkit. |
7 ** This file is part of the examples of the Qt Toolkit. |
8 ** |
8 ** |
9 ** $QT_BEGIN_LICENSE:LGPL$ |
9 ** $QT_BEGIN_LICENSE:BSD$ |
10 ** No Commercial Usage |
10 ** You may use this file under the terms of the BSD license as follows: |
11 ** This file contains pre-release code and may not be distributed. |
11 ** |
12 ** You may use this file in accordance with the terms and conditions |
12 ** "Redistribution and use in source and binary forms, with or without |
13 ** contained in the Technology Preview License Agreement accompanying |
13 ** modification, are permitted provided that the following conditions are |
14 ** this package. |
14 ** met: |
15 ** |
15 ** * Redistributions of source code must retain the above copyright |
16 ** GNU Lesser General Public License Usage |
16 ** notice, this list of conditions and the following disclaimer. |
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
17 ** * Redistributions in binary form must reproduce the above copyright |
18 ** General Public License version 2.1 as published by the Free Software |
18 ** notice, this list of conditions and the following disclaimer in |
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
19 ** the documentation and/or other materials provided with the |
20 ** packaging of this file. Please review the following information to |
20 ** distribution. |
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
22 ** the names of its contributors may be used to endorse or promote |
23 ** |
23 ** products derived from this software without specific prior written |
24 ** In addition, as a special exception, Nokia gives you certain additional |
24 ** permission. |
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
25 ** |
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
27 ** |
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
28 ** If you have questions regarding the use of this file, please contact |
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
29 ** Nokia at qt-info@nokia.com. |
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
30 ** |
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
31 ** |
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
32 ** |
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
33 ** |
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
34 ** |
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
35 ** |
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
36 ** |
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
37 ** |
|
38 ** $QT_END_LICENSE$ |
37 ** $QT_END_LICENSE$ |
39 ** |
38 ** |
40 ****************************************************************************/ |
39 ****************************************************************************/ |
41 |
40 |
42 #include <QtGui> |
41 #include <QtGui> |
43 |
42 |
44 #include "robot.h" |
43 #include "robot.h" |
45 |
44 |
|
45 //! [0] |
46 RobotPart::RobotPart(QGraphicsItem *parent) |
46 RobotPart::RobotPart(QGraphicsItem *parent) |
47 : QGraphicsItem(parent), color(Qt::lightGray), dragOver(false) |
47 : QGraphicsObject(parent), color(Qt::lightGray), dragOver(false) |
48 { |
48 { |
49 setAcceptDrops(true); |
49 setAcceptDrops(true); |
50 } |
50 } |
51 |
51 //! [0] |
|
52 |
|
53 //! [1] |
52 void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event) |
54 void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event) |
53 { |
55 { |
54 if (event->mimeData()->hasColor() |
56 if (event->mimeData()->hasColor()) { |
55 || (qgraphicsitem_cast<RobotHead *>(this) && event->mimeData()->hasImage())) { |
|
56 event->setAccepted(true); |
57 event->setAccepted(true); |
57 dragOver = true; |
58 dragOver = true; |
58 update(); |
59 update(); |
59 } else { |
60 } else { |
60 event->setAccepted(false); |
61 event->setAccepted(false); |
61 } |
62 } |
62 } |
63 } |
63 |
64 //! [1] |
|
65 |
|
66 //! [2] |
64 void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) |
67 void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) |
65 { |
68 { |
66 Q_UNUSED(event); |
69 Q_UNUSED(event); |
67 dragOver = false; |
70 dragOver = false; |
68 update(); |
71 update(); |
69 } |
72 } |
70 |
73 //! [2] |
|
74 |
|
75 //! [3] |
71 void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event) |
76 void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event) |
72 { |
77 { |
73 dragOver = false; |
78 dragOver = false; |
74 if (event->mimeData()->hasColor()) |
79 if (event->mimeData()->hasColor()) |
75 color = qVariantValue<QColor>(event->mimeData()->colorData()); |
80 color = qVariantValue<QColor>(event->mimeData()->colorData()); |
76 else if (event->mimeData()->hasImage()) |
|
77 pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData()); |
|
78 update(); |
81 update(); |
79 } |
82 } |
80 |
83 //! [3] |
|
84 |
|
85 //! [4] |
81 RobotHead::RobotHead(QGraphicsItem *parent) |
86 RobotHead::RobotHead(QGraphicsItem *parent) |
82 : RobotPart(parent) |
87 : RobotPart(parent) |
83 { |
88 { |
84 } |
89 } |
85 |
90 //! [4] |
|
91 |
|
92 //! [5] |
86 QRectF RobotHead::boundingRect() const |
93 QRectF RobotHead::boundingRect() const |
87 { |
94 { |
88 return QRectF(-15, -50, 30, 50); |
95 return QRectF(-15, -50, 30, 50); |
89 } |
96 } |
90 |
97 //! [5] |
|
98 |
|
99 //! [6] |
91 void RobotHead::paint(QPainter *painter, |
100 void RobotHead::paint(QPainter *painter, |
92 const QStyleOptionGraphicsItem *option, QWidget *widget) |
101 const QStyleOptionGraphicsItem *option, QWidget *widget) |
93 { |
102 { |
94 Q_UNUSED(option); |
103 Q_UNUSED(option); |
95 Q_UNUSED(widget); |
104 Q_UNUSED(widget); |
159 painter->setBrush(dragOver ? color.light(130) : color); |
190 painter->setBrush(dragOver ? color.light(130) : color); |
160 painter->drawRoundedRect(boundingRect(), 50, 50, Qt::RelativeSize); |
191 painter->drawRoundedRect(boundingRect(), 50, 50, Qt::RelativeSize); |
161 painter->drawEllipse(-5, -5, 10, 10); |
192 painter->drawEllipse(-5, -5, 10, 10); |
162 } |
193 } |
163 |
194 |
164 Robot::Robot() |
195 //! [10] |
165 { |
196 Robot::Robot(QGraphicsItem *parent) |
166 QGraphicsItem *torsoItem = new RobotTorso(this); |
197 : RobotPart(parent) |
167 QGraphicsItem *headItem = new RobotHead(torsoItem); |
198 { |
168 QGraphicsItem *upperLeftArmItem = new RobotLimb(torsoItem); |
199 setFlag(ItemHasNoContents); |
169 QGraphicsItem *lowerLeftArmItem = new RobotLimb(upperLeftArmItem); |
200 |
170 QGraphicsItem *upperRightArmItem = new RobotLimb(torsoItem); |
201 QGraphicsObject *torsoItem = new RobotTorso(this); |
171 QGraphicsItem *lowerRightArmItem = new RobotLimb(upperRightArmItem); |
202 QGraphicsObject *headItem = new RobotHead(torsoItem); |
172 QGraphicsItem *upperRightLegItem = new RobotLimb(torsoItem); |
203 QGraphicsObject *upperLeftArmItem = new RobotLimb(torsoItem); |
173 QGraphicsItem *lowerRightLegItem = new RobotLimb(upperRightLegItem); |
204 QGraphicsObject *lowerLeftArmItem = new RobotLimb(upperLeftArmItem); |
174 QGraphicsItem *upperLeftLegItem = new RobotLimb(torsoItem); |
205 QGraphicsObject *upperRightArmItem = new RobotLimb(torsoItem); |
175 QGraphicsItem *lowerLeftLegItem = new RobotLimb(upperLeftLegItem); |
206 QGraphicsObject *lowerRightArmItem = new RobotLimb(upperRightArmItem); |
176 |
207 QGraphicsObject *upperRightLegItem = new RobotLimb(torsoItem); |
|
208 QGraphicsObject *lowerRightLegItem = new RobotLimb(upperRightLegItem); |
|
209 QGraphicsObject *upperLeftLegItem = new RobotLimb(torsoItem); |
|
210 QGraphicsObject *lowerLeftLegItem = new RobotLimb(upperLeftLegItem); |
|
211 //! [10] |
|
212 |
|
213 //! [11] |
177 headItem->setPos(0, -18); |
214 headItem->setPos(0, -18); |
178 upperLeftArmItem->setPos(-15, -10); |
215 upperLeftArmItem->setPos(-15, -10); |
179 lowerLeftArmItem->setPos(30, 0); |
216 lowerLeftArmItem->setPos(30, 0); |
180 upperRightArmItem->setPos(15, -10); |
217 upperRightArmItem->setPos(15, -10); |
181 lowerRightArmItem->setPos(30, 0); |
218 lowerRightArmItem->setPos(30, 0); |
182 upperRightLegItem->setPos(10, 32); |
219 upperRightLegItem->setPos(10, 32); |
183 lowerRightLegItem->setPos(30, 0); |
220 lowerRightLegItem->setPos(30, 0); |
184 upperLeftLegItem->setPos(-10, 32); |
221 upperLeftLegItem->setPos(-10, 32); |
185 lowerLeftLegItem->setPos(30, 0); |
222 lowerLeftLegItem->setPos(30, 0); |
186 |
223 //! [11] |
187 timeLine = new QTimeLine; |
224 |
188 |
225 //! [12] |
189 QGraphicsItemAnimation *headAnimation = new QGraphicsItemAnimation; |
226 QParallelAnimationGroup *animation = new QParallelAnimationGroup(this); |
190 headAnimation->setItem(headItem); |
227 |
191 headAnimation->setTimeLine(timeLine); |
228 QPropertyAnimation *headAnimation = new QPropertyAnimation(headItem, "rotation"); |
192 headAnimation->setRotationAt(0, 20); |
229 headAnimation->setStartValue(20); |
193 headAnimation->setRotationAt(1, -20); |
230 headAnimation->setEndValue(-20); |
194 headAnimation->setScaleAt(1, 1.1, 1.1); |
231 QPropertyAnimation *headScaleAnimation = new QPropertyAnimation(headItem, "scale"); |
195 |
232 headScaleAnimation->setEndValue(1.1); |
196 QGraphicsItemAnimation *upperLeftArmAnimation = new QGraphicsItemAnimation; |
233 animation->addAnimation(headAnimation); |
197 upperLeftArmAnimation->setItem(upperLeftArmItem); |
234 animation->addAnimation(headScaleAnimation); |
198 upperLeftArmAnimation->setTimeLine(timeLine); |
235 //! [12] |
199 upperLeftArmAnimation->setRotationAt(0, 190); |
236 |
200 upperLeftArmAnimation->setRotationAt(1, 180); |
237 QPropertyAnimation *upperLeftArmAnimation = new QPropertyAnimation(upperLeftArmItem, "rotation"); |
201 |
238 upperLeftArmAnimation->setStartValue(190); |
202 QGraphicsItemAnimation *lowerLeftArmAnimation = new QGraphicsItemAnimation; |
239 upperLeftArmAnimation->setEndValue(180); |
203 lowerLeftArmAnimation->setItem(lowerLeftArmItem); |
240 animation->addAnimation(upperLeftArmAnimation); |
204 lowerLeftArmAnimation->setTimeLine(timeLine); |
241 |
205 lowerLeftArmAnimation->setRotationAt(0, 50); |
242 QPropertyAnimation *lowerLeftArmAnimation = new QPropertyAnimation(lowerLeftArmItem, "rotation"); |
206 lowerLeftArmAnimation->setRotationAt(1, 10); |
243 lowerLeftArmAnimation->setStartValue(50); |
207 |
244 lowerLeftArmAnimation->setEndValue(10); |
208 QGraphicsItemAnimation *upperRightArmAnimation = new QGraphicsItemAnimation; |
245 animation->addAnimation(lowerLeftArmAnimation); |
209 upperRightArmAnimation->setItem(upperRightArmItem); |
246 |
210 upperRightArmAnimation->setTimeLine(timeLine); |
247 QPropertyAnimation *upperRightArmAnimation = new QPropertyAnimation(upperRightArmItem, "rotation"); |
211 upperRightArmAnimation->setRotationAt(0, 300); |
248 upperRightArmAnimation->setStartValue(300); |
212 upperRightArmAnimation->setRotationAt(1, 310); |
249 upperRightArmAnimation->setEndValue(310); |
213 |
250 animation->addAnimation(upperRightArmAnimation); |
214 QGraphicsItemAnimation *lowerRightArmAnimation = new QGraphicsItemAnimation; |
251 |
215 lowerRightArmAnimation->setItem(lowerRightArmItem); |
252 QPropertyAnimation *lowerRightArmAnimation = new QPropertyAnimation(lowerRightArmItem, "rotation"); |
216 lowerRightArmAnimation->setTimeLine(timeLine); |
253 lowerRightArmAnimation->setStartValue(0); |
217 lowerRightArmAnimation->setRotationAt(0, 0); |
254 lowerRightArmAnimation->setEndValue(-70); |
218 lowerRightArmAnimation->setRotationAt(1, -70); |
255 animation->addAnimation(lowerRightArmAnimation); |
219 |
256 |
220 QGraphicsItemAnimation *upperLeftLegAnimation = new QGraphicsItemAnimation; |
257 QPropertyAnimation *upperLeftLegAnimation = new QPropertyAnimation(upperLeftLegItem, "rotation"); |
221 upperLeftLegAnimation->setItem(upperLeftLegItem); |
258 upperLeftLegAnimation->setStartValue(150); |
222 upperLeftLegAnimation->setTimeLine(timeLine); |
259 upperLeftLegAnimation->setEndValue(80); |
223 upperLeftLegAnimation->setRotationAt(0, 150); |
260 animation->addAnimation(upperLeftLegAnimation); |
224 upperLeftLegAnimation->setRotationAt(1, 80); |
261 |
225 |
262 QPropertyAnimation *lowerLeftLegAnimation = new QPropertyAnimation(lowerLeftLegItem, "rotation"); |
226 QGraphicsItemAnimation *lowerLeftLegAnimation = new QGraphicsItemAnimation; |
263 lowerLeftLegAnimation->setStartValue(70); |
227 lowerLeftLegAnimation->setItem(lowerLeftLegItem); |
264 lowerLeftLegAnimation->setEndValue(10); |
228 lowerLeftLegAnimation->setTimeLine(timeLine); |
265 animation->addAnimation(lowerLeftLegAnimation); |
229 lowerLeftLegAnimation->setRotationAt(0, 70); |
266 |
230 lowerLeftLegAnimation->setRotationAt(1, 10); |
267 QPropertyAnimation *upperRightLegAnimation = new QPropertyAnimation(upperRightLegItem, "rotation"); |
231 |
268 upperRightLegAnimation->setStartValue(40); |
232 QGraphicsItemAnimation *upperRightLegAnimation = new QGraphicsItemAnimation; |
269 upperRightLegAnimation->setEndValue(120); |
233 upperRightLegAnimation->setItem(upperRightLegItem); |
270 animation->addAnimation(upperRightLegAnimation); |
234 upperRightLegAnimation->setTimeLine(timeLine); |
271 |
235 upperRightLegAnimation->setRotationAt(0, 40); |
272 QPropertyAnimation *lowerRightLegAnimation = new QPropertyAnimation(lowerRightLegItem, "rotation"); |
236 upperRightLegAnimation->setRotationAt(1, 120); |
273 lowerRightLegAnimation->setStartValue(10); |
237 |
274 lowerRightLegAnimation->setEndValue(50); |
238 QGraphicsItemAnimation *lowerRightLegAnimation = new QGraphicsItemAnimation; |
275 animation->addAnimation(lowerRightLegAnimation); |
239 lowerRightLegAnimation->setItem(lowerRightLegItem); |
276 |
240 lowerRightLegAnimation->setTimeLine(timeLine); |
277 QPropertyAnimation *torsoAnimation = new QPropertyAnimation(torsoItem, "rotation"); |
241 lowerRightLegAnimation->setRotationAt(0, 10); |
278 torsoAnimation->setStartValue(5); |
242 lowerRightLegAnimation->setRotationAt(1, 50); |
279 torsoAnimation->setEndValue(-20); |
243 |
280 animation->addAnimation(torsoAnimation); |
244 QGraphicsItemAnimation *torsoAnimation = new QGraphicsItemAnimation; |
281 |
245 torsoAnimation->setItem(torsoItem); |
282 //! [13] |
246 torsoAnimation->setTimeLine(timeLine); |
283 for (int i = 0; i < animation->animationCount(); ++i) { |
247 torsoAnimation->setRotationAt(0, 5); |
284 QPropertyAnimation *anim = qobject_cast<QPropertyAnimation *>(animation->animationAt(i)); |
248 torsoAnimation->setRotationAt(1, -20); |
285 anim->setEasingCurve(QEasingCurve::SineCurve); |
249 |
286 anim->setDuration(2000); |
250 timeLine->setUpdateInterval(1000 / 25); |
287 } |
251 timeLine->setCurveShape(QTimeLine::SineCurve); |
288 |
252 timeLine->setLoopCount(0); |
289 animation->setLoopCount(-1); |
253 timeLine->setDuration(2000); |
290 animation->start(); |
254 timeLine->start(); |
291 //! [13] |
255 } |
292 } |
256 |
293 |
257 Robot::~Robot() |
294 //! [9] |
258 { |
|
259 delete timeLine; |
|
260 } |
|
261 |
|
262 QRectF Robot::boundingRect() const |
295 QRectF Robot::boundingRect() const |
263 { |
296 { |
264 return QRectF(); |
297 return QRectF(); |
265 } |
298 } |
266 |
299 |