0029559: Samples - wrong copyright statement in FuncDemo
[occt.git] / samples / qt / FuncDemo / src / edge.cpp
CommitLineData
aff5997d 1/****************************************************************************
2**
967905a3 3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
aff5997d 5**
967905a3 6** This file is part of the examples of the Qt Toolkit.
aff5997d 7**
967905a3 8** $QT_BEGIN_LICENSE:BSD$
9** You may use this file under the terms of the BSD license as follows:
aff5997d 10**
967905a3 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 Digia Plc and its Subsidiary(-ies) nor the names
21** of its contributors may be used to endorse or promote products derived
22** from this software without specific prior written permission.
aff5997d 23**
967905a3 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$
aff5997d 38**
39****************************************************************************/
40
41#include <QPainter>
42
43#include "edge.h"
44#include "node.h"
45
46#include <math.h>
47
48static const double Pi = 3.14159265358979323846264338327950288419717;
49static double TwoPi = 2.0 * Pi;
50
51Edge::Edge(Node *sourceNode, Node *destNode)
52 : arrowSize(10)
53{
54 setAcceptedMouseButtons(0);
55 source = sourceNode;
56 dest = destNode;
57 source->addEdge(this);
58 dest->addEdge(this);
59 adjust();
60}
61
aff5997d 62Node *Edge::sourceNode() const
63{
64 return source;
65}
66
aff5997d 67Node *Edge::destNode() const
68{
69 return dest;
70}
71
aff5997d 72void Edge::adjust()
73{
74 if (!source || !dest)
75 return;
76
77 QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
78 qreal length = line.length();
aff5997d 79
80 prepareGeometryChange();
967905a3 81
82 if (length > qreal(20.)) {
83 QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
84 sourcePoint = line.p1() + edgeOffset;
85 destPoint = line.p2() - edgeOffset;
86 } else {
87 sourcePoint = destPoint = line.p1();
88 }
aff5997d 89}
90
91QRectF Edge::boundingRect() const
92{
93 if (!source || !dest)
94 return QRectF();
95
96 qreal penWidth = 1;
97 qreal extra = (penWidth + arrowSize) / 2.0;
98
99 return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
100 destPoint.y() - sourcePoint.y()))
101 .normalized()
102 .adjusted(-extra, -extra, extra, extra);
103}
104
105void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
106{
107 if (!source || !dest)
108 return;
109
967905a3 110 QLineF line(sourcePoint, destPoint);
111 if (qFuzzyCompare(line.length(), qreal(0.)))
112 return;
aff5997d 113
114 // Draw the line itself
aff5997d 115 painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
116 painter->drawLine(line);
117
967905a3 118 // Draw the arrows
aff5997d 119 double angle = ::acos(line.dx() / line.length());
120 if (line.dy() >= 0)
121 angle = TwoPi - angle;
122
967905a3 123 QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize,
124 cos(angle + Pi / 3) * arrowSize);
125 QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
126 cos(angle + Pi - Pi / 3) * arrowSize);
aff5997d 127 QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
128 cos(angle - Pi / 3) * arrowSize);
129 QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
130 cos(angle - Pi + Pi / 3) * arrowSize);
131
132 painter->setBrush(Qt::black);
967905a3 133 painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
134 painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
aff5997d 135}