~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/graphicsview/dragdroprobot/robot.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-10-12 23:14:14 UTC
  • mto: (15.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20061012231414-y2oqbom5dy389os0
Tags: upstream-4.2.0
ImportĀ upstreamĀ versionĀ 4.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2006-2006 Trolltech ASA. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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
 
13
**
 
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.
 
18
**
 
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.
 
21
**
 
22
****************************************************************************/
 
23
 
 
24
#include <QtGui>
 
25
 
 
26
#include "robot.h"
 
27
 
 
28
RobotPart::RobotPart(QGraphicsItem *parent)
 
29
    : QGraphicsItem(parent), color(Qt::lightGray), dragOver(false)
 
30
{
 
31
    setAcceptDrops(true);
 
32
}
 
33
 
 
34
void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
 
35
{
 
36
    if (event->mimeData()->hasColor()
 
37
        || (qgraphicsitem_cast<RobotHead *>(this) && event->mimeData()->hasImage())) {
 
38
        event->setAccepted(true);
 
39
        dragOver = true;
 
40
        update();
 
41
    } else {
 
42
        event->setAccepted(false);
 
43
    }
 
44
}
 
45
 
 
46
void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
 
47
{
 
48
    Q_UNUSED(event);
 
49
    dragOver = false;
 
50
    update();
 
51
}
 
52
 
 
53
void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event)
 
54
{
 
55
    dragOver = false;
 
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());
 
60
    update();
 
61
}
 
62
 
 
63
RobotHead::RobotHead(QGraphicsItem *parent)
 
64
    : RobotPart(parent)
 
65
{
 
66
}
 
67
 
 
68
QRectF RobotHead::boundingRect() const
 
69
{
 
70
    return QRectF(-15, -50, 30, 50);
 
71
}
 
72
 
 
73
void RobotHead::paint(QPainter *painter,
 
74
           const QStyleOptionGraphicsItem *option, QWidget *widget)
 
75
{
 
76
    Q_UNUSED(option);
 
77
    Q_UNUSED(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);
 
90
    } else {
 
91
        painter->scale(.2272, .2824);
 
92
        painter->drawPixmap(QPointF(-15 * 4.4, -50 * 3.54), pixmap);
 
93
    }
 
94
}
 
95
 
 
96
int RobotHead::type() const
 
97
{
 
98
    return Type;
 
99
}
 
100
 
 
101
RobotTorso::RobotTorso(QGraphicsItem *parent)
 
102
    : RobotPart(parent)
 
103
{
 
104
}
 
105
 
 
106
QRectF RobotTorso::boundingRect() const
 
107
{
 
108
    return QRectF(-30, -20, 60, 60);
 
109
}
 
110
 
 
111
void RobotTorso::paint(QPainter *painter,
 
112
           const QStyleOptionGraphicsItem *option, QWidget *widget)
 
113
{
 
114
    Q_UNUSED(option);
 
115
    Q_UNUSED(widget);
 
116
    
 
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);
 
123
}
 
124
 
 
125
RobotLimb::RobotLimb(QGraphicsItem *parent)
 
126
    : RobotPart(parent)
 
127
{
 
128
}
 
129
 
 
130
QRectF RobotLimb::boundingRect() const
 
131
{
 
132
    return QRectF(-5, -5, 40, 10);
 
133
}
 
134
 
 
135
void RobotLimb::paint(QPainter *painter,
 
136
           const QStyleOptionGraphicsItem *option, QWidget *widget)
 
137
{
 
138
    Q_UNUSED(option);
 
139
    Q_UNUSED(widget);
 
140
 
 
141
    painter->setBrush(dragOver ? color.light(130) : color);
 
142
    painter->drawRoundRect(boundingRect(), 50, 50);
 
143
    painter->drawEllipse(-5, -5, 10, 10);
 
144
}
 
145
 
 
146
Robot::Robot()
 
147
{
 
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);
 
158
    
 
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);
 
168
 
 
169
    timeLine = new QTimeLine;
 
170
 
 
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);
 
177
 
 
178
    QGraphicsItemAnimation *upperLeftArmAnimation = new QGraphicsItemAnimation;
 
179
    upperLeftArmAnimation->setItem(upperLeftArmItem);
 
180
    upperLeftArmAnimation->setTimeLine(timeLine);
 
181
    upperLeftArmAnimation->setRotationAt(0, 190);
 
182
    upperLeftArmAnimation->setRotationAt(1, 180);
 
183
 
 
184
    QGraphicsItemAnimation *lowerLeftArmAnimation = new QGraphicsItemAnimation;
 
185
    lowerLeftArmAnimation->setItem(lowerLeftArmItem);
 
186
    lowerLeftArmAnimation->setTimeLine(timeLine);
 
187
    lowerLeftArmAnimation->setRotationAt(0, 50);
 
188
    lowerLeftArmAnimation->setRotationAt(1, 10);
 
189
    
 
190
    QGraphicsItemAnimation *upperRightArmAnimation = new QGraphicsItemAnimation;
 
191
    upperRightArmAnimation->setItem(upperRightArmItem);
 
192
    upperRightArmAnimation->setTimeLine(timeLine);
 
193
    upperRightArmAnimation->setRotationAt(0, 300);
 
194
    upperRightArmAnimation->setRotationAt(1, 310);
 
195
 
 
196
    QGraphicsItemAnimation *lowerRightArmAnimation = new QGraphicsItemAnimation;
 
197
    lowerRightArmAnimation->setItem(lowerRightArmItem);
 
198
    lowerRightArmAnimation->setTimeLine(timeLine);
 
199
    lowerRightArmAnimation->setRotationAt(0, 0);
 
200
    lowerRightArmAnimation->setRotationAt(1, -70);
 
201
 
 
202
    QGraphicsItemAnimation *upperLeftLegAnimation = new QGraphicsItemAnimation;
 
203
    upperLeftLegAnimation->setItem(upperLeftLegItem);
 
204
    upperLeftLegAnimation->setTimeLine(timeLine);
 
205
    upperLeftLegAnimation->setRotationAt(0, 150);
 
206
    upperLeftLegAnimation->setRotationAt(1, 80);
 
207
 
 
208
    QGraphicsItemAnimation *lowerLeftLegAnimation = new QGraphicsItemAnimation;
 
209
    lowerLeftLegAnimation->setItem(lowerLeftLegItem);
 
210
    lowerLeftLegAnimation->setTimeLine(timeLine);
 
211
    lowerLeftLegAnimation->setRotationAt(0, 70);
 
212
    lowerLeftLegAnimation->setRotationAt(1, 10);
 
213
 
 
214
    QGraphicsItemAnimation *upperRightLegAnimation = new QGraphicsItemAnimation;
 
215
    upperRightLegAnimation->setItem(upperRightLegItem);
 
216
    upperRightLegAnimation->setTimeLine(timeLine);
 
217
    upperRightLegAnimation->setRotationAt(0, 40);
 
218
    upperRightLegAnimation->setRotationAt(1, 120);
 
219
    
 
220
    QGraphicsItemAnimation *lowerRightLegAnimation = new QGraphicsItemAnimation;
 
221
    lowerRightLegAnimation->setItem(lowerRightLegItem);
 
222
    lowerRightLegAnimation->setTimeLine(timeLine);
 
223
    lowerRightLegAnimation->setRotationAt(0, 10);
 
224
    lowerRightLegAnimation->setRotationAt(1, 50);
 
225
    
 
226
    QGraphicsItemAnimation *torsoAnimation = new QGraphicsItemAnimation;
 
227
    torsoAnimation->setItem(torsoItem);
 
228
    torsoAnimation->setTimeLine(timeLine);
 
229
    torsoAnimation->setRotationAt(0, 5);
 
230
    torsoAnimation->setRotationAt(1, -20);
 
231
 
 
232
    timeLine->setUpdateInterval(1000 / 25);
 
233
    timeLine->setCurveShape(QTimeLine::SineCurve);
 
234
    timeLine->setLoopCount(0);
 
235
    timeLine->setDuration(2000);
 
236
    timeLine->start();
 
237
}
 
238
 
 
239
Robot::~Robot()
 
240
{
 
241
    delete timeLine;
 
242
}
 
243
 
 
244
QRectF Robot::boundingRect() const
 
245
{
 
246
    return QRectF();
 
247
}
 
248
 
 
249
void Robot::paint(QPainter *painter,
 
250
                  const QStyleOptionGraphicsItem *option, QWidget *widget)
 
251
{
 
252
    Q_UNUSED(painter);
 
253
    Q_UNUSED(option);
 
254
    Q_UNUSED(widget);
 
255
}