-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
281 lines (242 loc) · 7.98 KB
/
node.cpp
File metadata and controls
281 lines (242 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "edge.h"
#include "node.h"
#include "graphwidget.h"
#include "TreeMapWindow.h"
#include "TextureCubeManager.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QStyleOption>
//Node constructor
Widget::Node::Node(GraphWidget *graphWidget, TextureCubeManager * textureCubeManager)
: graph(graphWidget)
{
this->textureCubeManager = textureCubeManager;
setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache);
setZValue(-1);
nodeBiPtr = nullptr;
}
void Widget::Node::addEdge(Edge *edge)
{
edgeList << edge;
edge->adjust();
}
QList<Edge *> Widget::Node::edges() const
{
return edgeList;
}
void Widget::Node::calculateForces()
{
if (!scene() || scene()->mouseGrabberItem() == this) {
newPos = pos();
return;
}
// Sum up all forces pushing this item away
qreal xvel = 0;
qreal yvel = 0;
foreach(QGraphicsItem *item, scene()->items()) {
Widget::Node *node = qgraphicsitem_cast<Widget::Node *>(item);
if (!node)
continue;
QPointF vec = mapToItem(node, 0, 0);
qreal dx = vec.x();
qreal dy = vec.y();
double l = 2.0 * (dx * dx + dy * dy);
if (l > 0) {
xvel += (dx * 150.0) / l;
yvel += (dy * 150.0) / l;
}
}
// Now subtract all forces pulling items together
double weight = (edgeList.size() + 1) * 10;
foreach(Edge *edge, edgeList) {
QPointF vec;
if (edge->sourceNode() == this)
vec = mapToItem(edge->destNode(), 0, 0);
else
vec = mapToItem(edge->sourceNode(), 0, 0);
xvel -= vec.x() / weight;
yvel -= vec.y() / weight;
}
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
xvel = yvel = 0;
QRectF sceneRect = scene()->sceneRect();
newPos = pos() + QPointF(xvel, yvel);
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
}
//This method causes a node to advance its state based on the physics
//The logic for this is currently DISABLED
bool Widget::Node::advance()
{
if (newPos == pos())
return false;
//This is commented out for now - we have disabled movement physics
//setPos(newPos);
return true;
}
QRectF Widget::Node::boundingRect() const
{
qreal adjust = 2;
return QRectF(-10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust);
}
QPainterPath Widget::Node::shape() const
{
QPainterPath path;
path.addEllipse(-10, -10, 20, 20);
return path;
}
//This is the QT Paint method for graph nodes
void Widget::Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
//Currently only a radius at a maximum of 10 is guaranteed to work correctly, since the bounding box of the whole node is not large enough
//We will need to look into howt to increase it later
//The shadow does not work correctly yet for smaller radii
//This component was very much hardwired for a radius of 10 originally
const int RADIUS = 10;
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::darkGray);
painter->drawEllipse(-RADIUS * 0.7, -RADIUS * 0.7, RADIUS, RADIUS);
//Set the colors for the interior
QColor foreColor;
QColor backColor;
if (!nodeBiPtr->GetSelected())
{
foreColor = foreDisplayColor;
backColor = backDisplayColor;
}
else
{
foreColor = Qt::magenta;
backColor = Qt::darkMagenta;
}
//Draw the interior
QRadialGradient gradient(-3, -3, 10);
if (option->state & QStyle::State_Sunken) {
gradient.setCenter(3, 3);
gradient.setFocalPoint(3, 3);
gradient.setColorAt(1, foreColor.light(120));
gradient.setColorAt(0, backColor.light(120));
}
else {
gradient.setColorAt(0, foreColor);
gradient.setColorAt(1, backColor);
}
painter->setBrush(gradient);
//Draw the border
painter->setPen(QPen(Qt::black, 0));
painter->drawEllipse(-RADIUS, -RADIUS, RADIUS, RADIUS);
//Draw X's for invisible nodes
if (!nodeBiPtr->GetVisible()) {
painter->setPen(QPen(Qt::darkBlue, 2.5));
painter->drawLine(-RADIUS, -RADIUS, 0, 0);
painter->drawLine(-RADIUS, 0, 0, -RADIUS);
}
}
QVariant Widget::Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change) {
case ItemPositionHasChanged:
foreach(Edge *edge, edgeList)
edge->adjust();
graph->itemMoved();
break;
default:
break;
};
return QGraphicsItem::itemChange(change, value);
}
//Mouse press event for a node
//Toggles the selected state for a node
//Also prints information about it to the console - useful for debugging
void Widget::Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
update();
QGraphicsItem::mousePressEvent(event);
//Toggle visibility
//nodeBiPtr->SetVisible( !nodeBiPtr->GetVisible());
nodeBiPtr->SetSelected(!nodeBiPtr->GetSelected());
//SetChildrenVisibility(nodeBiPtr, nodeBiPtr->GetVisible());
NodeBi * nodeBiPtr = getNodeBiPtr();
double entropyValue = nodeBiPtr->GetEntropy();
int volumeValue = nodeBiPtr->GetVolume();
nodeBiPtr->getTreeMapWindow()->updateChildren();
setToolTip(QString("Entropy: ") + QString::number(entropyValue));
cout << "Entropy of Node: " << entropyValue << endl;
cout << "Volume of Node: " << volumeValue << endl;
cout << "Name of Node: " << name << endl;
}
void Widget::Node::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
update();
QGraphicsItem::contextMenuEvent(event);
NodeBi * nodeBiPtr = getNodeBiPtr();
graph->splitSuperQuadric(nodeBiPtr);
//TODO: Externalize setting of application (1) for this
textureCubeManager->UpdateTexture(1);
}
void Widget::Node::SetChildrenVisibility(NodeBi *nd, bool _isVisible)
{
nd->SetVisible(_isVisible);
if (nd->GetGraphNode() != nullptr) {
nd->GetGraphNode()->update();
}
if (nd->GetLeft() != nullptr) {
SetChildrenVisibility(nd->GetLeft(), _isVisible);
}
if (nd->GetRight() != nullptr) {
SetChildrenVisibility(nd->GetRight(), _isVisible);
}
}
void Widget::Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
update();
QGraphicsItem::mouseReleaseEvent(event);
}
/*void Widget::Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
NodeBi * nodeBiPtr = getNodeBiPtr();
double entropyValue = nodeBiPtr->GetEntropy();
setToolTip(QString("Entropy: ") + QString::number(entropyValue));
}*/