1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
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.
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."
39 ****************************************************************************/
48 static const double Pi = 3.14159265358979323846264338327950288419717;
49 static double TwoPi = 2.0 * Pi;
51 Edge::Edge(Node *sourceNode, Node *destNode)
54 setAcceptedMouseButtons(0);
57 source->addEdge(this);
59 this->setToolTip( "Edge!!!" );
63 Node *Edge::sourceNode() const
68 Node *Edge::destNode() const
78 QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
79 qreal length = line.length();
81 prepareGeometryChange();
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;
88 sourcePoint = destPoint = line.p1();
92 QRectF Edge::boundingRect() const
98 qreal extra = (penWidth + arrowSize) / 2.0;
100 return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
101 destPoint.y() - sourcePoint.y()))
103 .adjusted(-extra, -extra, extra, extra);
106 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
108 if (!source || !dest)
111 QLineF line(sourcePoint, destPoint);
112 if (qFuzzyCompare(line.length(), qreal(0.)))
115 // Draw the line itself
116 painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
117 painter->drawLine(line);
120 double angle = ::acos(line.dx() / line.length());
122 angle = TwoPi - angle;
124 QPointF destArrowP1 = destPoint + QPointF(
125 sin(angle - Pi / 3) * arrowSize,
126 cos(angle - Pi / 3) * arrowSize
128 QPointF destArrowP2 = destPoint + QPointF(
129 sin(angle - Pi + Pi / 3) * arrowSize,
130 cos(angle - Pi + Pi / 3) * arrowSize
132 QPointF center = sourcePoint + destPoint;
135 painter->setBrush(Qt::black);
136 painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
137 painter->drawText( center, "Edge!!!" );