| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 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: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 qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "diagramitem.h"
|
|---|
| 45 | #include "arrow.h"
|
|---|
| 46 |
|
|---|
| 47 | //! [0]
|
|---|
| 48 | DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
|
|---|
| 49 | QGraphicsItem *parent, QGraphicsScene *scene)
|
|---|
| 50 | : QGraphicsPolygonItem(parent, scene)
|
|---|
| 51 | {
|
|---|
| 52 | myDiagramType = diagramType;
|
|---|
| 53 | myContextMenu = contextMenu;
|
|---|
| 54 |
|
|---|
| 55 | QPainterPath path;
|
|---|
| 56 | switch (myDiagramType) {
|
|---|
| 57 | case StartEnd:
|
|---|
| 58 | path.moveTo(200, 50);
|
|---|
| 59 | path.arcTo(150, 0, 50, 50, 0, 90);
|
|---|
| 60 | path.arcTo(50, 0, 50, 50, 90, 90);
|
|---|
| 61 | path.arcTo(50, 50, 50, 50, 180, 90);
|
|---|
| 62 | path.arcTo(150, 50, 50, 50, 270, 90);
|
|---|
| 63 | path.lineTo(200, 25);
|
|---|
| 64 | myPolygon = path.toFillPolygon();
|
|---|
| 65 | break;
|
|---|
| 66 | case Conditional:
|
|---|
| 67 | myPolygon << QPointF(-100, 0) << QPointF(0, 100)
|
|---|
| 68 | << QPointF(100, 0) << QPointF(0, -100)
|
|---|
| 69 | << QPointF(-100, 0);
|
|---|
| 70 | break;
|
|---|
| 71 | case Step:
|
|---|
| 72 | myPolygon << QPointF(-100, -100) << QPointF(100, -100)
|
|---|
| 73 | << QPointF(100, 100) << QPointF(-100, 100)
|
|---|
| 74 | << QPointF(-100, -100);
|
|---|
| 75 | break;
|
|---|
| 76 | default:
|
|---|
| 77 | myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
|
|---|
| 78 | << QPointF(120, 80) << QPointF(70, -80)
|
|---|
| 79 | << QPointF(-120, -80);
|
|---|
| 80 | break;
|
|---|
| 81 | }
|
|---|
| 82 | setPolygon(myPolygon);
|
|---|
| 83 | setFlag(QGraphicsItem::ItemIsMovable, true);
|
|---|
| 84 | setFlag(QGraphicsItem::ItemIsSelectable, true);
|
|---|
| 85 | }
|
|---|
| 86 | //! [0]
|
|---|
| 87 |
|
|---|
| 88 | //! [1]
|
|---|
| 89 | void DiagramItem::removeArrow(Arrow *arrow)
|
|---|
| 90 | {
|
|---|
| 91 | int index = arrows.indexOf(arrow);
|
|---|
| 92 |
|
|---|
| 93 | if (index != -1)
|
|---|
| 94 | arrows.removeAt(index);
|
|---|
| 95 | }
|
|---|
| 96 | //! [1]
|
|---|
| 97 |
|
|---|
| 98 | //! [2]
|
|---|
| 99 | void DiagramItem::removeArrows()
|
|---|
| 100 | {
|
|---|
| 101 | foreach (Arrow *arrow, arrows) {
|
|---|
| 102 | arrow->startItem()->removeArrow(arrow);
|
|---|
| 103 | arrow->endItem()->removeArrow(arrow);
|
|---|
| 104 | scene()->removeItem(arrow);
|
|---|
| 105 | delete arrow;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | //! [2]
|
|---|
| 109 |
|
|---|
| 110 | //! [3]
|
|---|
| 111 | void DiagramItem::addArrow(Arrow *arrow)
|
|---|
| 112 | {
|
|---|
| 113 | arrows.append(arrow);
|
|---|
| 114 | }
|
|---|
| 115 | //! [3]
|
|---|
| 116 |
|
|---|
| 117 | //! [4]
|
|---|
| 118 | QPixmap DiagramItem::image() const
|
|---|
| 119 | {
|
|---|
| 120 | QPixmap pixmap(250, 250);
|
|---|
| 121 | pixmap.fill(Qt::transparent);
|
|---|
| 122 | QPainter painter(&pixmap);
|
|---|
| 123 | painter.setPen(QPen(Qt::black, 8));
|
|---|
| 124 | painter.translate(125, 125);
|
|---|
| 125 | painter.drawPolyline(myPolygon);
|
|---|
| 126 |
|
|---|
| 127 | return pixmap;
|
|---|
| 128 | }
|
|---|
| 129 | //! [4]
|
|---|
| 130 |
|
|---|
| 131 | //! [5]
|
|---|
| 132 | void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|---|
| 133 | {
|
|---|
| 134 | scene()->clearSelection();
|
|---|
| 135 | setSelected(true);
|
|---|
| 136 | myContextMenu->exec(event->screenPos());
|
|---|
| 137 | }
|
|---|
| 138 | //! [5]
|
|---|
| 139 |
|
|---|
| 140 | //! [6]
|
|---|
| 141 | QVariant DiagramItem::itemChange(GraphicsItemChange change,
|
|---|
| 142 | const QVariant &value)
|
|---|
| 143 | {
|
|---|
| 144 | if (change == QGraphicsItem::ItemPositionChange) {
|
|---|
| 145 | foreach (Arrow *arrow, arrows) {
|
|---|
| 146 | arrow->updatePosition();
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | return value;
|
|---|
| 151 | }
|
|---|
| 152 | //! [6]
|
|---|