1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
10 ** This file contains pre-release code and may not be distributed.
11 ** You may use this file in accordance with the terms and conditions
12 ** contained in the either Technology Preview License Agreement or the
13 ** Beta Release License Agreement.
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.
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
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.
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
40 ****************************************************************************/
49 static const double Pi
= 3.14159265358979323846264338327950288419717;
50 static double TwoPi
= 2.0 * Pi
;
52 Edge::Edge(Node
*sourceNode
, Node
*destNode
)
55 setAcceptedMouseButtons(0);
58 source
->addEdge(this);
67 Node
*Edge::sourceNode() const
72 void Edge::setSourceNode(Node
*node
)
78 Node
*Edge::destNode() const
83 void Edge::setDestNode(Node
*node
)
94 QLineF
line(mapFromItem(source
, 0, 0), mapFromItem(dest
, 0, 0));
95 qreal length
= line
.length();
97 prepareGeometryChange();
99 if (length
> qreal(20.)) {
100 QPointF
edgeOffset((line
.dx() * 10) / length
, (line
.dy() * 10) / length
);
101 sourcePoint
= line
.p1() + edgeOffset
;
102 destPoint
= line
.p2() - edgeOffset
;
104 sourcePoint
= destPoint
= line
.p1();
108 QRectF
Edge::boundingRect() const
110 if (!source
|| !dest
)
114 qreal extra
= (penWidth
+ arrowSize
) / 2.0;
116 return QRectF(sourcePoint
, QSizeF(destPoint
.x() - sourcePoint
.x(),
117 destPoint
.y() - sourcePoint
.y()))
119 .adjusted(-extra
, -extra
, extra
, extra
);
122 void Edge::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*, QWidget
*)
124 if (!source
|| !dest
)
127 QLineF
line(sourcePoint
, destPoint
);
128 if (qFuzzyCompare(line
.length(), qreal(0.)))
131 // Draw the line itself
132 painter
->setPen(QPen(Qt::black
, 1, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
));
133 painter
->drawLine(line
);
136 double angle
= ::acos(line
.dx() / line
.length());
138 angle
= TwoPi
- angle
;
140 QPointF sourceArrowP1
= sourcePoint
+ QPointF(sin(angle
+ Pi
/ 3) * arrowSize
,
141 cos(angle
+ Pi
/ 3) * arrowSize
);
142 QPointF sourceArrowP2
= sourcePoint
+ QPointF(sin(angle
+ Pi
- Pi
/ 3) * arrowSize
,
143 cos(angle
+ Pi
- Pi
/ 3) * arrowSize
);
144 QPointF destArrowP1
= destPoint
+ QPointF(sin(angle
- Pi
/ 3) * arrowSize
,
145 cos(angle
- Pi
/ 3) * arrowSize
);
146 QPointF destArrowP2
= destPoint
+ QPointF(sin(angle
- Pi
+ Pi
/ 3) * arrowSize
,
147 cos(angle
- Pi
+ Pi
/ 3) * arrowSize
);
149 painter
->setBrush(Qt::black
);
150 painter
->drawPolygon(QPolygonF() << line
.p1() << sourceArrowP1
<< sourceArrowP2
);
151 painter
->drawPolygon(QPolygonF() << line
.p2() << destArrowP1
<< destArrowP2
);