1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "diagramitem.h"
52#include "arrow.h"
53
54#include <QGraphicsScene>
55#include <QGraphicsSceneContextMenuEvent>
56#include <QMenu>
57#include <QPainter>
58
59//! [0]
60DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
61 QGraphicsItem *parent)
62 : QGraphicsPolygonItem(parent), myDiagramType(diagramType)
63 , myContextMenu(contextMenu)
64{
65 QPainterPath path;
66 switch (myDiagramType) {
67 case StartEnd:
68 path.moveTo(200, 50);
69 path.arcTo(150, 0, 50, 50, 0, 90);
70 path.arcTo(50, 0, 50, 50, 90, 90);
71 path.arcTo(50, 50, 50, 50, 180, 90);
72 path.arcTo(150, 50, 50, 50, 270, 90);
73 path.lineTo(200, 25);
74 myPolygon = path.toFillPolygon();
75 break;
76 case Conditional:
77 myPolygon << QPointF(-100, 0) << QPointF(0, 100)
78 << QPointF(100, 0) << QPointF(0, -100)
79 << QPointF(-100, 0);
80 break;
81 case Step:
82 myPolygon << QPointF(-100, -100) << QPointF(100, -100)
83 << QPointF(100, 100) << QPointF(-100, 100)
84 << QPointF(-100, -100);
85 break;
86 default:
87 myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
88 << QPointF(120, 80) << QPointF(70, -80)
89 << QPointF(-120, -80);
90 break;
91 }
92 setPolygon(myPolygon);
93 setFlag(QGraphicsItem::ItemIsMovable, true);
94 setFlag(QGraphicsItem::ItemIsSelectable, true);
95 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
96}
97//! [0]
98
99//! [1]
100void DiagramItem::removeArrow(Arrow *arrow)
101{
102 arrows.removeAll(arrow);
103}
104//! [1]
105
106//! [2]
107void DiagramItem::removeArrows()
108{
109 // need a copy here since removeArrow() will
110 // modify the arrows container
111 const auto arrowsCopy = arrows;
112 for (Arrow *arrow : arrowsCopy) {
113 arrow->startItem()->removeArrow(arrow);
114 arrow->endItem()->removeArrow(arrow);
115 scene()->removeItem(arrow);
116 delete arrow;
117 }
118}
119//! [2]
120
121//! [3]
122void DiagramItem::addArrow(Arrow *arrow)
123{
124 arrows.append(arrow);
125}
126//! [3]
127
128//! [4]
129QPixmap DiagramItem::image() const
130{
131 QPixmap pixmap(250, 250);
132 pixmap.fill(Qt::transparent);
133 QPainter painter(&pixmap);
134 painter.setPen(QPen(Qt::black, 8));
135 painter.translate(125, 125);
136 painter.drawPolyline(myPolygon);
137
138 return pixmap;
139}
140//! [4]
141
142//! [5]
143void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
144{
145 scene()->clearSelection();
146 setSelected(true);
147 myContextMenu->exec(event->screenPos());
148}
149//! [5]
150
151//! [6]
152QVariant DiagramItem::itemChange(GraphicsItemChange change, const QVariant &value)
153{
154 if (change == QGraphicsItem::ItemPositionChange) {
155 for (Arrow *arrow : qAsConst(arrows))
156 arrow->updatePosition();
157 }
158
159 return value;
160}
161//! [6]
162