0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 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 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
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 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "graphwidget.h"
|
|
43 |
#include "edge.h"
|
|
44 |
#include "node.h"
|
|
45 |
|
|
46 |
#include <QDebug>
|
|
47 |
#include <QGraphicsScene>
|
|
48 |
#include <QWheelEvent>
|
|
49 |
|
|
50 |
#include <math.h>
|
|
51 |
|
|
52 |
GraphWidget::GraphWidget()
|
|
53 |
: timerId(0)
|
|
54 |
{
|
|
55 |
QGraphicsScene *scene = new QGraphicsScene(this);
|
|
56 |
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
|
57 |
scene->setSceneRect(-200, -200, 400, 400);
|
|
58 |
setScene(scene);
|
|
59 |
setCacheMode(CacheBackground);
|
|
60 |
setViewportUpdateMode(BoundingRectViewportUpdate);
|
|
61 |
setRenderHint(QPainter::Antialiasing);
|
|
62 |
setTransformationAnchor(AnchorUnderMouse);
|
|
63 |
setResizeAnchor(AnchorViewCenter);
|
|
64 |
|
|
65 |
Node *node1 = new Node(this);
|
|
66 |
Node *node2 = new Node(this);
|
|
67 |
Node *node3 = new Node(this);
|
|
68 |
Node *node4 = new Node(this);
|
|
69 |
centerNode = new Node(this);
|
|
70 |
Node *node6 = new Node(this);
|
|
71 |
Node *node7 = new Node(this);
|
|
72 |
Node *node8 = new Node(this);
|
|
73 |
Node *node9 = new Node(this);
|
|
74 |
scene->addItem(node1);
|
|
75 |
scene->addItem(node2);
|
|
76 |
scene->addItem(node3);
|
|
77 |
scene->addItem(node4);
|
|
78 |
scene->addItem(centerNode);
|
|
79 |
scene->addItem(node6);
|
|
80 |
scene->addItem(node7);
|
|
81 |
scene->addItem(node8);
|
|
82 |
scene->addItem(node9);
|
|
83 |
scene->addItem(new Edge(node1, node2));
|
|
84 |
scene->addItem(new Edge(node2, node3));
|
|
85 |
scene->addItem(new Edge(node2, centerNode));
|
|
86 |
scene->addItem(new Edge(node3, node6));
|
|
87 |
scene->addItem(new Edge(node4, node1));
|
|
88 |
scene->addItem(new Edge(node4, centerNode));
|
|
89 |
scene->addItem(new Edge(centerNode, node6));
|
|
90 |
scene->addItem(new Edge(centerNode, node8));
|
|
91 |
scene->addItem(new Edge(node6, node9));
|
|
92 |
scene->addItem(new Edge(node7, node4));
|
|
93 |
scene->addItem(new Edge(node8, node7));
|
|
94 |
scene->addItem(new Edge(node9, node8));
|
|
95 |
|
|
96 |
node1->setPos(-50, -50);
|
|
97 |
node2->setPos(0, -50);
|
|
98 |
node3->setPos(50, -50);
|
|
99 |
node4->setPos(-50, 0);
|
|
100 |
centerNode->setPos(0, 0);
|
|
101 |
node6->setPos(50, 0);
|
|
102 |
node7->setPos(-50, 50);
|
|
103 |
node8->setPos(0, 50);
|
|
104 |
node9->setPos(50, 50);
|
|
105 |
|
|
106 |
scale(qreal(0.8), qreal(0.8));
|
|
107 |
setMinimumSize(400, 400);
|
|
108 |
setWindowTitle(tr("Elastic Nodes"));
|
|
109 |
}
|
|
110 |
|
|
111 |
void GraphWidget::itemMoved()
|
|
112 |
{
|
|
113 |
if (!timerId)
|
|
114 |
timerId = startTimer(1000 / 25);
|
|
115 |
}
|
|
116 |
|
|
117 |
void GraphWidget::keyPressEvent(QKeyEvent *event)
|
|
118 |
{
|
|
119 |
switch (event->key()) {
|
|
120 |
case Qt::Key_Up:
|
|
121 |
centerNode->moveBy(0, -20);
|
|
122 |
break;
|
|
123 |
case Qt::Key_Down:
|
|
124 |
centerNode->moveBy(0, 20);
|
|
125 |
break;
|
|
126 |
case Qt::Key_Left:
|
|
127 |
centerNode->moveBy(-20, 0);
|
|
128 |
break;
|
|
129 |
case Qt::Key_Right:
|
|
130 |
centerNode->moveBy(20, 0);
|
|
131 |
break;
|
|
132 |
case Qt::Key_Plus:
|
|
133 |
scaleView(qreal(1.2));
|
|
134 |
break;
|
|
135 |
case Qt::Key_Minus:
|
|
136 |
scaleView(1 / qreal(1.2));
|
|
137 |
break;
|
|
138 |
case Qt::Key_Space:
|
|
139 |
case Qt::Key_Enter:
|
|
140 |
foreach (QGraphicsItem *item, scene()->items()) {
|
|
141 |
if (qgraphicsitem_cast<Node *>(item))
|
|
142 |
item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
|
|
143 |
}
|
|
144 |
break;
|
|
145 |
default:
|
|
146 |
QGraphicsView::keyPressEvent(event);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
void GraphWidget::timerEvent(QTimerEvent *event)
|
|
151 |
{
|
|
152 |
Q_UNUSED(event);
|
|
153 |
|
|
154 |
QList<Node *> nodes;
|
|
155 |
foreach (QGraphicsItem *item, scene()->items()) {
|
|
156 |
if (Node *node = qgraphicsitem_cast<Node *>(item))
|
|
157 |
nodes << node;
|
|
158 |
}
|
|
159 |
|
|
160 |
foreach (Node *node, nodes)
|
|
161 |
node->calculateForces();
|
|
162 |
|
|
163 |
bool itemsMoved = false;
|
|
164 |
foreach (Node *node, nodes) {
|
|
165 |
if (node->advance())
|
|
166 |
itemsMoved = true;
|
|
167 |
}
|
|
168 |
|
|
169 |
if (!itemsMoved) {
|
|
170 |
killTimer(timerId);
|
|
171 |
timerId = 0;
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
void GraphWidget::wheelEvent(QWheelEvent *event)
|
|
176 |
{
|
|
177 |
scaleView(pow((double)2, -event->delta() / 240.0));
|
|
178 |
}
|
|
179 |
|
|
180 |
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
|
|
181 |
{
|
|
182 |
Q_UNUSED(rect);
|
|
183 |
|
|
184 |
// Shadow
|
|
185 |
QRectF sceneRect = this->sceneRect();
|
|
186 |
QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
|
|
187 |
QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
|
|
188 |
if (rightShadow.intersects(rect) || rightShadow.contains(rect))
|
|
189 |
painter->fillRect(rightShadow, Qt::darkGray);
|
|
190 |
if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
|
|
191 |
painter->fillRect(bottomShadow, Qt::darkGray);
|
|
192 |
|
|
193 |
// Fill
|
|
194 |
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
|
|
195 |
gradient.setColorAt(0, Qt::white);
|
|
196 |
gradient.setColorAt(1, Qt::lightGray);
|
|
197 |
painter->fillRect(rect.intersect(sceneRect), gradient);
|
|
198 |
painter->setBrush(Qt::NoBrush);
|
|
199 |
painter->drawRect(sceneRect);
|
|
200 |
|
|
201 |
// Text
|
|
202 |
QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
|
|
203 |
sceneRect.width() - 4, sceneRect.height() - 4);
|
|
204 |
QString message(tr("Click and drag the nodes around, and zoom with the mouse "
|
|
205 |
"wheel or the '+' and '-' keys"));
|
|
206 |
|
|
207 |
QFont font = painter->font();
|
|
208 |
font.setBold(true);
|
|
209 |
font.setPointSize(14);
|
|
210 |
painter->setFont(font);
|
|
211 |
painter->setPen(Qt::lightGray);
|
|
212 |
painter->drawText(textRect.translated(2, 2), message);
|
|
213 |
painter->setPen(Qt::black);
|
|
214 |
painter->drawText(textRect, message);
|
|
215 |
}
|
|
216 |
|
|
217 |
void GraphWidget::scaleView(qreal scaleFactor)
|
|
218 |
{
|
|
219 |
qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
|
|
220 |
if (factor < 0.07 || factor > 100)
|
|
221 |
return;
|
|
222 |
|
|
223 |
scale(scaleFactor, scaleFactor);
|
|
224 |
}
|