source: trunk/examples/graphicsview/diagramscene/diagramitem.cpp@ 385

Last change on this file since 385 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
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 [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "diagramitem.h"
45#include "arrow.h"
46
47//! [0]
48DiagramItem::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]
89void 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]
99void 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]
111void DiagramItem::addArrow(Arrow *arrow)
112{
113 arrows.append(arrow);
114}
115//! [3]
116
117//! [4]
118QPixmap 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]
132void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
133{
134 scene()->clearSelection();
135 setSelected(true);
136 myContextMenu->exec(event->screenPos());
137}
138//! [5]
139
140//! [6]
141QVariant 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]
Note: See TracBrowser for help on using the repository browser.