1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QtGui>
42
43 #include "arrow.h"
44 #include <math.h>
45
46 const qreal Pi = 3.14;
47
48 //! [0]
Arrow(DiagramItem * startItem,DiagramItem * endItem,QGraphicsItem * parent,QGraphicsScene * scene)49 Arrow::Arrow(DiagramItem *startItem, DiagramItem *endItem,
50 QGraphicsItem *parent, QGraphicsScene *scene)
51 : QGraphicsLineItem(parent, scene)
52 {
53 myStartItem = startItem;
54 myEndItem = endItem;
55 setFlag(QGraphicsItem::ItemIsSelectable, true);
56 myColor = Qt::black;
57 setPen(QPen(myColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
58 }
59 //! [0]
60
61 //! [1]
boundingRect() const62 QRectF Arrow::boundingRect() const
63 {
64 qreal extra = (pen().width() + 20) / 2.0;
65
66 return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(),
67 line().p2().y() - line().p1().y()))
68 .normalized()
69 .adjusted(-extra, -extra, extra, extra);
70 }
71 //! [1]
72
73 //! [2]
shape() const74 QPainterPath Arrow::shape() const
75 {
76 QPainterPath path = QGraphicsLineItem::shape();
77 path.addPolygon(arrowHead);
78 return path;
79 }
80 //! [2]
81
82 //! [3]
updatePosition()83 void Arrow::updatePosition()
84 {
85 QLineF line(mapFromItem(myStartItem, 0, 0), mapFromItem(myEndItem, 0, 0));
86 setLine(line);
87 }
88 //! [3]
89
90 //! [4]
paint(QPainter * painter,const QStyleOptionGraphicsItem *,QWidget *)91 void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
92 QWidget *)
93 {
94 if (myStartItem->collidesWithItem(myEndItem))
95 return;
96
97 QPen myPen = pen();
98 myPen.setColor(myColor);
99 qreal arrowSize = 20;
100 painter->setPen(myPen);
101 painter->setBrush(myColor);
102 //! [4] //! [5]
103
104 QLineF centerLine(myStartItem->pos(), myEndItem->pos());
105 QPolygonF endPolygon = myEndItem->polygon();
106 QPointF p1 = endPolygon.first() + myEndItem->pos();
107 QPointF p2;
108 QPointF intersectPoint;
109 QLineF polyLine;
110 for (int i = 1; i < endPolygon.count(); ++i) {
111 p2 = endPolygon.at(i) + myEndItem->pos();
112 polyLine = QLineF(p1, p2);
113 QLineF::IntersectType intersectType =
114 polyLine.intersect(centerLine, &intersectPoint);
115 if (intersectType == QLineF::BoundedIntersection)
116 break;
117 p1 = p2;
118 }
119
120 setLine(QLineF(intersectPoint, myStartItem->pos()));
121 //! [5] //! [6]
122
123 double angle = ::acos(line().dx() / line().length());
124 if (line().dy() >= 0)
125 angle = (Pi * 2) - angle;
126
127 QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize,
128 cos(angle + Pi / 3) * arrowSize);
129 QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
130 cos(angle + Pi - Pi / 3) * arrowSize);
131
132 arrowHead.clear();
133 arrowHead << line().p1() << arrowP1 << arrowP2;
134 //! [6] //! [7]
135 painter->drawLine(line());
136 painter->drawPolygon(arrowHead);
137 if (isSelected()) {
138 painter->setPen(QPen(myColor, 1, Qt::DashLine));
139 QLineF myLine = line();
140 myLine.translate(0, 4.0);
141 painter->drawLine(myLine);
142 myLine.translate(0,-8.0);
143 painter->drawLine(myLine);
144 }
145 }
146 //! [7]
147