annotate examples/graphicsview/elasticnodes/node.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
371
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
1 /****************************************************************************
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
2 **
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
4 ** All rights reserved.
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
6 **
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
7 ** This file is part of the examples of the Qt Toolkit.
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
8 **
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
9 ** $QT_BEGIN_LICENSE:LGPL$
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
10 ** Commercial Usage
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
12 ** accordance with the Qt Commercial License Agreement provided with the
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
13 ** Software or, alternatively, in accordance with the terms contained in
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
14 ** a written agreement between you and Nokia.
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
15 **
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
16 ** GNU Lesser General Public License Usage
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
18 ** General Public License version 2.1 as published by the Free Software
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
20 ** packaging of this file. Please review the following information to
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
23 **
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
24 ** In addition, as a special exception, Nokia gives you certain additional
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
27 **
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
28 ** GNU General Public License Usage
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
29 ** Alternatively, this file may be used under the terms of the GNU
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
30 ** General Public License version 3.0 as published by the Free Software
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
31 ** Foundation and appearing in the file LICENSE.GPL included in the
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
32 ** packaging of this file. Please review the following information to
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
33 ** ensure the GNU General Public License version 3.0 requirements will be
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
34 ** met: http://www.gnu.org/copyleft/gpl.html.
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
35 **
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
36 ** If you have questions regarding the use of this file, please contact
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
37 ** Nokia at qt-info@nokia.com.
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
38 ** $QT_END_LICENSE$
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
39 **
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
40 ****************************************************************************/
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
41
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
42 module node;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
43
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
44 import
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
45 qt.gui.QRadialGradient,
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
46 qt.gui.QGraphicsItem;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
47
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
48 import
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
49 graphwidget,
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
50 edge;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
51
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
52 class Node : QGraphicsItem
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
53 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
54 public:
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
55 this(GraphWidget graphWidget)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
56 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
57 graph = graphWidget;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
58 setFlag(ItemIsMovable);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
59 setFlag(ItemSendsGeometryChanges);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
60 setCacheMode(DeviceCoordinateCache);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
61 setZValue(-1);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
62 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
63
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
64 void addEdge(Edge edge)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
65 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
66 edgeList ~= edge;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
67 edge.adjust();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
68 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
69 /*
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
70 Edge[] edges() const
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
71 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
72 return edgeList;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
73 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
74 */
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
75 enum { Type = UserType + 1 };
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
76 int type() const { return Type; }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
77
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
78 void calculateForces()
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
79 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
80 if (!scene() || scene().mouseGrabberItem() is this) {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
81 newPos = pos();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
82 return;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
83 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
84
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
85 // Sum up all forces pushing this item away
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
86 qreal xvel = 0;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
87 qreal yvel = 0;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
88 foreach (IQGraphicsItem item; scene().items()) {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
89 auto node = cast(Node)item;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
90 if (!node)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
91 continue;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
92
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
93 auto line = QLineF(mapFromItem(node, 0, 0), QPointF(0, 0));
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
94 qreal dx = line.dx();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
95 qreal dy = line.dy();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
96 double l = 2.0 * (dx * dx + dy * dy);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
97 if (l > 0) {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
98 xvel += (dx * 150.0) / l;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
99 yvel += (dy * 150.0) / l;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
100 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
101 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
102
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
103 // Now subtract all forces pulling items together
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
104 double weight = (edgeList.length + 1) * 10;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
105 foreach (Edge edge; edgeList) {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
106 QPointF pos;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
107 if (edge.sourceNode() == this)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
108 pos = mapFromItem(edge.destNode(), 0, 0);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
109 else
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
110 pos = mapFromItem(edge.sourceNode(), 0, 0);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
111 xvel += pos.x() / weight;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
112 yvel += pos.y() / weight;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
113 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
114
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
115 if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
116 xvel = yvel = 0;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
117
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
118 QRectF sceneRect = scene().sceneRect();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
119 newPos = pos() + QPointF(xvel, yvel);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
120 newPos.x = qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
121 newPos.y = qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
122
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
123 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
124
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
125 bool advance()
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
126 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
127 auto pos_ = pos();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
128 if (newPos == pos_)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
129 return false;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
130
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
131 setPos(newPos);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
132 return true;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
133 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
134
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
135 QRectF boundingRect() const
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
136 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
137 qreal adjust = 2;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
138 return QRectF(-10 - adjust, -10 - adjust,
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
139 23 + adjust, 23 + adjust);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
140 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
141
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
142 QPainterPath shape() const
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
143 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
144 QPainterPath path;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
145 path.addEllipse(-10, -10, 20, 20);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
146 return path;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
147 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
148
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
149 override public void paint(QPainter painter, QStyleOptionGraphicsItem option, QWidget widget = null)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
150 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
151 painter.setPen(Qt.NoPen);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
152 painter.setBrush(new QBrush(Qt.darkGray));
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
153 painter.drawEllipse(-7, -7, 20, 20);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
154
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
155 auto gradient = new QRadialGradient(-3, -3, 10);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
156 if (option.state & QStyle.State_Sunken) {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
157 gradient.setCenter(3, 3);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
158 gradient.setFocalPoint(3, 3);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
159 gradient.setColorAt(1, (new QColor(Qt.yellow)).lighter(120));
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
160 gradient.setColorAt(0, (new QColor(Qt.darkYellow)).lighter(120));
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
161 } else {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
162 gradient.setColorAt(0, new QColor(Qt.yellow));
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
163 gradient.setColorAt(1, new QColor(Qt.darkYellow));
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
164 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
165 painter.setBrush(gradient);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
166 painter.setPen(new QPen(new QBrush(Qt.black), 0));
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
167 painter.drawEllipse(-10, -10, 20, 20);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
168 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
169
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
170 protected:
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
171 override QVariant itemChange(GraphicsItemChange change, QVariant value)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
172 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
173 switch (change)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
174 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
175 case ItemPositionHasChanged:
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
176 foreach (Edge edge; edgeList)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
177 edge.adjust();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
178 graph.itemMoved();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
179 break;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
180 default:
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
181 break;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
182 };
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
183
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
184 return QGraphicsItem.itemChange(change, value);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
185 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
186
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
187 override void mousePressEvent(QGraphicsSceneMouseEvent event)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
188 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
189 update();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
190 QGraphicsItem.mousePressEvent(event);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
191 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
192
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
193 override void mouseReleaseEvent(QGraphicsSceneMouseEvent event)
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
194 {
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
195 update();
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
196 QGraphicsItem.mouseReleaseEvent(event);
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
197 }
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
198
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
199 private:
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
200 Edge[] edgeList;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
201 QPointF newPos;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
202 GraphWidget graph;
12f60887ed15 add elasticnodes example and necessary changes to the library.
Eldar Insafutdinov
parents:
diff changeset
203 };