comparison examples/graphicsview/elasticnodes/edge.d @ 371:12f60887ed15

add elasticnodes example and necessary changes to the library.
author Eldar Insafutdinov
date Wed, 07 Jul 2010 22:54:12 +0100
parents
children
comparison
equal deleted inserted replaced
370:7fd4b69378bf 371:12f60887ed15
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial Usage
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 module edge;
42
43 import std.math;
44 import qt.gui.QGraphicsItem;
45
46 import node;
47
48 static const double Pi = 3.14159265358979323846264338327950288419717;
49 static double TwoPi = 2.0 * Pi;
50
51 class Edge : QGraphicsItem
52 {
53 public:
54 this(Node sourceNode, Node destNode)
55 {
56 arrowSize = 10;
57 setAcceptedMouseButtons(0);
58 source = sourceNode;
59 dest = destNode;
60 source.addEdge(this);
61 dest.addEdge(this);
62 adjust();
63 }
64
65 Node sourceNode() const
66 {
67 return cast(Node)source;
68 }
69
70 void setSourceNode(Node node)
71 {
72 source = node;
73 adjust();
74 }
75
76 Node destNode() const
77 {
78 return cast(Node)dest;
79 }
80
81 void setDestNode(Node node)
82 {
83 dest = node;
84 adjust();
85 }
86
87 void adjust()
88 {
89 if (!source || !dest)
90 return;
91
92 auto line = QLineF(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
93 qreal length = line.length();
94
95 prepareGeometryChange();
96
97 if (length > 20.) {
98 auto edgeOffset = QPointF((line.dx() * 10) / length, (line.dy() * 10) / length);
99 sourcePoint = line.p1() + edgeOffset;
100 destPoint = line.p2() - edgeOffset;
101 } else {
102 sourcePoint = destPoint = line.p1();
103 }
104 }
105
106 enum { Type = UserType + 2 };
107 int type() const { return Type; }
108
109 protected:
110 override QRectF boundingRect() const
111 {
112 if (!source || !dest)
113 return QRectF();
114
115 qreal penWidth = 1;
116 qreal extra = (penWidth + arrowSize) / 2.0;
117
118 return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
119 destPoint.y() - sourcePoint.y()))
120 .normalized()
121 .adjusted(-extra, -extra, extra, extra);
122 }
123
124 override void paint(QPainter painter, QStyleOptionGraphicsItem option, QWidget widget)
125 {
126 if (!source || !dest)
127 return;
128
129 auto line = QLineF(sourcePoint, destPoint);
130 if (qFuzzyCompare(line.length(), 0.))
131 return;
132
133 // Draw the line itself
134 painter.setPen(new QPen(new QBrush(Qt.black), 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin));
135 painter.drawLine(line);
136
137 // Draw the arrows
138 double angle = .acos(line.dx() / line.length());
139 if (line.dy() >= 0)
140 angle = TwoPi - angle;
141
142 QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize,
143 cos(angle + Pi / 3) * arrowSize);
144 QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
145 cos(angle + Pi - Pi / 3) * arrowSize);
146 QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
147 cos(angle - Pi / 3) * arrowSize);
148 QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
149 cos(angle - Pi + Pi / 3) * arrowSize);
150
151 painter.setBrush(new QBrush(Qt.black));
152 auto p1 = new QPolygonF();
153 p1.append(line.p1());
154 p1.append(sourceArrowP1);
155 p1.append(sourceArrowP2);
156 painter.drawPolygon(p1);
157 auto p2 = new QPolygonF();
158 p2.append(line.p2());
159 p2.append(destArrowP1);
160 p2.append(destArrowP2);
161 painter.drawPolygon(p2);
162 }
163
164 private:
165 Node source, dest;
166
167 QPointF sourcePoint;
168 QPointF destPoint;
169 qreal arrowSize;
170 };
171