source: trunk/examples/graphicsview/elasticnodes/edge.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 4.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QPainter>
43
44#include "edge.h"
45#include "node.h"
46
47#include <math.h>
48
49static const double Pi = 3.14159265358979323846264338327950288419717;
50static double TwoPi = 2.0 * Pi;
51
52Edge::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
63Edge::~Edge()
64{
65}
66
67Node *Edge::sourceNode() const
68{
69 return source;
70}
71
72void Edge::setSourceNode(Node *node)
73{
74 source = node;
75 adjust();
76}
77
78Node *Edge::destNode() const
79{
80 return dest;
81}
82
83void Edge::setDestNode(Node *node)
84{
85 dest = node;
86 adjust();
87}
88
89void Edge::adjust()
90{
91 if (!source || !dest)
92 return;
93
94 QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
95 qreal length = line.length();
96
97 prepareGeometryChange();
98
99 if (length > qreal(20.)) {
100 QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
101 sourcePoint = line.p1() + edgeOffset;
102 destPoint = line.p2() - edgeOffset;
103 } else {
104 sourcePoint = destPoint = line.p1();
105 }
106}
107
108QRectF Edge::boundingRect() const
109{
110 if (!source || !dest)
111 return QRectF();
112
113 qreal penWidth = 1;
114 qreal extra = (penWidth + arrowSize) / 2.0;
115
116 return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
117 destPoint.y() - sourcePoint.y()))
118 .normalized()
119 .adjusted(-extra, -extra, extra, extra);
120}
121
122void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
123{
124 if (!source || !dest)
125 return;
126
127 QLineF line(sourcePoint, destPoint);
128 if (qFuzzyCompare(line.length(), qreal(0.)))
129 return;
130
131 // Draw the line itself
132 painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
133 painter->drawLine(line);
134
135 // Draw the arrows
136 double angle = ::acos(line.dx() / line.length());
137 if (line.dy() >= 0)
138 angle = TwoPi - angle;
139
140 QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize,
141 cos(angle + Pi / 3) * arrowSize);
142 QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
143 cos(angle + Pi - Pi / 3) * arrowSize);
144 QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
145 cos(angle - Pi / 3) * arrowSize);
146 QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
147 cos(angle - Pi + Pi / 3) * arrowSize);
148
149 painter->setBrush(Qt::black);
150 painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
151 painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
152}
Note: See TracBrowser for help on using the repository browser.