]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/Edge.cxx
Pipeline editor added.
[cpPlugins.git] / appli / cpPipelineEditor / Edge.cxx
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 Edge::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     this->setToolTip( "Edge!!!" );
60     adjust();
61 }
62
63 Node *Edge::sourceNode() const
64 {
65     return source;
66 }
67
68 Node *Edge::destNode() const
69 {
70     return dest;
71 }
72
73 void Edge::adjust()
74 {
75     if (!source || !dest)
76         return;
77
78     QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
79     qreal length = line.length();
80
81     prepareGeometryChange();
82
83     if (length > qreal(20.)) {
84         QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
85         sourcePoint = line.p1() + edgeOffset;
86         destPoint = line.p2() - edgeOffset;
87     } else {
88         sourcePoint = destPoint = line.p1();
89     }
90 }
91
92 QRectF Edge::boundingRect() const
93 {
94     if (!source || !dest)
95         return QRectF();
96
97     qreal penWidth = 1;
98     qreal extra = (penWidth + arrowSize) / 2.0;
99
100     return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
101                                       destPoint.y() - sourcePoint.y()))
102         .normalized()
103         .adjusted(-extra, -extra, extra, extra);
104 }
105
106 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
107 {
108     if (!source || !dest)
109         return;
110
111     QLineF line(sourcePoint, destPoint);
112     if (qFuzzyCompare(line.length(), qreal(0.)))
113         return;
114
115     // Draw the line itself
116     painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
117     painter->drawLine(line);
118
119     // Draw the arrows
120     double angle = ::acos(line.dx() / line.length());
121     if (line.dy() >= 0)
122         angle = TwoPi - angle;
123
124     QPointF destArrowP1 = destPoint + QPointF(
125       sin(angle - Pi / 3) * arrowSize,
126       cos(angle - Pi / 3) * arrowSize
127       );
128     QPointF destArrowP2 = destPoint + QPointF(
129       sin(angle - Pi + Pi / 3) * arrowSize,
130       cos(angle - Pi + Pi / 3) * arrowSize
131       );
132     QPointF center = sourcePoint + destPoint;
133     center /= 2;
134
135     painter->setBrush(Qt::black);
136     painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
137     painter->drawText( center, "Edge!!!" );
138 }
139
140 // eof - $RCSfile$