From 806dda08d685bc5f9ed71dfe8b61f21848d48066 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Fri, 17 Aug 2012 13:23:19 +0200 Subject: Moving .qdoc files under examples/widgets/doc Updated those .qdoc files to refer to the new relative examples emplacement. Images and snippets to be moved later. Also grouped all widgets related examples under widgets. Change-Id: Ib29696e2d8948524537f53e8dda88f9ee26a597f Reviewed-by: J-P Nurmi --- .../graphicsview/dragdroprobot/coloritem.cpp | 142 ++++++++++ .../widgets/graphicsview/dragdroprobot/coloritem.h | 65 +++++ .../dragdroprobot/dragdroprobot.desktop | 11 + .../graphicsview/dragdroprobot/dragdroprobot.pro | 22 ++ .../graphicsview/dragdroprobot/images/head.png | Bin 0 -> 14972 bytes .../widgets/graphicsview/dragdroprobot/main.cpp | 94 +++++++ .../widgets/graphicsview/dragdroprobot/robot.cpp | 307 +++++++++++++++++++++ .../widgets/graphicsview/dragdroprobot/robot.h | 118 ++++++++ .../widgets/graphicsview/dragdroprobot/robot.qrc | 5 + 9 files changed, 764 insertions(+) create mode 100644 examples/widgets/graphicsview/dragdroprobot/coloritem.cpp create mode 100644 examples/widgets/graphicsview/dragdroprobot/coloritem.h create mode 100644 examples/widgets/graphicsview/dragdroprobot/dragdroprobot.desktop create mode 100644 examples/widgets/graphicsview/dragdroprobot/dragdroprobot.pro create mode 100644 examples/widgets/graphicsview/dragdroprobot/images/head.png create mode 100644 examples/widgets/graphicsview/dragdroprobot/main.cpp create mode 100644 examples/widgets/graphicsview/dragdroprobot/robot.cpp create mode 100644 examples/widgets/graphicsview/dragdroprobot/robot.h create mode 100644 examples/widgets/graphicsview/dragdroprobot/robot.qrc (limited to 'examples/widgets/graphicsview/dragdroprobot') diff --git a/examples/widgets/graphicsview/dragdroprobot/coloritem.cpp b/examples/widgets/graphicsview/dragdroprobot/coloritem.cpp new file mode 100644 index 0000000000..18e7a01af2 --- /dev/null +++ b/examples/widgets/graphicsview/dragdroprobot/coloritem.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "coloritem.h" + +//! [0] +ColorItem::ColorItem() + : color(qrand() % 256, qrand() % 256, qrand() % 256) +{ + setToolTip(QString("QColor(%1, %2, %3)\n%4") + .arg(color.red()).arg(color.green()).arg(color.blue()) + .arg("Click and drag this color onto the robot!")); + setCursor(Qt::OpenHandCursor); + setAcceptedMouseButtons(Qt::LeftButton); +} +//! [0] + +//! [1] +QRectF ColorItem::boundingRect() const +{ + return QRectF(-15.5, -15.5, 34, 34); +} +//! [1] + +//! [2] +void ColorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + painter->setPen(Qt::NoPen); + painter->setBrush(Qt::darkGray); + painter->drawEllipse(-12, -12, 30, 30); + painter->setPen(QPen(Qt::black, 1)); + painter->setBrush(QBrush(color)); + painter->drawEllipse(-15, -15, 30, 30); +} +//! [2] + +//! [3] +void ColorItem::mousePressEvent(QGraphicsSceneMouseEvent *) +{ + setCursor(Qt::ClosedHandCursor); +} +//! [3] + +//! [5] +void ColorItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)) + .length() < QApplication::startDragDistance()) { + return; + } + + QDrag *drag = new QDrag(event->widget()); + QMimeData *mime = new QMimeData; + drag->setMimeData(mime); +//! [5] + +//! [6] + static int n = 0; + if (n++ > 2 && (qrand() % 3) == 0) { + QImage image(":/images/head.png"); + mime->setImageData(image); + + drag->setPixmap(QPixmap::fromImage(image).scaled(30, 40)); + drag->setHotSpot(QPoint(15, 30)); +//! [6] +//! [7] + } else { + mime->setColorData(color); + mime->setText(QString("#%1%2%3") + .arg(color.red(), 2, 16, QLatin1Char('0')) + .arg(color.green(), 2, 16, QLatin1Char('0')) + .arg(color.blue(), 2, 16, QLatin1Char('0'))); + + QPixmap pixmap(34, 34); + pixmap.fill(Qt::white); + + QPainter painter(&pixmap); + painter.translate(15, 15); + painter.setRenderHint(QPainter::Antialiasing); + paint(&painter, 0, 0); + painter.end(); + + pixmap.setMask(pixmap.createHeuristicMask()); + + drag->setPixmap(pixmap); + drag->setHotSpot(QPoint(15, 20)); + } +//! [7] + +//! [8] + drag->exec(); + setCursor(Qt::OpenHandCursor); +} +//! [8] + +//! [4] +void ColorItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *) +{ + setCursor(Qt::OpenHandCursor); +} +//! [4] diff --git a/examples/widgets/graphicsview/dragdroprobot/coloritem.h b/examples/widgets/graphicsview/dragdroprobot/coloritem.h new file mode 100644 index 0000000000..9d3b4a9693 --- /dev/null +++ b/examples/widgets/graphicsview/dragdroprobot/coloritem.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef COLORITEM_H +#define COLORITEM_H + +#include + +//! [0] +class ColorItem : public QGraphicsItem +{ +public: + ColorItem(); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + +private: + QColor color; +}; +//! [0] + +#endif diff --git a/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.desktop b/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.desktop new file mode 100644 index 0000000000..c01e3a3425 --- /dev/null +++ b/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Drag and Drop Robot +Exec=/opt/usr/bin/dragdroprobot +Icon=dragdroprobot +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.pro b/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.pro new file mode 100644 index 0000000000..3e33bad2f9 --- /dev/null +++ b/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.pro @@ -0,0 +1,22 @@ +HEADERS += \ + coloritem.h \ + robot.h + +SOURCES += \ + coloritem.cpp \ + main.cpp \ + robot.cpp + +RESOURCES += \ + robot.qrc + + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/dragdroprobot +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS dragdroprobot.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/dragdroprobot +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/graphicsview/dragdroprobot/images/head.png b/examples/widgets/graphicsview/dragdroprobot/images/head.png new file mode 100644 index 0000000000..1e520e09b8 Binary files /dev/null and b/examples/widgets/graphicsview/dragdroprobot/images/head.png differ diff --git a/examples/widgets/graphicsview/dragdroprobot/main.cpp b/examples/widgets/graphicsview/dragdroprobot/main.cpp new file mode 100644 index 0000000000..05d3c8f1b3 --- /dev/null +++ b/examples/widgets/graphicsview/dragdroprobot/main.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "coloritem.h" +#include "robot.h" + +#include + +class GraphicsView : public QGraphicsView +{ +public: + GraphicsView(QGraphicsScene *scene) : QGraphicsView(scene) + { + } + +protected: + virtual void resizeEvent(QResizeEvent *) + { + } +}; + +//! [0] +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); +//! [0] +//! [1] + QGraphicsScene scene(-200, -200, 400, 400); + + for (int i = 0; i < 10; ++i) { + ColorItem *item = new ColorItem; + item->setPos(::sin((i * 6.28) / 10.0) * 150, + ::cos((i * 6.28) / 10.0) * 150); + + scene.addItem(item); + } + + Robot *robot = new Robot; + robot->setTransform(QTransform::fromScale(1.2, 1.2), true); + robot->setPos(0, -20); + scene.addItem(robot); +//! [1] +//! [2] + GraphicsView view(&scene); + view.setRenderHint(QPainter::Antialiasing); + view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); + view.setBackgroundBrush(QColor(230, 200, 167)); + view.setWindowTitle("Drag and Drop Robot"); + view.show(); + + return app.exec(); +} +//! [2] diff --git a/examples/widgets/graphicsview/dragdroprobot/robot.cpp b/examples/widgets/graphicsview/dragdroprobot/robot.cpp new file mode 100644 index 0000000000..20935bf6c5 --- /dev/null +++ b/examples/widgets/graphicsview/dragdroprobot/robot.cpp @@ -0,0 +1,307 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "robot.h" + +//! [0] +RobotPart::RobotPart(QGraphicsItem *parent) + : QGraphicsObject(parent), color(Qt::lightGray), dragOver(false) +{ + setAcceptDrops(true); +} +//! [0] + +//! [1] +void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasColor()) { + event->setAccepted(true); + dragOver = true; + update(); + } else { + event->setAccepted(false); + } +} +//! [1] + +//! [2] +void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) +{ + Q_UNUSED(event); + dragOver = false; + update(); +} +//! [2] + +//! [3] +void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event) +{ + dragOver = false; + if (event->mimeData()->hasColor()) + color = qvariant_cast(event->mimeData()->colorData()); + update(); +} +//! [3] + +//! [4] +RobotHead::RobotHead(QGraphicsItem *parent) + : RobotPart(parent) +{ +} +//! [4] + +//! [5] +QRectF RobotHead::boundingRect() const +{ + return QRectF(-15, -50, 30, 50); +} +//! [5] + +//! [6] +void RobotHead::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + if (pixmap.isNull()) { + painter->setBrush(dragOver ? color.light(130) : color); + painter->drawRoundedRect(-10, -30, 20, 30, 25, 25, Qt::RelativeSize); + painter->setBrush(Qt::white); + painter->drawEllipse(-7, -3 - 20, 7, 7); + painter->drawEllipse(0, -3 - 20, 7, 7); + painter->setBrush(Qt::black); + painter->drawEllipse(-5, -1 - 20, 2, 2); + painter->drawEllipse(2, -1 - 20, 2, 2); + painter->setPen(QPen(Qt::black, 2)); + painter->setBrush(Qt::NoBrush); + painter->drawArc(-6, -2 - 20, 12, 15, 190 * 16, 160 * 16); + } else { + painter->scale(.2272, .2824); + painter->drawPixmap(QPointF(-15 * 4.4, -50 * 3.54), pixmap); + } +} +//! [6] + +//! [7] +void RobotHead::dragEnterEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasImage()) { + event->setAccepted(true); + dragOver = true; + update(); + } else { + RobotPart::dragEnterEvent(event); + } +} +//! [7] + +//! [8] +void RobotHead::dropEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasImage()) { + dragOver = false; + pixmap = qvariant_cast(event->mimeData()->imageData()); + update(); + } else { + RobotPart::dropEvent(event); + } +} +//! [8] + +RobotTorso::RobotTorso(QGraphicsItem *parent) + : RobotPart(parent) +{ +} + +QRectF RobotTorso::boundingRect() const +{ + return QRectF(-30, -20, 60, 60); +} + +void RobotTorso::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + + painter->setBrush(dragOver ? color.light(130) : color); + painter->drawRoundedRect(-20, -20, 40, 60, 25, 25, Qt::RelativeSize); + painter->drawEllipse(-25, -20, 20, 20); + painter->drawEllipse(5, -20, 20, 20); + painter->drawEllipse(-20, 22, 20, 20); + painter->drawEllipse(0, 22, 20, 20); +} + +RobotLimb::RobotLimb(QGraphicsItem *parent) + : RobotPart(parent) +{ +} + +QRectF RobotLimb::boundingRect() const +{ + return QRectF(-5, -5, 40, 10); +} + +void RobotLimb::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + + painter->setBrush(dragOver ? color.light(130) : color); + painter->drawRoundedRect(boundingRect(), 50, 50, Qt::RelativeSize); + painter->drawEllipse(-5, -5, 10, 10); +} + +//! [10] +Robot::Robot(QGraphicsItem *parent) + : RobotPart(parent) +{ + setFlag(ItemHasNoContents); + + QGraphicsObject *torsoItem = new RobotTorso(this); + QGraphicsObject *headItem = new RobotHead(torsoItem); + QGraphicsObject *upperLeftArmItem = new RobotLimb(torsoItem); + QGraphicsObject *lowerLeftArmItem = new RobotLimb(upperLeftArmItem); + QGraphicsObject *upperRightArmItem = new RobotLimb(torsoItem); + QGraphicsObject *lowerRightArmItem = new RobotLimb(upperRightArmItem); + QGraphicsObject *upperRightLegItem = new RobotLimb(torsoItem); + QGraphicsObject *lowerRightLegItem = new RobotLimb(upperRightLegItem); + QGraphicsObject *upperLeftLegItem = new RobotLimb(torsoItem); + QGraphicsObject *lowerLeftLegItem = new RobotLimb(upperLeftLegItem); +//! [10] + +//! [11] + headItem->setPos(0, -18); + upperLeftArmItem->setPos(-15, -10); + lowerLeftArmItem->setPos(30, 0); + upperRightArmItem->setPos(15, -10); + lowerRightArmItem->setPos(30, 0); + upperRightLegItem->setPos(10, 32); + lowerRightLegItem->setPos(30, 0); + upperLeftLegItem->setPos(-10, 32); + lowerLeftLegItem->setPos(30, 0); +//! [11] + +//! [12] + QParallelAnimationGroup *animation = new QParallelAnimationGroup(this); + + QPropertyAnimation *headAnimation = new QPropertyAnimation(headItem, "rotation"); + headAnimation->setStartValue(20); + headAnimation->setEndValue(-20); + QPropertyAnimation *headScaleAnimation = new QPropertyAnimation(headItem, "scale"); + headScaleAnimation->setEndValue(1.1); + animation->addAnimation(headAnimation); + animation->addAnimation(headScaleAnimation); +//! [12] + + QPropertyAnimation *upperLeftArmAnimation = new QPropertyAnimation(upperLeftArmItem, "rotation"); + upperLeftArmAnimation->setStartValue(190); + upperLeftArmAnimation->setEndValue(180); + animation->addAnimation(upperLeftArmAnimation); + + QPropertyAnimation *lowerLeftArmAnimation = new QPropertyAnimation(lowerLeftArmItem, "rotation"); + lowerLeftArmAnimation->setStartValue(50); + lowerLeftArmAnimation->setEndValue(10); + animation->addAnimation(lowerLeftArmAnimation); + + QPropertyAnimation *upperRightArmAnimation = new QPropertyAnimation(upperRightArmItem, "rotation"); + upperRightArmAnimation->setStartValue(300); + upperRightArmAnimation->setEndValue(310); + animation->addAnimation(upperRightArmAnimation); + + QPropertyAnimation *lowerRightArmAnimation = new QPropertyAnimation(lowerRightArmItem, "rotation"); + lowerRightArmAnimation->setStartValue(0); + lowerRightArmAnimation->setEndValue(-70); + animation->addAnimation(lowerRightArmAnimation); + + QPropertyAnimation *upperLeftLegAnimation = new QPropertyAnimation(upperLeftLegItem, "rotation"); + upperLeftLegAnimation->setStartValue(150); + upperLeftLegAnimation->setEndValue(80); + animation->addAnimation(upperLeftLegAnimation); + + QPropertyAnimation *lowerLeftLegAnimation = new QPropertyAnimation(lowerLeftLegItem, "rotation"); + lowerLeftLegAnimation->setStartValue(70); + lowerLeftLegAnimation->setEndValue(10); + animation->addAnimation(lowerLeftLegAnimation); + + QPropertyAnimation *upperRightLegAnimation = new QPropertyAnimation(upperRightLegItem, "rotation"); + upperRightLegAnimation->setStartValue(40); + upperRightLegAnimation->setEndValue(120); + animation->addAnimation(upperRightLegAnimation); + + QPropertyAnimation *lowerRightLegAnimation = new QPropertyAnimation(lowerRightLegItem, "rotation"); + lowerRightLegAnimation->setStartValue(10); + lowerRightLegAnimation->setEndValue(50); + animation->addAnimation(lowerRightLegAnimation); + + QPropertyAnimation *torsoAnimation = new QPropertyAnimation(torsoItem, "rotation"); + torsoAnimation->setStartValue(5); + torsoAnimation->setEndValue(-20); + animation->addAnimation(torsoAnimation); + +//! [13] + for (int i = 0; i < animation->animationCount(); ++i) { + QPropertyAnimation *anim = qobject_cast(animation->animationAt(i)); + anim->setEasingCurve(QEasingCurve::SineCurve); + anim->setDuration(2000); + } + + animation->setLoopCount(-1); + animation->start(); +//! [13] +} + +//! [9] +QRectF Robot::boundingRect() const +{ + return QRectF(); +} + +void Robot::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(painter); + Q_UNUSED(option); + Q_UNUSED(widget); +} +//! [9] diff --git a/examples/widgets/graphicsview/dragdroprobot/robot.h b/examples/widgets/graphicsview/dragdroprobot/robot.h new file mode 100644 index 0000000000..ff9fb49485 --- /dev/null +++ b/examples/widgets/graphicsview/dragdroprobot/robot.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ROBOT_H +#define ROBOT_H + +#include + +QT_BEGIN_NAMESPACE +class QGraphicsSceneMouseEvent; +class QParallelAnimationGroup; +QT_END_NAMESPACE + +//! [0] +class RobotPart : public QGraphicsObject +{ +public: + RobotPart(QGraphicsItem *parent = 0); + +protected: + void dragEnterEvent(QGraphicsSceneDragDropEvent *event); + void dragLeaveEvent(QGraphicsSceneDragDropEvent *event); + void dropEvent(QGraphicsSceneDragDropEvent *event); + + QColor color; + bool dragOver; +}; +//! [0] + +//! [1] +class RobotHead : public RobotPart +{ +public: + RobotHead(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + +protected: + void dragEnterEvent(QGraphicsSceneDragDropEvent *event); + void dropEvent(QGraphicsSceneDragDropEvent *event); + +private: + QPixmap pixmap; +}; +//! [1] + +//! [2] +class RobotTorso : public RobotPart +{ +public: + RobotTorso(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; +//! [2] + +//! [3] +class RobotLimb : public RobotPart +{ +public: + RobotLimb(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; +//! [3] + +//! [4] +class Robot : public RobotPart +{ +public: + Robot(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; +//! [4] + +#endif diff --git a/examples/widgets/graphicsview/dragdroprobot/robot.qrc b/examples/widgets/graphicsview/dragdroprobot/robot.qrc new file mode 100644 index 0000000000..b0969d2a64 --- /dev/null +++ b/examples/widgets/graphicsview/dragdroprobot/robot.qrc @@ -0,0 +1,5 @@ + + + images/head.png + + -- cgit v1.2.3