comparison examples/graphicsview/elasticnodes/graphwidget.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
42 import
43 std.math;
44
45 import
46 qt.gui.QLinearGradient,
47 qt.gui.QGraphicsView,
48 qt.gui.QGraphicsScene;
49
50 import
51 edge,
52 node;
53
54 class GraphWidget : QGraphicsView
55 {
56 public:
57 this()
58 {
59 auto scene = new QGraphicsScene(this);
60 scene.setItemIndexMethod(QGraphicsScene.NoIndex);
61 scene.setSceneRect(-200, -200, 400, 400);
62 setScene(scene);
63 setCacheMode(CacheBackground);
64 setViewportUpdateMode(BoundingRectViewportUpdate);
65 setRenderHint(QPainter.Antialiasing);
66 setTransformationAnchor(AnchorUnderMouse);
67 setResizeAnchor(AnchorViewCenter);
68
69 Node node1 = new Node(this);
70 Node node2 = new Node(this);
71 Node node3 = new Node(this);
72 Node node4 = new Node(this);
73 centerNode = new Node(this);
74 Node node6 = new Node(this);
75 Node node7 = new Node(this);
76 Node node8 = new Node(this);
77 Node node9 = new Node(this);
78 scene.addItem(node1);
79 scene.addItem(node2);
80 scene.addItem(node3);
81 scene.addItem(node4);
82 scene.addItem(centerNode);
83 scene.addItem(node6);
84 scene.addItem(node7);
85 scene.addItem(node8);
86 scene.addItem(node9);
87 scene.addItem(new Edge(node1, node2));
88 scene.addItem(new Edge(node2, node3));
89 scene.addItem(new Edge(node2, centerNode));
90 scene.addItem(new Edge(node3, node6));
91 scene.addItem(new Edge(node4, node1));
92 scene.addItem(new Edge(node4, centerNode));
93 scene.addItem(new Edge(centerNode, node6));
94 scene.addItem(new Edge(centerNode, node8));
95 scene.addItem(new Edge(node6, node9));
96 scene.addItem(new Edge(node7, node4));
97 scene.addItem(new Edge(node8, node7));
98 scene.addItem(new Edge(node9, node8));
99
100 node1.setPos(-50, -50);
101 node2.setPos(0, -50);
102 node3.setPos(50, -50);
103 node4.setPos(-50, 0);
104 centerNode.setPos(0, 0);
105 node6.setPos(50, 0);
106 node7.setPos(-50, 50);
107 node8.setPos(0, 50);
108 node9.setPos(50, 50);
109
110 scale(0.8, 0.8);
111 setMinimumSize(400, 400);
112 setWindowTitle(tr("Elastic Nodes"));
113 }
114
115 void itemMoved()
116 {
117 if (!timerId)
118 timerId = startTimer(1000 / 25);
119 }
120
121 protected:
122 void keyPressEvent(QKeyEvent event)
123 {
124 switch (event.key()) {
125 case Qt.Key_Up:
126 centerNode.moveBy(0, -20);
127 break;
128 case Qt.Key_Down:
129 centerNode.moveBy(0, 20);
130 break;
131 case Qt.Key_Left:
132 centerNode.moveBy(-20, 0);
133 break;
134 case Qt.Key_Right:
135 centerNode.moveBy(20, 0);
136 break;
137 case Qt.Key_Plus:
138 scaleView(1.2);
139 break;
140 case Qt.Key_Minus:
141 scaleView(1 / 1.2);
142 break;
143 case Qt.Key_Space:
144 case Qt.Key_Enter:
145 foreach (IQGraphicsItem item; scene().items()) {
146 if (cast(Node)item)
147 item.setPos(-150 + qrand() % 300, -150 + qrand() % 300);
148 }
149 break;
150 default:
151 super.keyPressEvent(event);
152 }
153 }
154
155 void timerEvent(QTimerEvent event)
156 {
157 Node[] nodes;
158 foreach (IQGraphicsItem item; scene().items()) {
159 if (auto node = cast(Node)item)
160 nodes ~= node;
161 }
162
163 foreach (Node node; nodes)
164 node.calculateForces();
165
166 bool itemsMoved = false;
167 foreach (Node node; nodes) {
168 if (node.advance())
169 itemsMoved = true;
170 }
171
172 if (!itemsMoved) {
173 killTimer(timerId);
174 timerId = 0;
175 }
176 }
177
178 void wheelEvent(QWheelEvent event)
179 {
180 scaleView(pow(cast(float)2, cast(float)(-event.delta() / 240.0)));
181 }
182
183 void drawBackground(QPainter painter, const QRectF rect)
184 {
185 // Shadow
186 QRectF sceneRect = this.sceneRect();
187 auto rightShadow = QRectF(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
188 auto bottomShadow = QRectF(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
189 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
190 painter.fillRect(rightShadow, Qt.darkGray);
191 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
192 painter.fillRect(bottomShadow, Qt.darkGray);
193
194 // Fill
195 auto gradient = new QLinearGradient(sceneRect.topLeft(), sceneRect.bottomRight());
196 gradient.setColorAt(0, new QColor(Qt.white));
197 gradient.setColorAt(1, new QColor(Qt.lightGray));
198 painter.fillRect(rect.intersected(sceneRect), new QBrush(gradient));
199 painter.setBrush(Qt.NoBrush);
200 painter.drawRect(sceneRect);
201
202 // Text
203 auto textRect = QRectF(sceneRect.left() + 4, sceneRect.top() + 4,
204 sceneRect.width() - 4, sceneRect.height() - 4);
205 string message = tr("Click and drag the nodes around, and zoom with the mouse "
206 "wheel or the '+' and '-' keys");
207
208 QFont font = painter.font();
209 font.setBold(true);
210 font.setPointSize(14);
211 painter.setFont(font);
212 painter.setPen(new QColor(Qt.lightGray));
213 painter.drawText(textRect.translated(2, 2), message);
214 painter.setPen(new QColor(Qt.black));
215 painter.drawText(textRect, message);
216 }
217
218 void scaleView(qreal scaleFactor)
219 {
220 qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
221 if (factor < 0.07 || factor > 100)
222 return;
223
224 scale(scaleFactor, scaleFactor);
225 }
226
227 private:
228 int timerId;
229 Node centerNode;
230
231 mixin Q_OBJECT;
232 };