1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
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 <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]
|
---|
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]
|
---|
65 | Node *Edge::sourceNode() const
|
---|
66 | {
|
---|
67 | return source;
|
---|
68 | }
|
---|
69 |
|
---|
70 | Node *Edge::destNode() const
|
---|
71 | {
|
---|
72 | return dest;
|
---|
73 | }
|
---|
74 | //! [1]
|
---|
75 |
|
---|
76 | //! [2]
|
---|
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]
|
---|
98 | 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]
|
---|
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]
|
---|