annotate examples/graphicsview/elasticnodes/graphwidget.d @ 374:681af90e1d53

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