1
/****************************************************************************
3
** Copyright (C) 2006-2006 Trolltech ASA. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be used under the terms of the GNU General Public
8
** License version 2.0 as published by the Free Software Foundation
9
** and appearing in the file LICENSE.GPL included in the packaging of
10
** this file. Please review the following information to ensure GNU
11
** General Public Licensing requirements will be met:
12
** http://www.trolltech.com/products/qt/opensource.html
14
** If you are unsure which license is appropriate for your use, please
15
** review the following information:
16
** http://www.trolltech.com/products/qt/licensing.html or contact the
17
** sales department at sales@trolltech.com.
19
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22
****************************************************************************/
28
RobotPart::RobotPart(QGraphicsItem *parent)
29
: QGraphicsItem(parent), color(Qt::lightGray), dragOver(false)
34
void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
36
if (event->mimeData()->hasColor()
37
|| (qgraphicsitem_cast<RobotHead *>(this) && event->mimeData()->hasImage())) {
38
event->setAccepted(true);
42
event->setAccepted(false);
46
void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
53
void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event)
56
if (event->mimeData()->hasColor())
57
color = qVariantValue<QColor>(event->mimeData()->colorData());
58
else if (event->mimeData()->hasImage())
59
pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData());
63
RobotHead::RobotHead(QGraphicsItem *parent)
68
QRectF RobotHead::boundingRect() const
70
return QRectF(-15, -50, 30, 50);
73
void RobotHead::paint(QPainter *painter,
74
const QStyleOptionGraphicsItem *option, QWidget *widget)
78
if (pixmap.isNull()) {
79
painter->setBrush(dragOver ? color.light(130) : color);
80
painter->drawRoundRect(-10, -30, 20, 30);
81
painter->setBrush(Qt::white);
82
painter->drawEllipse(-7, -3 - 20, 7, 7);
83
painter->drawEllipse(0, -3 - 20, 7, 7);
84
painter->setBrush(Qt::black);
85
painter->drawEllipse(-5, -1 - 20, 2, 2);
86
painter->drawEllipse(2, -1 - 20, 2, 2);
87
painter->setPen(QPen(Qt::black, 2));
88
painter->setBrush(Qt::NoBrush);
89
painter->drawArc(-6, -2 - 20, 12, 15, 190 * 16, 160 * 16);
91
painter->scale(.2272, .2824);
92
painter->drawPixmap(QPointF(-15 * 4.4, -50 * 3.54), pixmap);
96
int RobotHead::type() const
101
RobotTorso::RobotTorso(QGraphicsItem *parent)
106
QRectF RobotTorso::boundingRect() const
108
return QRectF(-30, -20, 60, 60);
111
void RobotTorso::paint(QPainter *painter,
112
const QStyleOptionGraphicsItem *option, QWidget *widget)
117
painter->setBrush(dragOver ? color.light(130) : color);
118
painter->drawRoundRect(-20, -20, 40, 60);
119
painter->drawEllipse(-25, -20, 20, 20);
120
painter->drawEllipse(5, -20, 20, 20);
121
painter->drawEllipse(-20, 22, 20, 20);
122
painter->drawEllipse(0, 22, 20, 20);
125
RobotLimb::RobotLimb(QGraphicsItem *parent)
130
QRectF RobotLimb::boundingRect() const
132
return QRectF(-5, -5, 40, 10);
135
void RobotLimb::paint(QPainter *painter,
136
const QStyleOptionGraphicsItem *option, QWidget *widget)
141
painter->setBrush(dragOver ? color.light(130) : color);
142
painter->drawRoundRect(boundingRect(), 50, 50);
143
painter->drawEllipse(-5, -5, 10, 10);
148
QGraphicsItem *torsoItem = new RobotTorso(this);
149
QGraphicsItem *headItem = new RobotHead(torsoItem);
150
QGraphicsItem *upperLeftArmItem = new RobotLimb(torsoItem);
151
QGraphicsItem *lowerLeftArmItem = new RobotLimb(upperLeftArmItem);
152
QGraphicsItem *upperRightArmItem = new RobotLimb(torsoItem);
153
QGraphicsItem *lowerRightArmItem = new RobotLimb(upperRightArmItem);
154
QGraphicsItem *upperRightLegItem = new RobotLimb(torsoItem);
155
QGraphicsItem *lowerRightLegItem = new RobotLimb(upperRightLegItem);
156
QGraphicsItem *upperLeftLegItem = new RobotLimb(torsoItem);
157
QGraphicsItem *lowerLeftLegItem = new RobotLimb(upperLeftLegItem);
159
headItem->setPos(0, -18);
160
upperLeftArmItem->setPos(-15, -10);
161
lowerLeftArmItem->setPos(30, 0);
162
upperRightArmItem->setPos(15, -10);
163
lowerRightArmItem->setPos(30, 0);
164
upperRightLegItem->setPos(10, 32);
165
lowerRightLegItem->setPos(30, 0);
166
upperLeftLegItem->setPos(-10, 32);
167
lowerLeftLegItem->setPos(30, 0);
169
timeLine = new QTimeLine;
171
QGraphicsItemAnimation *headAnimation = new QGraphicsItemAnimation;
172
headAnimation->setItem(headItem);
173
headAnimation->setTimeLine(timeLine);
174
headAnimation->setRotationAt(0, 20);
175
headAnimation->setRotationAt(1, -20);
176
headAnimation->setScaleAt(1, 1.1, 1.1);
178
QGraphicsItemAnimation *upperLeftArmAnimation = new QGraphicsItemAnimation;
179
upperLeftArmAnimation->setItem(upperLeftArmItem);
180
upperLeftArmAnimation->setTimeLine(timeLine);
181
upperLeftArmAnimation->setRotationAt(0, 190);
182
upperLeftArmAnimation->setRotationAt(1, 180);
184
QGraphicsItemAnimation *lowerLeftArmAnimation = new QGraphicsItemAnimation;
185
lowerLeftArmAnimation->setItem(lowerLeftArmItem);
186
lowerLeftArmAnimation->setTimeLine(timeLine);
187
lowerLeftArmAnimation->setRotationAt(0, 50);
188
lowerLeftArmAnimation->setRotationAt(1, 10);
190
QGraphicsItemAnimation *upperRightArmAnimation = new QGraphicsItemAnimation;
191
upperRightArmAnimation->setItem(upperRightArmItem);
192
upperRightArmAnimation->setTimeLine(timeLine);
193
upperRightArmAnimation->setRotationAt(0, 300);
194
upperRightArmAnimation->setRotationAt(1, 310);
196
QGraphicsItemAnimation *lowerRightArmAnimation = new QGraphicsItemAnimation;
197
lowerRightArmAnimation->setItem(lowerRightArmItem);
198
lowerRightArmAnimation->setTimeLine(timeLine);
199
lowerRightArmAnimation->setRotationAt(0, 0);
200
lowerRightArmAnimation->setRotationAt(1, -70);
202
QGraphicsItemAnimation *upperLeftLegAnimation = new QGraphicsItemAnimation;
203
upperLeftLegAnimation->setItem(upperLeftLegItem);
204
upperLeftLegAnimation->setTimeLine(timeLine);
205
upperLeftLegAnimation->setRotationAt(0, 150);
206
upperLeftLegAnimation->setRotationAt(1, 80);
208
QGraphicsItemAnimation *lowerLeftLegAnimation = new QGraphicsItemAnimation;
209
lowerLeftLegAnimation->setItem(lowerLeftLegItem);
210
lowerLeftLegAnimation->setTimeLine(timeLine);
211
lowerLeftLegAnimation->setRotationAt(0, 70);
212
lowerLeftLegAnimation->setRotationAt(1, 10);
214
QGraphicsItemAnimation *upperRightLegAnimation = new QGraphicsItemAnimation;
215
upperRightLegAnimation->setItem(upperRightLegItem);
216
upperRightLegAnimation->setTimeLine(timeLine);
217
upperRightLegAnimation->setRotationAt(0, 40);
218
upperRightLegAnimation->setRotationAt(1, 120);
220
QGraphicsItemAnimation *lowerRightLegAnimation = new QGraphicsItemAnimation;
221
lowerRightLegAnimation->setItem(lowerRightLegItem);
222
lowerRightLegAnimation->setTimeLine(timeLine);
223
lowerRightLegAnimation->setRotationAt(0, 10);
224
lowerRightLegAnimation->setRotationAt(1, 50);
226
QGraphicsItemAnimation *torsoAnimation = new QGraphicsItemAnimation;
227
torsoAnimation->setItem(torsoItem);
228
torsoAnimation->setTimeLine(timeLine);
229
torsoAnimation->setRotationAt(0, 5);
230
torsoAnimation->setRotationAt(1, -20);
232
timeLine->setUpdateInterval(1000 / 25);
233
timeLine->setCurveShape(QTimeLine::SineCurve);
234
timeLine->setLoopCount(0);
235
timeLine->setDuration(2000);
244
QRectF Robot::boundingRect() const
249
void Robot::paint(QPainter *painter,
250
const QStyleOptionGraphicsItem *option, QWidget *widget)