| 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 "diagramscene.h" | 
|---|
| 45 | #include "arrow.h" | 
|---|
| 46 |  | 
|---|
| 47 | //! [0] | 
|---|
| 48 | DiagramScene::DiagramScene(QMenu *itemMenu, QObject *parent) | 
|---|
| 49 | : QGraphicsScene(parent) | 
|---|
| 50 | { | 
|---|
| 51 | myItemMenu = itemMenu; | 
|---|
| 52 | myMode = MoveItem; | 
|---|
| 53 | myItemType = DiagramItem::Step; | 
|---|
| 54 | line = 0; | 
|---|
| 55 | textItem = 0; | 
|---|
| 56 | myItemColor = Qt::white; | 
|---|
| 57 | myTextColor = Qt::black; | 
|---|
| 58 | myLineColor = Qt::black; | 
|---|
| 59 | } | 
|---|
| 60 | //! [0] | 
|---|
| 61 |  | 
|---|
| 62 | //! [1] | 
|---|
| 63 | void DiagramScene::setLineColor(const QColor &color) | 
|---|
| 64 | { | 
|---|
| 65 | myLineColor = color; | 
|---|
| 66 | if (isItemChange(Arrow::Type)) { | 
|---|
| 67 | Arrow *item = | 
|---|
| 68 | qgraphicsitem_cast<Arrow *>(selectedItems().first()); | 
|---|
| 69 | item->setColor(myLineColor); | 
|---|
| 70 | update(); | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|
| 73 | //! [1] | 
|---|
| 74 |  | 
|---|
| 75 | //! [2] | 
|---|
| 76 | void DiagramScene::setTextColor(const QColor &color) | 
|---|
| 77 | { | 
|---|
| 78 | myTextColor = color; | 
|---|
| 79 | if (isItemChange(DiagramTextItem::Type)) { | 
|---|
| 80 | DiagramTextItem *item = | 
|---|
| 81 | qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first()); | 
|---|
| 82 | item->setDefaultTextColor(myTextColor); | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 | //! [2] | 
|---|
| 86 |  | 
|---|
| 87 | //! [3] | 
|---|
| 88 | void DiagramScene::setItemColor(const QColor &color) | 
|---|
| 89 | { | 
|---|
| 90 | myItemColor = color; | 
|---|
| 91 | if (isItemChange(DiagramItem::Type)) { | 
|---|
| 92 | DiagramItem *item = | 
|---|
| 93 | qgraphicsitem_cast<DiagramItem *>(selectedItems().first()); | 
|---|
| 94 | item->setBrush(myItemColor); | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 | //! [3] | 
|---|
| 98 |  | 
|---|
| 99 | //! [4] | 
|---|
| 100 | void DiagramScene::setFont(const QFont &font) | 
|---|
| 101 | { | 
|---|
| 102 | myFont = font; | 
|---|
| 103 |  | 
|---|
| 104 | if (isItemChange(DiagramTextItem::Type)) { | 
|---|
| 105 | QGraphicsTextItem *item = | 
|---|
| 106 | qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first()); | 
|---|
| 107 | //At this point the selection can change so the first selected item might not be a DiagramTextItem | 
|---|
| 108 | if (item) | 
|---|
| 109 | item->setFont(myFont); | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 | //! [4] | 
|---|
| 113 |  | 
|---|
| 114 | void DiagramScene::setMode(Mode mode) | 
|---|
| 115 | { | 
|---|
| 116 | myMode = mode; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | void DiagramScene::setItemType(DiagramItem::DiagramType type) | 
|---|
| 120 | { | 
|---|
| 121 | myItemType = type; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | //! [5] | 
|---|
| 125 | void DiagramScene::editorLostFocus(DiagramTextItem *item) | 
|---|
| 126 | { | 
|---|
| 127 | QTextCursor cursor = item->textCursor(); | 
|---|
| 128 | cursor.clearSelection(); | 
|---|
| 129 | item->setTextCursor(cursor); | 
|---|
| 130 |  | 
|---|
| 131 | if (item->toPlainText().isEmpty()) { | 
|---|
| 132 | removeItem(item); | 
|---|
| 133 | item->deleteLater(); | 
|---|
| 134 | } | 
|---|
| 135 | } | 
|---|
| 136 | //! [5] | 
|---|
| 137 |  | 
|---|
| 138 | //! [6] | 
|---|
| 139 | void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) | 
|---|
| 140 | { | 
|---|
| 141 | if (mouseEvent->button() != Qt::LeftButton) | 
|---|
| 142 | return; | 
|---|
| 143 |  | 
|---|
| 144 | DiagramItem *item; | 
|---|
| 145 | switch (myMode) { | 
|---|
| 146 | case InsertItem: | 
|---|
| 147 | item = new DiagramItem(myItemType, myItemMenu); | 
|---|
| 148 | item->setBrush(myItemColor); | 
|---|
| 149 | addItem(item); | 
|---|
| 150 | item->setPos(mouseEvent->scenePos()); | 
|---|
| 151 | emit itemInserted(item); | 
|---|
| 152 | break; | 
|---|
| 153 | //! [6] //! [7] | 
|---|
| 154 | case InsertLine: | 
|---|
| 155 | line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(), | 
|---|
| 156 | mouseEvent->scenePos())); | 
|---|
| 157 | line->setPen(QPen(myLineColor, 2)); | 
|---|
| 158 | addItem(line); | 
|---|
| 159 | break; | 
|---|
| 160 | //! [7] //! [8] | 
|---|
| 161 | case InsertText: | 
|---|
| 162 | textItem = new DiagramTextItem(); | 
|---|
| 163 | textItem->setFont(myFont); | 
|---|
| 164 | textItem->setTextInteractionFlags(Qt::TextEditorInteraction); | 
|---|
| 165 | textItem->setZValue(1000.0); | 
|---|
| 166 | connect(textItem, SIGNAL(lostFocus(DiagramTextItem *)), | 
|---|
| 167 | this, SLOT(editorLostFocus(DiagramTextItem *))); | 
|---|
| 168 | connect(textItem, SIGNAL(selectedChange(QGraphicsItem *)), | 
|---|
| 169 | this, SIGNAL(itemSelected(QGraphicsItem *))); | 
|---|
| 170 | addItem(textItem); | 
|---|
| 171 | textItem->setDefaultTextColor(myTextColor); | 
|---|
| 172 | textItem->setPos(mouseEvent->scenePos()); | 
|---|
| 173 | emit textInserted(textItem); | 
|---|
| 174 | //! [8] //! [9] | 
|---|
| 175 | default: | 
|---|
| 176 | ; | 
|---|
| 177 | } | 
|---|
| 178 | QGraphicsScene::mousePressEvent(mouseEvent); | 
|---|
| 179 | } | 
|---|
| 180 | //! [9] | 
|---|
| 181 |  | 
|---|
| 182 | //! [10] | 
|---|
| 183 | void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) | 
|---|
| 184 | { | 
|---|
| 185 | if (myMode == InsertLine && line != 0) { | 
|---|
| 186 | QLineF newLine(line->line().p1(), mouseEvent->scenePos()); | 
|---|
| 187 | line->setLine(newLine); | 
|---|
| 188 | } else if (myMode == MoveItem) { | 
|---|
| 189 | QGraphicsScene::mouseMoveEvent(mouseEvent); | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|
| 192 | //! [10] | 
|---|
| 193 |  | 
|---|
| 194 | //! [11] | 
|---|
| 195 | void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) | 
|---|
| 196 | { | 
|---|
| 197 | if (line != 0 && myMode == InsertLine) { | 
|---|
| 198 | QList<QGraphicsItem *> startItems = items(line->line().p1()); | 
|---|
| 199 | if (startItems.count() && startItems.first() == line) | 
|---|
| 200 | startItems.removeFirst(); | 
|---|
| 201 | QList<QGraphicsItem *> endItems = items(line->line().p2()); | 
|---|
| 202 | if (endItems.count() && endItems.first() == line) | 
|---|
| 203 | endItems.removeFirst(); | 
|---|
| 204 |  | 
|---|
| 205 | removeItem(line); | 
|---|
| 206 | delete line; | 
|---|
| 207 | //! [11] //! [12] | 
|---|
| 208 |  | 
|---|
| 209 | if (startItems.count() > 0 && endItems.count() > 0 && | 
|---|
| 210 | startItems.first()->type() == DiagramItem::Type && | 
|---|
| 211 | endItems.first()->type() == DiagramItem::Type && | 
|---|
| 212 | startItems.first() != endItems.first()) { | 
|---|
| 213 | DiagramItem *startItem = | 
|---|
| 214 | qgraphicsitem_cast<DiagramItem *>(startItems.first()); | 
|---|
| 215 | DiagramItem *endItem = | 
|---|
| 216 | qgraphicsitem_cast<DiagramItem *>(endItems.first()); | 
|---|
| 217 | Arrow *arrow = new Arrow(startItem, endItem); | 
|---|
| 218 | arrow->setColor(myLineColor); | 
|---|
| 219 | startItem->addArrow(arrow); | 
|---|
| 220 | endItem->addArrow(arrow); | 
|---|
| 221 | arrow->setZValue(-1000.0); | 
|---|
| 222 | addItem(arrow); | 
|---|
| 223 | arrow->updatePosition(); | 
|---|
| 224 | } | 
|---|
| 225 | } | 
|---|
| 226 | //! [12] //! [13] | 
|---|
| 227 | line = 0; | 
|---|
| 228 | QGraphicsScene::mouseReleaseEvent(mouseEvent); | 
|---|
| 229 | } | 
|---|
| 230 | //! [13] | 
|---|
| 231 |  | 
|---|
| 232 | //! [14] | 
|---|
| 233 | bool DiagramScene::isItemChange(int type) | 
|---|
| 234 | { | 
|---|
| 235 | foreach (QGraphicsItem *item, selectedItems()) { | 
|---|
| 236 | if (item->type() == type) | 
|---|
| 237 | return true; | 
|---|
| 238 | } | 
|---|
| 239 | return false; | 
|---|
| 240 | } | 
|---|
| 241 | //! [14] | 
|---|