source: trunk/examples/graphicsview/diagramscene/diagramscene.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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