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 <QPainter>
42 
43 #include "edge.h"
44 #include "node.h"
45 
46 #include <math.h>
47 
48 static const double Pi = 3.14159265358979323846264338327950288419717;
49 static double TwoPi = 2.0 * Pi;
50 
51 //! [0]
Edge(Node * sourceNode,Node * destNode)52 Edge::Edge(Node *sourceNode, Node *destNode)
53     : arrowSize(10)
54 {
55     setAcceptedMouseButtons(0);
56     source = sourceNode;
57     dest = destNode;
58     source->addEdge(this);
59     dest->addEdge(this);
60     adjust();
61 }
62 //! [0]
63 
64 //! [1]
sourceNode() const65 Node *Edge::sourceNode() const
66 {
67     return source;
68 }
69 
destNode() const70 Node *Edge::destNode() const
71 {
72     return dest;
73 }
74 //! [1]
75 
76 //! [2]
adjust()77 void Edge::adjust()
78 {
79     if (!source || !dest)
80         return;
81 
82     QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
83     qreal length = line.length();
84 
85     prepareGeometryChange();
86 
87     if (length > qreal(20.)) {
88         QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
89         sourcePoint = line.p1() + edgeOffset;
90         destPoint = line.p2() - edgeOffset;
91     } else {
92         sourcePoint = destPoint = line.p1();
93     }
94 }
95 //! [2]
96 
97 //! [3]
boundingRect() const98 QRectF Edge::boundingRect() const
99 {
100     if (!source || !dest)
101         return QRectF();
102 
103     qreal penWidth = 1;
104     qreal extra = (penWidth + arrowSize) / 2.0;
105 
106     return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
107                                       destPoint.y() - sourcePoint.y()))
108         .normalized()
109         .adjusted(-extra, -extra, extra, extra);
110 }
111 //! [3]
112 
113 //! [4]
paint(QPainter * painter,const QStyleOptionGraphicsItem *,QWidget *)114 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
115 {
116     if (!source || !dest)
117         return;
118 
119     QLineF line(sourcePoint, destPoint);
120     if (qFuzzyCompare(line.length(), qreal(0.)))
121         return;
122 //! [4]
123 
124 //! [5]
125     // Draw the line itself
126     painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
127     painter->drawLine(line);
128 //! [5]
129 
130 //! [6]
131     // Draw the arrows
132     double angle = ::acos(line.dx() / line.length());
133     if (line.dy() >= 0)
134         angle = TwoPi - angle;
135 
136     QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize,
137                                                   cos(angle + Pi / 3) * arrowSize);
138     QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
139                                                   cos(angle + Pi - Pi / 3) * arrowSize);
140     QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
141                                               cos(angle - Pi / 3) * arrowSize);
142     QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
143                                               cos(angle - Pi + Pi / 3) * arrowSize);
144 
145     painter->setBrush(Qt::black);
146     painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
147     painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
148 }
149 //! [6]
150