Changeset 846 for trunk/examples/graphicsview/dragdroprobot/robot.cpp
- Timestamp:
- May 5, 2011, 5:36:53 AM (14 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/vendor/nokia/qt/4.7.2 (added) merged: 845 /branches/vendor/nokia/qt/current merged: 844 /branches/vendor/nokia/qt/4.6.3 removed
- Property svn:mergeinfo changed
-
trunk/examples/graphicsview/dragdroprobot/robot.cpp
r651 r846 1 1 /**************************************************************************** 2 2 ** 3 ** Copyright (C) 201 0Nokia Corporation and/or its subsidiary(-ies).3 ** Copyright (C) 201 Nokia Corporation and/or its subsidiary(-ies). 4 4 ** All rights reserved. 5 5 ** Contact: Nokia Corporation ([email protected]) … … 7 7 ** This file is part of the examples of the Qt Toolkit. 8 8 ** 9 ** $QT_BEGIN_LICENSE:LGPL$ 10 ** Commercial Usage 11 ** Licensees holding valid Qt Commercial licenses may use this file in 12 ** accordance with the Qt Commercial License Agreement provided with the 13 ** Software or, alternatively, in accordance with the terms contained in 14 ** a written agreement between you and Nokia. 15 ** 16 ** GNU Lesser General Public License Usage 17 ** Alternatively, this file may be used under the terms of the GNU Lesser 18 ** General Public License version 2.1 as published by the Free Software 19 ** Foundation and appearing in the file LICENSE.LGPL included in the 20 ** packaging of this file. Please review the following information to 21 ** ensure the GNU Lesser General Public License version 2.1 requirements 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 23 ** 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact 37 ** Nokia at [email protected]. 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." 38 37 ** $QT_END_LICENSE$ 39 38 ** … … 44 43 #include "robot.h" 45 44 45 46 46 RobotPart::RobotPart(QGraphicsItem *parent) 47 : QGraphics Item(parent), color(Qt::lightGray), dragOver(false)47 : QGraphics(parent), color(Qt::lightGray), dragOver(false) 48 48 { 49 49 setAcceptDrops(true); 50 50 } 51 51 //! [0] 52 53 //! [1] 52 54 void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event) 53 55 { 54 if (event->mimeData()->hasColor() 55 || (qgraphicsitem_cast<RobotHead *>(this) && event->mimeData()->hasImage())) { 56 if (event->mimeData()->hasColor()) { 56 57 event->setAccepted(true); 57 58 dragOver = true; … … 61 62 } 62 63 } 63 64 //! [1] 65 66 //! [2] 64 67 void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) 65 68 { … … 68 71 update(); 69 72 } 70 73 //! [2] 74 75 //! [3] 71 76 void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event) 72 77 { … … 74 79 if (event->mimeData()->hasColor()) 75 80 color = qVariantValue<QColor>(event->mimeData()->colorData()); 76 else if (event->mimeData()->hasImage())77 pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData());78 81 update(); 79 82 } 80 83 //! [3] 84 85 //! [4] 81 86 RobotHead::RobotHead(QGraphicsItem *parent) 82 87 : RobotPart(parent) 83 88 { 84 89 } 85 90 //! [4] 91 92 //! [5] 86 93 QRectF RobotHead::boundingRect() const 87 94 { 88 95 return QRectF(-15, -50, 30, 50); 89 96 } 90 97 //! [5] 98 99 //! [6] 91 100 void RobotHead::paint(QPainter *painter, 92 101 const QStyleOptionGraphicsItem *option, QWidget *widget) … … 111 120 } 112 121 } 113 114 int RobotHead::type() const 115 { 116 return Type; 117 } 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] 118 149 119 150 RobotTorso::RobotTorso(QGraphicsItem *parent) … … 162 193 } 163 194 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 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] 177 214 headItem->setPos(0, -18); 178 215 upperLeftArmItem->setPos(-15, -10); … … 184 221 upperLeftLegItem->setPos(-10, 32); 185 222 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 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] 262 295 QRectF Robot::boundingRect() const 263 296 { … … 272 305 Q_UNUSED(widget); 273 306 } 307
Note:
See TracChangeset
for help on using the changeset viewer.