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 <QGraphicsScene>
|
|
43 |
#include <QGraphicsSceneMouseEvent>
|
|
44 |
#include <QPainter>
|
|
45 |
#include <QStyleOption>
|
|
46 |
|
|
47 |
#include "edge.h"
|
|
48 |
#include "node.h"
|
|
49 |
#include "graphwidget.h"
|
|
50 |
|
|
51 |
Node::Node(GraphWidget *graphWidget)
|
|
52 |
: graph(graphWidget)
|
|
53 |
{
|
|
54 |
setFlag(ItemIsMovable);
|
|
55 |
setFlag(ItemSendsGeometryChanges);
|
|
56 |
setCacheMode(DeviceCoordinateCache);
|
|
57 |
setZValue(-1);
|
|
58 |
}
|
|
59 |
|
|
60 |
void Node::addEdge(Edge *edge)
|
|
61 |
{
|
|
62 |
edgeList << edge;
|
|
63 |
edge->adjust();
|
|
64 |
}
|
|
65 |
|
|
66 |
QList<Edge *> Node::edges() const
|
|
67 |
{
|
|
68 |
return edgeList;
|
|
69 |
}
|
|
70 |
|
|
71 |
void Node::calculateForces()
|
|
72 |
{
|
|
73 |
if (!scene() || scene()->mouseGrabberItem() == this) {
|
|
74 |
newPos = pos();
|
|
75 |
return;
|
|
76 |
}
|
|
77 |
|
|
78 |
// Sum up all forces pushing this item away
|
|
79 |
qreal xvel = 0;
|
|
80 |
qreal yvel = 0;
|
|
81 |
foreach (QGraphicsItem *item, scene()->items()) {
|
|
82 |
Node *node = qgraphicsitem_cast<Node *>(item);
|
|
83 |
if (!node)
|
|
84 |
continue;
|
|
85 |
|
|
86 |
QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0));
|
|
87 |
qreal dx = line.dx();
|
|
88 |
qreal dy = line.dy();
|
|
89 |
double l = 2.0 * (dx * dx + dy * dy);
|
|
90 |
if (l > 0) {
|
|
91 |
xvel += (dx * 150.0) / l;
|
|
92 |
yvel += (dy * 150.0) / l;
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
// Now subtract all forces pulling items together
|
|
97 |
double weight = (edgeList.size() + 1) * 10;
|
|
98 |
foreach (Edge *edge, edgeList) {
|
|
99 |
QPointF pos;
|
|
100 |
if (edge->sourceNode() == this)
|
|
101 |
pos = mapFromItem(edge->destNode(), 0, 0);
|
|
102 |
else
|
|
103 |
pos = mapFromItem(edge->sourceNode(), 0, 0);
|
|
104 |
xvel += pos.x() / weight;
|
|
105 |
yvel += pos.y() / weight;
|
|
106 |
}
|
|
107 |
|
|
108 |
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
|
|
109 |
xvel = yvel = 0;
|
|
110 |
|
|
111 |
QRectF sceneRect = scene()->sceneRect();
|
|
112 |
newPos = pos() + QPointF(xvel, yvel);
|
|
113 |
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
|
|
114 |
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
|
|
115 |
}
|
|
116 |
|
|
117 |
bool Node::advance()
|
|
118 |
{
|
|
119 |
if (newPos == pos())
|
|
120 |
return false;
|
|
121 |
|
|
122 |
setPos(newPos);
|
|
123 |
return true;
|
|
124 |
}
|
|
125 |
|
|
126 |
QRectF Node::boundingRect() const
|
|
127 |
{
|
|
128 |
qreal adjust = 2;
|
|
129 |
return QRectF(-10 - adjust, -10 - adjust,
|
|
130 |
23 + adjust, 23 + adjust);
|
|
131 |
}
|
|
132 |
|
|
133 |
QPainterPath Node::shape() const
|
|
134 |
{
|
|
135 |
QPainterPath path;
|
|
136 |
path.addEllipse(-10, -10, 20, 20);
|
|
137 |
return path;
|
|
138 |
}
|
|
139 |
|
|
140 |
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
|
141 |
{
|
|
142 |
painter->setPen(Qt::NoPen);
|
|
143 |
painter->setBrush(Qt::darkGray);
|
|
144 |
painter->drawEllipse(-7, -7, 20, 20);
|
|
145 |
|
|
146 |
QRadialGradient gradient(-3, -3, 10);
|
|
147 |
if (option->state & QStyle::State_Sunken) {
|
|
148 |
gradient.setCenter(3, 3);
|
|
149 |
gradient.setFocalPoint(3, 3);
|
|
150 |
gradient.setColorAt(1, QColor(Qt::yellow).light(120));
|
|
151 |
gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
|
|
152 |
} else {
|
|
153 |
gradient.setColorAt(0, Qt::yellow);
|
|
154 |
gradient.setColorAt(1, Qt::darkYellow);
|
|
155 |
}
|
|
156 |
painter->setBrush(gradient);
|
|
157 |
painter->setPen(QPen(Qt::black, 0));
|
|
158 |
painter->drawEllipse(-10, -10, 20, 20);
|
|
159 |
}
|
|
160 |
|
|
161 |
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
162 |
{
|
|
163 |
switch (change) {
|
|
164 |
case ItemPositionHasChanged:
|
|
165 |
foreach (Edge *edge, edgeList)
|
|
166 |
edge->adjust();
|
|
167 |
graph->itemMoved();
|
|
168 |
break;
|
|
169 |
default:
|
|
170 |
break;
|
|
171 |
};
|
|
172 |
|
|
173 |
return QGraphicsItem::itemChange(change, value);
|
|
174 |
}
|
|
175 |
|
|
176 |
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
177 |
{
|
|
178 |
update();
|
|
179 |
QGraphicsItem::mousePressEvent(event);
|
|
180 |
}
|
|
181 |
|
|
182 |
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
183 |
{
|
|
184 |
update();
|
|
185 |
QGraphicsItem::mouseReleaseEvent(event);
|
|
186 |
}
|