| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "robot.h"
|
|---|
| 45 |
|
|---|
| 46 | RobotPart::RobotPart(QGraphicsItem *parent)
|
|---|
| 47 | : QGraphicsItem(parent), color(Qt::lightGray), dragOver(false)
|
|---|
| 48 | {
|
|---|
| 49 | setAcceptDrops(true);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
|
|---|
| 53 | {
|
|---|
| 54 | if (event->mimeData()->hasColor()
|
|---|
| 55 | || (qgraphicsitem_cast<RobotHead *>(this) && event->mimeData()->hasImage())) {
|
|---|
| 56 | event->setAccepted(true);
|
|---|
| 57 | dragOver = true;
|
|---|
| 58 | update();
|
|---|
| 59 | } else {
|
|---|
| 60 | event->setAccepted(false);
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
|
|---|
| 65 | {
|
|---|
| 66 | Q_UNUSED(event);
|
|---|
| 67 | dragOver = false;
|
|---|
| 68 | update();
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event)
|
|---|
| 72 | {
|
|---|
| 73 | dragOver = false;
|
|---|
| 74 | if (event->mimeData()->hasColor())
|
|---|
| 75 | color = qVariantValue<QColor>(event->mimeData()->colorData());
|
|---|
| 76 | else if (event->mimeData()->hasImage())
|
|---|
| 77 | pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData());
|
|---|
| 78 | update();
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | RobotHead::RobotHead(QGraphicsItem *parent)
|
|---|
| 82 | : RobotPart(parent)
|
|---|
| 83 | {
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | QRectF RobotHead::boundingRect() const
|
|---|
| 87 | {
|
|---|
| 88 | return QRectF(-15, -50, 30, 50);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void RobotHead::paint(QPainter *painter,
|
|---|
| 92 | const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 93 | {
|
|---|
| 94 | Q_UNUSED(option);
|
|---|
| 95 | Q_UNUSED(widget);
|
|---|
| 96 | if (pixmap.isNull()) {
|
|---|
| 97 | painter->setBrush(dragOver ? color.light(130) : color);
|
|---|
| 98 | painter->drawRoundedRect(-10, -30, 20, 30, 25, 25, Qt::RelativeSize);
|
|---|
| 99 | painter->setBrush(Qt::white);
|
|---|
| 100 | painter->drawEllipse(-7, -3 - 20, 7, 7);
|
|---|
| 101 | painter->drawEllipse(0, -3 - 20, 7, 7);
|
|---|
| 102 | painter->setBrush(Qt::black);
|
|---|
| 103 | painter->drawEllipse(-5, -1 - 20, 2, 2);
|
|---|
| 104 | painter->drawEllipse(2, -1 - 20, 2, 2);
|
|---|
| 105 | painter->setPen(QPen(Qt::black, 2));
|
|---|
| 106 | painter->setBrush(Qt::NoBrush);
|
|---|
| 107 | painter->drawArc(-6, -2 - 20, 12, 15, 190 * 16, 160 * 16);
|
|---|
| 108 | } else {
|
|---|
| 109 | painter->scale(.2272, .2824);
|
|---|
| 110 | painter->drawPixmap(QPointF(-15 * 4.4, -50 * 3.54), pixmap);
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | int RobotHead::type() const
|
|---|
| 115 | {
|
|---|
| 116 | return Type;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | RobotTorso::RobotTorso(QGraphicsItem *parent)
|
|---|
| 120 | : RobotPart(parent)
|
|---|
| 121 | {
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | QRectF RobotTorso::boundingRect() const
|
|---|
| 125 | {
|
|---|
| 126 | return QRectF(-30, -20, 60, 60);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | void RobotTorso::paint(QPainter *painter,
|
|---|
| 130 | const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 131 | {
|
|---|
| 132 | Q_UNUSED(option);
|
|---|
| 133 | Q_UNUSED(widget);
|
|---|
| 134 |
|
|---|
| 135 | painter->setBrush(dragOver ? color.light(130) : color);
|
|---|
| 136 | painter->drawRoundedRect(-20, -20, 40, 60, 25, 25, Qt::RelativeSize);
|
|---|
| 137 | painter->drawEllipse(-25, -20, 20, 20);
|
|---|
| 138 | painter->drawEllipse(5, -20, 20, 20);
|
|---|
| 139 | painter->drawEllipse(-20, 22, 20, 20);
|
|---|
| 140 | painter->drawEllipse(0, 22, 20, 20);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | RobotLimb::RobotLimb(QGraphicsItem *parent)
|
|---|
| 144 | : RobotPart(parent)
|
|---|
| 145 | {
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | QRectF RobotLimb::boundingRect() const
|
|---|
| 149 | {
|
|---|
| 150 | return QRectF(-5, -5, 40, 10);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | void RobotLimb::paint(QPainter *painter,
|
|---|
| 154 | const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 155 | {
|
|---|
| 156 | Q_UNUSED(option);
|
|---|
| 157 | Q_UNUSED(widget);
|
|---|
| 158 |
|
|---|
| 159 | painter->setBrush(dragOver ? color.light(130) : color);
|
|---|
| 160 | painter->drawRoundedRect(boundingRect(), 50, 50, Qt::RelativeSize);
|
|---|
| 161 | painter->drawEllipse(-5, -5, 10, 10);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | Robot::Robot()
|
|---|
| 165 | {
|
|---|
| 166 | QGraphicsItem *torsoItem = new RobotTorso(this);
|
|---|
| 167 | QGraphicsItem *headItem = new RobotHead(torsoItem);
|
|---|
| 168 | QGraphicsItem *upperLeftArmItem = new RobotLimb(torsoItem);
|
|---|
| 169 | QGraphicsItem *lowerLeftArmItem = new RobotLimb(upperLeftArmItem);
|
|---|
| 170 | QGraphicsItem *upperRightArmItem = new RobotLimb(torsoItem);
|
|---|
| 171 | QGraphicsItem *lowerRightArmItem = new RobotLimb(upperRightArmItem);
|
|---|
| 172 | QGraphicsItem *upperRightLegItem = new RobotLimb(torsoItem);
|
|---|
| 173 | QGraphicsItem *lowerRightLegItem = new RobotLimb(upperRightLegItem);
|
|---|
| 174 | QGraphicsItem *upperLeftLegItem = new RobotLimb(torsoItem);
|
|---|
| 175 | QGraphicsItem *lowerLeftLegItem = new RobotLimb(upperLeftLegItem);
|
|---|
| 176 |
|
|---|
| 177 | headItem->setPos(0, -18);
|
|---|
| 178 | upperLeftArmItem->setPos(-15, -10);
|
|---|
| 179 | lowerLeftArmItem->setPos(30, 0);
|
|---|
| 180 | upperRightArmItem->setPos(15, -10);
|
|---|
| 181 | lowerRightArmItem->setPos(30, 0);
|
|---|
| 182 | upperRightLegItem->setPos(10, 32);
|
|---|
| 183 | lowerRightLegItem->setPos(30, 0);
|
|---|
| 184 | upperLeftLegItem->setPos(-10, 32);
|
|---|
| 185 | lowerLeftLegItem->setPos(30, 0);
|
|---|
| 186 |
|
|---|
| 187 | timeLine = new QTimeLine;
|
|---|
| 188 |
|
|---|
| 189 | QGraphicsItemAnimation *headAnimation = new QGraphicsItemAnimation;
|
|---|
| 190 | headAnimation->setItem(headItem);
|
|---|
| 191 | headAnimation->setTimeLine(timeLine);
|
|---|
| 192 | headAnimation->setRotationAt(0, 20);
|
|---|
| 193 | headAnimation->setRotationAt(1, -20);
|
|---|
| 194 | headAnimation->setScaleAt(1, 1.1, 1.1);
|
|---|
| 195 |
|
|---|
| 196 | QGraphicsItemAnimation *upperLeftArmAnimation = new QGraphicsItemAnimation;
|
|---|
| 197 | upperLeftArmAnimation->setItem(upperLeftArmItem);
|
|---|
| 198 | upperLeftArmAnimation->setTimeLine(timeLine);
|
|---|
| 199 | upperLeftArmAnimation->setRotationAt(0, 190);
|
|---|
| 200 | upperLeftArmAnimation->setRotationAt(1, 180);
|
|---|
| 201 |
|
|---|
| 202 | QGraphicsItemAnimation *lowerLeftArmAnimation = new QGraphicsItemAnimation;
|
|---|
| 203 | lowerLeftArmAnimation->setItem(lowerLeftArmItem);
|
|---|
| 204 | lowerLeftArmAnimation->setTimeLine(timeLine);
|
|---|
| 205 | lowerLeftArmAnimation->setRotationAt(0, 50);
|
|---|
| 206 | lowerLeftArmAnimation->setRotationAt(1, 10);
|
|---|
| 207 |
|
|---|
| 208 | QGraphicsItemAnimation *upperRightArmAnimation = new QGraphicsItemAnimation;
|
|---|
| 209 | upperRightArmAnimation->setItem(upperRightArmItem);
|
|---|
| 210 | upperRightArmAnimation->setTimeLine(timeLine);
|
|---|
| 211 | upperRightArmAnimation->setRotationAt(0, 300);
|
|---|
| 212 | upperRightArmAnimation->setRotationAt(1, 310);
|
|---|
| 213 |
|
|---|
| 214 | QGraphicsItemAnimation *lowerRightArmAnimation = new QGraphicsItemAnimation;
|
|---|
| 215 | lowerRightArmAnimation->setItem(lowerRightArmItem);
|
|---|
| 216 | lowerRightArmAnimation->setTimeLine(timeLine);
|
|---|
| 217 | lowerRightArmAnimation->setRotationAt(0, 0);
|
|---|
| 218 | lowerRightArmAnimation->setRotationAt(1, -70);
|
|---|
| 219 |
|
|---|
| 220 | QGraphicsItemAnimation *upperLeftLegAnimation = new QGraphicsItemAnimation;
|
|---|
| 221 | upperLeftLegAnimation->setItem(upperLeftLegItem);
|
|---|
| 222 | upperLeftLegAnimation->setTimeLine(timeLine);
|
|---|
| 223 | upperLeftLegAnimation->setRotationAt(0, 150);
|
|---|
| 224 | upperLeftLegAnimation->setRotationAt(1, 80);
|
|---|
| 225 |
|
|---|
| 226 | QGraphicsItemAnimation *lowerLeftLegAnimation = new QGraphicsItemAnimation;
|
|---|
| 227 | lowerLeftLegAnimation->setItem(lowerLeftLegItem);
|
|---|
| 228 | lowerLeftLegAnimation->setTimeLine(timeLine);
|
|---|
| 229 | lowerLeftLegAnimation->setRotationAt(0, 70);
|
|---|
| 230 | lowerLeftLegAnimation->setRotationAt(1, 10);
|
|---|
| 231 |
|
|---|
| 232 | QGraphicsItemAnimation *upperRightLegAnimation = new QGraphicsItemAnimation;
|
|---|
| 233 | upperRightLegAnimation->setItem(upperRightLegItem);
|
|---|
| 234 | upperRightLegAnimation->setTimeLine(timeLine);
|
|---|
| 235 | upperRightLegAnimation->setRotationAt(0, 40);
|
|---|
| 236 | upperRightLegAnimation->setRotationAt(1, 120);
|
|---|
| 237 |
|
|---|
| 238 | QGraphicsItemAnimation *lowerRightLegAnimation = new QGraphicsItemAnimation;
|
|---|
| 239 | lowerRightLegAnimation->setItem(lowerRightLegItem);
|
|---|
| 240 | lowerRightLegAnimation->setTimeLine(timeLine);
|
|---|
| 241 | lowerRightLegAnimation->setRotationAt(0, 10);
|
|---|
| 242 | lowerRightLegAnimation->setRotationAt(1, 50);
|
|---|
| 243 |
|
|---|
| 244 | QGraphicsItemAnimation *torsoAnimation = new QGraphicsItemAnimation;
|
|---|
| 245 | torsoAnimation->setItem(torsoItem);
|
|---|
| 246 | torsoAnimation->setTimeLine(timeLine);
|
|---|
| 247 | torsoAnimation->setRotationAt(0, 5);
|
|---|
| 248 | torsoAnimation->setRotationAt(1, -20);
|
|---|
| 249 |
|
|---|
| 250 | timeLine->setUpdateInterval(1000 / 25);
|
|---|
| 251 | timeLine->setCurveShape(QTimeLine::SineCurve);
|
|---|
| 252 | timeLine->setLoopCount(0);
|
|---|
| 253 | timeLine->setDuration(2000);
|
|---|
| 254 | timeLine->start();
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | Robot::~Robot()
|
|---|
| 258 | {
|
|---|
| 259 | delete timeLine;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | QRectF Robot::boundingRect() const
|
|---|
| 263 | {
|
|---|
| 264 | return QRectF();
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | void Robot::paint(QPainter *painter,
|
|---|
| 268 | const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 269 | {
|
|---|
| 270 | Q_UNUSED(painter);
|
|---|
| 271 | Q_UNUSED(option);
|
|---|
| 272 | Q_UNUSED(widget);
|
|---|
| 273 | }
|
|---|