Fix more NaNs in Elastic Nodes example
[qt-netbsd.git] / examples / graphicsview / elasticnodes / edge.cpp
blobeb021433e50e619fe45dc211fbeefe89eb709bf4
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** No Commercial Usage
10 ** This file contains pre-release code and may not be distributed.
11 ** You may use this file in accordance with the terms and conditions
12 ** contained in the either Technology Preview License Agreement or the
13 ** Beta Release License Agreement.
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
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.
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include <QPainter>
44 #include "edge.h"
45 #include "node.h"
47 #include <math.h>
49 static const double Pi = 3.14159265358979323846264338327950288419717;
50 static double TwoPi = 2.0 * Pi;
52 Edge::Edge(Node *sourceNode, Node *destNode)
53 : arrowSize(10)
55 setAcceptedMouseButtons(0);
56 source = sourceNode;
57 dest = destNode;
58 source->addEdge(this);
59 dest->addEdge(this);
60 adjust();
63 Edge::~Edge()
67 Node *Edge::sourceNode() const
69 return source;
72 void Edge::setSourceNode(Node *node)
74 source = node;
75 adjust();
78 Node *Edge::destNode() const
80 return dest;
83 void Edge::setDestNode(Node *node)
85 dest = node;
86 adjust();
89 void Edge::adjust()
91 if (!source || !dest)
92 return;
94 QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
95 qreal length = line.length();
97 prepareGeometryChange();
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();
108 QRectF Edge::boundingRect() const
110 if (!source || !dest)
111 return QRectF();
113 qreal penWidth = 1;
114 qreal extra = (penWidth + arrowSize) / 2.0;
116 return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
117 destPoint.y() - sourcePoint.y()))
118 .normalized()
119 .adjusted(-extra, -extra, extra, extra);
122 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
124 if (!source || !dest)
125 return;
127 QLineF line(sourcePoint, destPoint);
128 if (qFuzzyCompare(line.length(), qreal(0.)))
129 return;
131 // Draw the line itself
132 painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
133 painter->drawLine(line);
135 // Draw the arrows
136 double angle = ::acos(line.dx() / line.length());
137 if (line.dy() >= 0)
138 angle = TwoPi - angle;
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);
149 painter->setBrush(Qt::black);
150 painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
151 painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);