| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 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:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include <QtGui>
|
|---|
| 42 |
|
|---|
| 43 | #include "robot.h"
|
|---|
| 44 |
|
|---|
| 45 | //! [0]
|
|---|
| 46 | RobotPart::RobotPart(QGraphicsItem *parent)
|
|---|
| 47 | : QGraphicsObject(parent), color(Qt::lightGray), dragOver(false)
|
|---|
| 48 | {
|
|---|
| 49 | setAcceptDrops(true);
|
|---|
| 50 | }
|
|---|
| 51 | //! [0]
|
|---|
| 52 |
|
|---|
| 53 | //! [1]
|
|---|
| 54 | void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
|
|---|
| 55 | {
|
|---|
| 56 | if (event->mimeData()->hasColor()) {
|
|---|
| 57 | event->setAccepted(true);
|
|---|
| 58 | dragOver = true;
|
|---|
| 59 | update();
|
|---|
| 60 | } else {
|
|---|
| 61 | event->setAccepted(false);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | //! [1]
|
|---|
| 65 |
|
|---|
| 66 | //! [2]
|
|---|
| 67 | void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
|
|---|
| 68 | {
|
|---|
| 69 | Q_UNUSED(event);
|
|---|
| 70 | dragOver = false;
|
|---|
| 71 | update();
|
|---|
| 72 | }
|
|---|
| 73 | //! [2]
|
|---|
| 74 |
|
|---|
| 75 | //! [3]
|
|---|
| 76 | void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event)
|
|---|
| 77 | {
|
|---|
| 78 | dragOver = false;
|
|---|
| 79 | if (event->mimeData()->hasColor())
|
|---|
| 80 | color = qVariantValue<QColor>(event->mimeData()->colorData());
|
|---|
| 81 | update();
|
|---|
| 82 | }
|
|---|
| 83 | //! [3]
|
|---|
| 84 |
|
|---|
| 85 | //! [4]
|
|---|
| 86 | RobotHead::RobotHead(QGraphicsItem *parent)
|
|---|
| 87 | : RobotPart(parent)
|
|---|
| 88 | {
|
|---|
| 89 | }
|
|---|
| 90 | //! [4]
|
|---|
| 91 |
|
|---|
| 92 | //! [5]
|
|---|
| 93 | QRectF RobotHead::boundingRect() const
|
|---|
| 94 | {
|
|---|
| 95 | return QRectF(-15, -50, 30, 50);
|
|---|
| 96 | }
|
|---|
| 97 | //! [5]
|
|---|
| 98 |
|
|---|
| 99 | //! [6]
|
|---|
| 100 | void RobotHead::paint(QPainter *painter,
|
|---|
| 101 | const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 102 | {
|
|---|
| 103 | Q_UNUSED(option);
|
|---|
| 104 | Q_UNUSED(widget);
|
|---|
| 105 | if (pixmap.isNull()) {
|
|---|
| 106 | painter->setBrush(dragOver ? color.light(130) : color);
|
|---|
| 107 | painter->drawRoundedRect(-10, -30, 20, 30, 25, 25, Qt::RelativeSize);
|
|---|
| 108 | painter->setBrush(Qt::white);
|
|---|
| 109 | painter->drawEllipse(-7, -3 - 20, 7, 7);
|
|---|
| 110 | painter->drawEllipse(0, -3 - 20, 7, 7);
|
|---|
| 111 | painter->setBrush(Qt::black);
|
|---|
| 112 | painter->drawEllipse(-5, -1 - 20, 2, 2);
|
|---|
| 113 | painter->drawEllipse(2, -1 - 20, 2, 2);
|
|---|
| 114 | painter->setPen(QPen(Qt::black, 2));
|
|---|
| 115 | painter->setBrush(Qt::NoBrush);
|
|---|
| 116 | painter->drawArc(-6, -2 - 20, 12, 15, 190 * 16, 160 * 16);
|
|---|
| 117 | } else {
|
|---|
| 118 | painter->scale(.2272, .2824);
|
|---|
| 119 | painter->drawPixmap(QPointF(-15 * 4.4, -50 * 3.54), pixmap);
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | //! [6]
|
|---|
| 123 |
|
|---|
| 124 | //! [7]
|
|---|
| 125 | void RobotHead::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
|
|---|
| 126 | {
|
|---|
| 127 | if (event->mimeData()->hasImage()) {
|
|---|
| 128 | event->setAccepted(true);
|
|---|
| 129 | dragOver = true;
|
|---|
| 130 | update();
|
|---|
| 131 | } else {
|
|---|
| 132 | RobotPart::dragEnterEvent(event);
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | //! [7]
|
|---|
| 136 |
|
|---|
| 137 | //! [8]
|
|---|
| 138 | void RobotHead::dropEvent(QGraphicsSceneDragDropEvent *event)
|
|---|
| 139 | {
|
|---|
| 140 | if (event->mimeData()->hasImage()) {
|
|---|
| 141 | dragOver = false;
|
|---|
| 142 | pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData());
|
|---|
| 143 | update();
|
|---|
| 144 | } else {
|
|---|
| 145 | RobotPart::dropEvent(event);
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | //! [8]
|
|---|
| 149 |
|
|---|
| 150 | RobotTorso::RobotTorso(QGraphicsItem *parent)
|
|---|
| 151 | : RobotPart(parent)
|
|---|
| 152 | {
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | QRectF RobotTorso::boundingRect() const
|
|---|
| 156 | {
|
|---|
| 157 | return QRectF(-30, -20, 60, 60);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | void RobotTorso::paint(QPainter *painter,
|
|---|
| 161 | const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 162 | {
|
|---|
| 163 | Q_UNUSED(option);
|
|---|
| 164 | Q_UNUSED(widget);
|
|---|
| 165 |
|
|---|
| 166 | painter->setBrush(dragOver ? color.light(130) : color);
|
|---|
| 167 | painter->drawRoundedRect(-20, -20, 40, 60, 25, 25, Qt::RelativeSize);
|
|---|
| 168 | painter->drawEllipse(-25, -20, 20, 20);
|
|---|
| 169 | painter->drawEllipse(5, -20, 20, 20);
|
|---|
| 170 | painter->drawEllipse(-20, 22, 20, 20);
|
|---|
| 171 | painter->drawEllipse(0, 22, 20, 20);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | RobotLimb::RobotLimb(QGraphicsItem *parent)
|
|---|
| 175 | : RobotPart(parent)
|
|---|
| 176 | {
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | QRectF RobotLimb::boundingRect() const
|
|---|
| 180 | {
|
|---|
| 181 | return QRectF(-5, -5, 40, 10);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | void RobotLimb::paint(QPainter *painter,
|
|---|
| 185 | const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 186 | {
|
|---|
| 187 | Q_UNUSED(option);
|
|---|
| 188 | Q_UNUSED(widget);
|
|---|
| 189 |
|
|---|
| 190 | painter->setBrush(dragOver ? color.light(130) : color);
|
|---|
| 191 | painter->drawRoundedRect(boundingRect(), 50, 50, Qt::RelativeSize);
|
|---|
| 192 | painter->drawEllipse(-5, -5, 10, 10);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | //! [10]
|
|---|
| 196 | Robot::Robot(QGraphicsItem *parent)
|
|---|
| 197 | : RobotPart(parent)
|
|---|
| 198 | {
|
|---|
| 199 | setFlag(ItemHasNoContents);
|
|---|
| 200 |
|
|---|
| 201 | QGraphicsObject *torsoItem = new RobotTorso(this);
|
|---|
| 202 | QGraphicsObject *headItem = new RobotHead(torsoItem);
|
|---|
| 203 | QGraphicsObject *upperLeftArmItem = new RobotLimb(torsoItem);
|
|---|
| 204 | QGraphicsObject *lowerLeftArmItem = new RobotLimb(upperLeftArmItem);
|
|---|
| 205 | QGraphicsObject *upperRightArmItem = new RobotLimb(torsoItem);
|
|---|
| 206 | QGraphicsObject *lowerRightArmItem = new RobotLimb(upperRightArmItem);
|
|---|
| 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]
|
|---|
| 214 | headItem->setPos(0, -18);
|
|---|
| 215 | upperLeftArmItem->setPos(-15, -10);
|
|---|
| 216 | lowerLeftArmItem->setPos(30, 0);
|
|---|
| 217 | upperRightArmItem->setPos(15, -10);
|
|---|
| 218 | lowerRightArmItem->setPos(30, 0);
|
|---|
| 219 | upperRightLegItem->setPos(10, 32);
|
|---|
| 220 | lowerRightLegItem->setPos(30, 0);
|
|---|
| 221 | upperLeftLegItem->setPos(-10, 32);
|
|---|
| 222 | lowerLeftLegItem->setPos(30, 0);
|
|---|
| 223 | //! [11]
|
|---|
| 224 |
|
|---|
| 225 | //! [12]
|
|---|
| 226 | QParallelAnimationGroup *animation = new QParallelAnimationGroup(this);
|
|---|
| 227 |
|
|---|
| 228 | QPropertyAnimation *headAnimation = new QPropertyAnimation(headItem, "rotation");
|
|---|
| 229 | headAnimation->setStartValue(20);
|
|---|
| 230 | headAnimation->setEndValue(-20);
|
|---|
| 231 | QPropertyAnimation *headScaleAnimation = new QPropertyAnimation(headItem, "scale");
|
|---|
| 232 | headScaleAnimation->setEndValue(1.1);
|
|---|
| 233 | animation->addAnimation(headAnimation);
|
|---|
| 234 | animation->addAnimation(headScaleAnimation);
|
|---|
| 235 | //! [12]
|
|---|
| 236 |
|
|---|
| 237 | QPropertyAnimation *upperLeftArmAnimation = new QPropertyAnimation(upperLeftArmItem, "rotation");
|
|---|
| 238 | upperLeftArmAnimation->setStartValue(190);
|
|---|
| 239 | upperLeftArmAnimation->setEndValue(180);
|
|---|
| 240 | animation->addAnimation(upperLeftArmAnimation);
|
|---|
| 241 |
|
|---|
| 242 | QPropertyAnimation *lowerLeftArmAnimation = new QPropertyAnimation(lowerLeftArmItem, "rotation");
|
|---|
| 243 | lowerLeftArmAnimation->setStartValue(50);
|
|---|
| 244 | lowerLeftArmAnimation->setEndValue(10);
|
|---|
| 245 | animation->addAnimation(lowerLeftArmAnimation);
|
|---|
| 246 |
|
|---|
| 247 | QPropertyAnimation *upperRightArmAnimation = new QPropertyAnimation(upperRightArmItem, "rotation");
|
|---|
| 248 | upperRightArmAnimation->setStartValue(300);
|
|---|
| 249 | upperRightArmAnimation->setEndValue(310);
|
|---|
| 250 | animation->addAnimation(upperRightArmAnimation);
|
|---|
| 251 |
|
|---|
| 252 | QPropertyAnimation *lowerRightArmAnimation = new QPropertyAnimation(lowerRightArmItem, "rotation");
|
|---|
| 253 | lowerRightArmAnimation->setStartValue(0);
|
|---|
| 254 | lowerRightArmAnimation->setEndValue(-70);
|
|---|
| 255 | animation->addAnimation(lowerRightArmAnimation);
|
|---|
| 256 |
|
|---|
| 257 | QPropertyAnimation *upperLeftLegAnimation = new QPropertyAnimation(upperLeftLegItem, "rotation");
|
|---|
| 258 | upperLeftLegAnimation->setStartValue(150);
|
|---|
| 259 | upperLeftLegAnimation->setEndValue(80);
|
|---|
| 260 | animation->addAnimation(upperLeftLegAnimation);
|
|---|
| 261 |
|
|---|
| 262 | QPropertyAnimation *lowerLeftLegAnimation = new QPropertyAnimation(lowerLeftLegItem, "rotation");
|
|---|
| 263 | lowerLeftLegAnimation->setStartValue(70);
|
|---|
| 264 | lowerLeftLegAnimation->setEndValue(10);
|
|---|
| 265 | animation->addAnimation(lowerLeftLegAnimation);
|
|---|
| 266 |
|
|---|
| 267 | QPropertyAnimation *upperRightLegAnimation = new QPropertyAnimation(upperRightLegItem, "rotation");
|
|---|
| 268 | upperRightLegAnimation->setStartValue(40);
|
|---|
| 269 | upperRightLegAnimation->setEndValue(120);
|
|---|
| 270 | animation->addAnimation(upperRightLegAnimation);
|
|---|
| 271 |
|
|---|
| 272 | QPropertyAnimation *lowerRightLegAnimation = new QPropertyAnimation(lowerRightLegItem, "rotation");
|
|---|
| 273 | lowerRightLegAnimation->setStartValue(10);
|
|---|
| 274 | lowerRightLegAnimation->setEndValue(50);
|
|---|
| 275 | animation->addAnimation(lowerRightLegAnimation);
|
|---|
| 276 |
|
|---|
| 277 | QPropertyAnimation *torsoAnimation = new QPropertyAnimation(torsoItem, "rotation");
|
|---|
| 278 | torsoAnimation->setStartValue(5);
|
|---|
| 279 | torsoAnimation->setEndValue(-20);
|
|---|
| 280 | animation->addAnimation(torsoAnimation);
|
|---|
| 281 |
|
|---|
| 282 | //! [13]
|
|---|
| 283 | for (int i = 0; i < animation->animationCount(); ++i) {
|
|---|
| 284 | QPropertyAnimation *anim = qobject_cast<QPropertyAnimation *>(animation->animationAt(i));
|
|---|
| 285 | anim->setEasingCurve(QEasingCurve::SineCurve);
|
|---|
| 286 | anim->setDuration(2000);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | animation->setLoopCount(-1);
|
|---|
| 290 | animation->start();
|
|---|
| 291 | //! [13]
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | //! [9]
|
|---|
| 295 | QRectF Robot::boundingRect() const
|
|---|
| 296 | {
|
|---|
| 297 | return QRectF();
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | void Robot::paint(QPainter *painter,
|
|---|
| 301 | const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 302 | {
|
|---|
| 303 | Q_UNUSED(painter);
|
|---|
| 304 | Q_UNUSED(option);
|
|---|
| 305 | Q_UNUSED(widget);
|
|---|
| 306 | }
|
|---|
| 307 | //! [9]
|
|---|