| 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 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 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. | 
|---|
| 22 | ** | 
|---|
| 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. | 
|---|
| 27 | ** | 
|---|
| 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. | 
|---|
| 35 | ** | 
|---|
| 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$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QPainter> | 
|---|
| 43 |  | 
|---|
| 44 | #include "edge.h" | 
|---|
| 45 | #include "node.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include <math.h> | 
|---|
| 48 |  | 
|---|
| 49 | static const double Pi = 3.14159265358979323846264338327950288419717; | 
|---|
| 50 | static double TwoPi = 2.0 * Pi; | 
|---|
| 51 |  | 
|---|
| 52 | Edge::Edge(Node *sourceNode, Node *destNode) | 
|---|
| 53 | : arrowSize(10) | 
|---|
| 54 | { | 
|---|
| 55 | setAcceptedMouseButtons(0); | 
|---|
| 56 | source = sourceNode; | 
|---|
| 57 | dest = destNode; | 
|---|
| 58 | source->addEdge(this); | 
|---|
| 59 | dest->addEdge(this); | 
|---|
| 60 | adjust(); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | Edge::~Edge() | 
|---|
| 64 | { | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | Node *Edge::sourceNode() const | 
|---|
| 68 | { | 
|---|
| 69 | return source; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void Edge::setSourceNode(Node *node) | 
|---|
| 73 | { | 
|---|
| 74 | source = node; | 
|---|
| 75 | adjust(); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | Node *Edge::destNode() const | 
|---|
| 79 | { | 
|---|
| 80 | return dest; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void Edge::setDestNode(Node *node) | 
|---|
| 84 | { | 
|---|
| 85 | dest = node; | 
|---|
| 86 | adjust(); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | void Edge::adjust() | 
|---|
| 90 | { | 
|---|
| 91 | if (!source || !dest) | 
|---|
| 92 | return; | 
|---|
| 93 |  | 
|---|
| 94 | QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0)); | 
|---|
| 95 | qreal length = line.length(); | 
|---|
| 96 |  | 
|---|
| 97 | prepareGeometryChange(); | 
|---|
| 98 |  | 
|---|
| 99 | if (!qFuzzyCompare(length, qreal(0.0))) { | 
|---|
| 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(); | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | QRectF Edge::boundingRect() const | 
|---|
| 109 | { | 
|---|
| 110 | if (!source || !dest) | 
|---|
| 111 | return QRectF(); | 
|---|
| 112 |  | 
|---|
| 113 | qreal penWidth = 1; | 
|---|
| 114 | qreal extra = (penWidth + arrowSize) / 2.0; | 
|---|
| 115 |  | 
|---|
| 116 | return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(), | 
|---|
| 117 | destPoint.y() - sourcePoint.y())) | 
|---|
| 118 | .normalized() | 
|---|
| 119 | .adjusted(-extra, -extra, extra, extra); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) | 
|---|
| 123 | { | 
|---|
| 124 | if (!source || !dest) | 
|---|
| 125 | return; | 
|---|
| 126 |  | 
|---|
| 127 | // Draw the line itself | 
|---|
| 128 | QLineF line(sourcePoint, destPoint); | 
|---|
| 129 | painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); | 
|---|
| 130 | painter->drawLine(line); | 
|---|
| 131 |  | 
|---|
| 132 | // Draw the arrows if there's enough room | 
|---|
| 133 | double angle = ::acos(line.dx() / line.length()); | 
|---|
| 134 | if (line.dy() >= 0) | 
|---|
| 135 | angle = TwoPi - angle; | 
|---|
| 136 |  | 
|---|
| 137 | QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize, | 
|---|
| 138 | cos(angle + Pi / 3) * arrowSize); | 
|---|
| 139 | QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, | 
|---|
| 140 | cos(angle + Pi - Pi / 3) * arrowSize); | 
|---|
| 141 | QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize, | 
|---|
| 142 | cos(angle - Pi / 3) * arrowSize); | 
|---|
| 143 | QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, | 
|---|
| 144 | cos(angle - Pi + Pi / 3) * arrowSize); | 
|---|
| 145 |  | 
|---|
| 146 | painter->setBrush(Qt::black); | 
|---|
| 147 | painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2); | 
|---|
| 148 | painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); | 
|---|
| 149 | } | 
|---|