1 /****************************************************************************
3 ** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
5 ** This file is part of the example classes of the Qt Toolkit.
7 ** Licensees holding a valid Qt License Agreement may use this file in
8 ** accordance with the rights, responsibilities and obligations
9 ** contained therein. Please consult your licensing agreement or
10 ** contact sales@trolltech.com if any conditions of this licensing
11 ** agreement are not clear to you.
13 ** Further information about Qt licensing is available at:
14 ** http://www.trolltech.com/products/qt/licensing.html or by
15 ** contacting info@trolltech.com.
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 ****************************************************************************/
29 static const double Pi = 3.14159265358979323846264338327950288419717;
30 static double TwoPi = 2.0 * Pi;
32 Edge::Edge(Node *sourceNode, Node *destNode)
35 setAcceptedMouseButtons(0);
38 source->addEdge(this);
47 Node *Edge::sourceNode() const
52 void Edge::setSourceNode(Node *node)
58 Node *Edge::destNode() const
63 void Edge::setDestNode(Node *node)
74 QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
75 qreal length = line.length();
76 QPointF edgeOffset((line.dx() * 15) / length, (line.dy() * 15) / length);
78 prepareGeometryChange();
79 sourcePoint = line.p1() + edgeOffset;
80 destPoint = line.p2() - edgeOffset;
83 QRectF Edge::boundingRect() const
89 qreal extra = (penWidth + arrowSize) / 2.0;
91 return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
92 destPoint.y() - sourcePoint.y()))
94 .adjusted(-extra, -extra, extra, extra);
97 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
104 // Draw the line itself
105 QLineF line(sourcePoint, destPoint);
106 painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
107 painter->drawLine(line);
109 // Draw the arrows if there's enough room
110 double angle = ::acos(line.dx() / line.length());
112 angle = TwoPi - angle;
114 QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
115 cos(angle - Pi / 3) * arrowSize);
116 QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
117 cos(angle - Pi + Pi / 3) * arrowSize);
119 painter->setBrush(Qt::black);
120 painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);