-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeMapPlot.cpp
More file actions
516 lines (408 loc) · 15.1 KB
/
TreeMapPlot.cpp
File metadata and controls
516 lines (408 loc) · 15.1 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Copyright (c) 2010 Mark Liversedge (liversedge@gmail.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "DataManager.h"
#include "DataMgrVect.h"
#include "TreeMapPlot.h"
#include "TreeMapWindow.h"
#include <QSettings>
#include <cmath> // for isinf() isnan()
#include <vector>
#include <iostream>
using namespace std;
bool TreeMapEntropyLessThan(const TreeMap *a, const TreeMap *b) {
return (a->value.entropy) > (b->value.entropy);
}
bool TreeMapVolumeLessThan(const TreeMap *a, const TreeMap *b) {
return (a->value.volume) > (b->value.volume);
}
//Constructor
//Parameters:
//parent - the TreeMapWindow object to which the tree map belongs
//dataManager - reference to the data manager object
//appSettings - reference to the AppSettings object
//showLabel - true if labels should be shown in the widget, false if not (this will be moved to the AppSettings class later)
TreeMapPlot::TreeMapPlot(TreeMapWindow *parent, DataManager * dataManager, AppSettings * appSettings, bool showLabel)
: QWidget(parent), parent(parent)
{
this->dataManager = dataManager;
this->appSettings = appSettings;
this->showLabel = showLabel;
root = new TreeMap(nullptr, "", TreeMapRecord(0,0), appSettings);
setMouseTracking(true);
installEventFilter(this);
}
//Destructor
TreeMapPlot::~TreeMapPlot()
{
}
//Sets the nodeBi root record and causes the display tree and layout to be generated
void TreeMapPlot::setData(NodeBi * rootBi)
{
this->rootBi = rootBi;
//Issue a resize event
//This will cause a new layout to be issued, calling function resizeEvent,
//which will both build the display tree from the root Bi node and
//then run the necessary layout algorithms
resizeEvent(NULL);
}
//Builds the display tree used by the layout system
//Calls different methods depending on the layout method
void TreeMapPlot::buildTree()
{
if (root != nullptr) {
root->clear();
}
switch (layoutMethod) {
case 0:
case 1:
buldMultiLevelTree(rootBi);
break;
case 2:
buildTreeOfLeaves(rootBi);
break;
}
leafNodes.clear();
buildLeafList(root);
}
//Builds a display tree with multiple node levels
//Parameter biNode - the root node of the tree
void TreeMapPlot::buldMultiLevelTree(NodeBi * biNode)
{
//Deal with edge cases
if (biNode == nullptr) {
root->insert(QString(""), TreeMapRecord(0, 0));
}
else if (biNode->GetLeft() == nullptr && biNode->GetRight() == nullptr) {
biNode->setTreeMapWindow(parent);
root->insert(QString(""), TreeMapRecord(biNode->GetEntropy(), biNode->GetVolume()));
}
else {
biNode->setTreeMapWindow(parent);
//Recurse to the next level in the tree
if (biNode->GetLeft() != nullptr) {
buldMultiLevelTree(biNode->GetLeft(), root, 0);
}
if (biNode->GetRight() != nullptr) {
buldMultiLevelTree(biNode->GetRight(), root, 0);
}
}
}
//Recursively builds the remainder of the display tree, building a tree with multiple node levels
//Parameter biNode - current BI node data record
//Parameter treeMapNode - current display tree record
//Current depth - how deep we are into the tree
void TreeMapPlot::buldMultiLevelTree(NodeBi * biNode, TreeMap * treeMapNode, int currentDepth)
{
//Record the tree window reference for each node
//Hopefully we will replace this later with references to individual tree map nodes, once we resolve the circular #include design issues
biNode->setTreeMapWindow(parent);
TreeMap * newNode = nullptr;
const int DEPTH_LIMIT = 100000; //This constant can be used to limit the number of levels used - it is useful in debugging. For now it is set to a massive number to make there be no real limit.
//If we are at a leaf node, add a regular value to the tree map
if (biNode->GetLeft() == nullptr && biNode->GetRight() == nullptr || currentDepth >= DEPTH_LIMIT) {
//newNode = treeMapNode->insert(QString(""), biNode->GetEntropy());
newNode = treeMapNode->insert(QString::number(biNode->GetEntropy(), 'g', 2), TreeMapRecord(biNode->GetEntropy(), biNode->GetVolume()));
}
//Otherwise we are not at a leaf node, in which case the value really doesn't matter. Show that with a value of 0.
else {
newNode = treeMapNode->insert(QString(""), TreeMapRecord(0, 0));
}
newNode->nodeBiRef = biNode;
//Recurse to the next level in the tree
if (biNode->GetLeft() != nullptr && currentDepth < DEPTH_LIMIT) {
buldMultiLevelTree(biNode->GetLeft(), newNode, currentDepth + 1);
}
if (biNode->GetRight() != nullptr && currentDepth < DEPTH_LIMIT) {
buldMultiLevelTree(biNode->GetRight(), newNode, currentDepth + 1);
}
}
//Builds a display tree in which only leaf nodes from the original BI tree are added, all under a comon root. Thus this tree only has "one level" under the root.
//Parameter biNode - the root NodeBi record from which to build
void TreeMapPlot::buildTreeOfLeaves(NodeBi * biNode)
{
//Deal with edge cases
if (biNode == nullptr) {
root->insert(QString(""), TreeMapRecord(0, 0));
}
else if (biNode->GetLeft() == nullptr && biNode->GetRight() == nullptr) {
biNode->setTreeMapWindow(parent);
root->insert(QString(""), TreeMapRecord(biNode->GetEntropy(), biNode->GetVolume()));
}
else {
biNode->setTreeMapWindow(parent);
//Recurse to the next level in the tree
if (biNode->GetLeft() != nullptr) {
buildTreeOfLeaves(biNode->GetLeft(), root, 0);
}
if (biNode->GetRight() != nullptr) {
buildTreeOfLeaves(biNode->GetRight(), root, 0);
}
}
}
//This method recursively builds the remainder of the display tree, building it only using leaf nodes from the original BI tree
//Parameter biNode - current BI node data record
//Parameter treeMapNode - current display tree record
//Current depth - how deep we are into the tree
void TreeMapPlot::buildTreeOfLeaves(NodeBi * biNode, TreeMap * treeMapNode, int currentDepth)
{
//Record the tree window reference for each node
//Hopefully we will replace this later with references to individual tree map nodes, once we resolve the circular #include design issues
biNode->setTreeMapWindow(parent);
TreeMap * newNode = nullptr;
const int DEPTH_LIMIT = 100000; //This constant can be used to limit the number of levels used - it is useful in debugging. For now it is set to a massive number to make there be no real limit.
//If we are at a leaf node, add a regular value to the tree map
if (biNode->GetLeft() == nullptr && biNode->GetRight() == nullptr || currentDepth >= DEPTH_LIMIT) {
//newNode = treeMapNode->insert(QString(""), biNode->GetEntropy());
newNode = root->insert(QString::number(biNode->GetEntropy(), 'g', 2), TreeMapRecord(biNode->GetEntropy(), biNode->GetVolume()));
newNode->nodeBiRef = biNode;
}
//Recurse to the next level in the tree
if (biNode->GetLeft() != nullptr && currentDepth < DEPTH_LIMIT) {
buildTreeOfLeaves(biNode->GetLeft(), newNode, currentDepth + 1);
}
if (biNode->GetRight() != nullptr && currentDepth < DEPTH_LIMIT) {
buildTreeOfLeaves(biNode->GetRight(), newNode, currentDepth + 1);
}
}
//This method responds to resize events, rebuilding the layout
void TreeMapPlot::resizeEvent(QResizeEvent *)
{
issueLayout();
}
//This method paints to the QT widget
void TreeMapPlot::paintEvent(QPaintEvent *)
{
TreeMap * drawNode = nullptr;
drawNode = root;
//areaReport();
if (!drawNode) return;
//resize(sizeHint() * 2);
// labels
// Init paint settings
QPainter painter(this);
QColor color = QColor(255, 255, 255, 255);
QPen pen(color);
pen.setWidth(10); // root
QBrush brush(color);
painter.setBrush(brush);
painter.setPen(pen);
// draw border and background (root node)
painter.drawRect(drawNode->rect.x() + 4,
drawNode->rect.y() + 4,
drawNode->rect.width() - 8,
drawNode->rect.height() - 8);
// first level - rectangles, but not text yet
pen.setWidth(5);
pen.setColor(color);
painter.setPen(pen);
paintChildren(drawNode, painter, brush, 0);
}
//This method recursively paints the children of a display tree
//Parameter parent -- the current display tree node
//Parameter painter - the QPainter object being used
//Parameter brush - the Qbrush object being used
//Parameter level - how deep we are into the tree
void TreeMapPlot::paintChildren(TreeMap * parent, QPainter & painter, QBrush & brush, int level)
{
QFont font;
QPen textPen;
QColor hcolor(Qt::lightGray);
hcolor.setAlpha(127);
QBrush hbrush(hcolor);
QPen selectedPen;
selectedPen.setColor(Qt::magenta);
selectedPen.setWidth(5);
textPen.setColor(Qt::darkMagenta);
int fontSizeValue = 0;
//Make the starting font size a bit smaller for some of the lower levels
switch (level) {
case 0:
fontSizeValue = 18;
break;
case 1:
fontSizeValue = 16;
break;
default:
fontSizeValue = 14;
break;
}
font.setPointSize(fontSizeValue);
painter.setFont(font);
int n = 1;
QColor cHSV, cRGB;
double factor = double(1) / double(parent->children.count());
double color[3];
foreach(TreeMap *first, parent->children) {
//Use color map based on entropy value or volume depending on AppSettings to determine color of rendered square
if (appSettings->useEntropyForTreeMapColor) {
double entropyValue = first->value.entropy;
((DataMgrVect *)dataManager)->getEntropyColor(entropyValue, color);
}
else {
double volumeValue = first->value.volume;
((DataMgrVect *)dataManager)->getVolumeColor(volumeValue, color);
}
cRGB.setRgb(255 * color[0], 255 * color[1], 255 * color[2], 255);
brush.setColor(cRGB);
//Set the brush. Use a hightlight if a node is moused over
if (first == highlight)
painter.setBrush(hbrush);
else
painter.setBrush(brush);
//Set the pen. Use the selection color if a node was clicked.
if (first->nodeBiRef != nullptr && first->nodeBiRef->GetSelected())
painter.setPen(selectedPen);
else
painter.setPen(textPen);
const int OFFSET_FACTOR = 0;
QRect drawRect;
drawRect.setRect(first->rect.x() + level * OFFSET_FACTOR, first->rect.y() + level * OFFSET_FACTOR, first->rect.width() - level * OFFSET_FACTOR, first->rect.height() - level * OFFSET_FACTOR);
painter.drawRect(drawRect);
paintChildren(first, painter, brush, level + 1);
QRect textRect;
QRect demandedTextRect;
textRect.setRect(first->rect.x() + 2, first->rect.y() + 2, first->rect.width() - 4, first->rect.height() - 4);
painter.setFont(font);
painter.setPen(textPen);
demandedTextRect = painter.boundingRect(textRect, Qt::AlignTop | Qt::AlignLeft, first->name);
if (!textRect.contains(demandedTextRect)) {
font.setPointSize(fontSizeValue - 2);
painter.setFont(font);
demandedTextRect = painter.boundingRect(textRect, Qt::AlignTop | Qt::AlignLeft, first->name);
if (!textRect.contains(demandedTextRect)) {
font.setPointSize(fontSizeValue - 4);
painter.setFont(font);
demandedTextRect = painter.boundingRect(textRect, Qt::AlignTop | Qt::AlignLeft, first->name);
if (!textRect.contains(demandedTextRect)) {
// fall back to use smallest useful font / even if text may be clipped
font.setPointSize(fontSizeValue - 6);
painter.setFont(font);
}
}
}
if (showLabel) {
painter.drawText(textRect, Qt::AlignVCenter | Qt::AlignHCenter, first->name);
}
n++;
}
}
//This is a debugging method to print areas of rectangles to the console
void TreeMapPlot::areaReport()
{
/*foreach(TreeMap *first, root->children) {
QRect firstRect = first->rect;
cout << "First level rectangle::: Width: " << firstRect.width() << " Height: " << firstRect.height() << " Area: " << firstRect.width() * firstRect.height() << " Value " << first->value << endl;
foreach(TreeMap *second, first->children) {
QRect secondRect = second->rect;
cout << "Second level rectangle::: Width: " << secondRect.width() << " Height: " << secondRect.height() << " Area: " << secondRect.width() * secondRect.height() << " Value: " << second->value << endl;
}
}*/
}
//This function builds a linked list of leaf nodes from the display tree structure
void TreeMapPlot::buildLeafList(TreeMap * parent)
{
if (parent->children.size() > 0) {
for (int i = 0; i < parent->children.size(); i++) {
if (parent->children[i] != nullptr) {
buildLeafList(parent->children[i]);
}
}
}
else {
leafNodes.push_back(parent);
}
}
//This function rebuilds the layout
//It first rebuilds the display tree. Then it rebuilds the layout on that display tree.
void TreeMapPlot::issueLayout()
{
buildTree();
if (root) {
switch (layoutMethod) {
case 0:
case 2:
root->layout(QRect(9, 9, geometry().width() - 18, geometry().height() - 18), true);
break;
case 1:
root->layout(QRect(9, 9, geometry().width() - 18, geometry().height() - 18), false);
break;
default:
cerr << "Bad layout request with id " << layoutMethod << ". This should not happen." << endl;
break;
}
}
repaint();
}
//This method receives and responds to events of interest
//Parameter e - the event being processed
bool TreeMapPlot::eventFilter(QObject *, QEvent *e)
{
//Respond to mouse movement events (they highlight rectangles with a brighter overall color)
if (e->type() == QEvent::MouseMove) {
QPoint pos = static_cast<QMouseEvent*>(e)->pos();
TreeMap *underMouse = NULL;
// look at the bottom rung.
foreach(TreeMap *first, leafNodes)
if ((underMouse = first->findAt(pos)) != NULL)
break;
// if this one isn't the one that is
// currently highlighted repaint to
// highlight it!
if (underMouse && highlight != underMouse) {
highlight = underMouse;
repaint();
return true;
}
}
else if (e->type() == QEvent::Leave) {
highlight = NULL;
repaint();
return true;
}
//Respond to left mouse button clicks (they select rectangles)
else if (e->type() == QEvent::MouseButtonPress) {
QPoint pos = static_cast<QMouseEvent*>(e)->pos();
Qt::MouseButton button = static_cast<QMouseEvent*>(e)->button();
if (button == Qt::LeftButton) {
TreeMap *underMouse = NULL;
// look at the bottom rung.
foreach(TreeMap *first, leafNodes)
if ((underMouse = first->findAt(pos)) != NULL)
break;
// got one?
if (underMouse && underMouse->nodeBiRef != nullptr) {
cout << "Node has: entropy = " << underMouse->nodeBiRef->GetEntropy() << " volume = " << underMouse->nodeBiRef->GetVolume() << endl;
underMouse->nodeBiRef->SetSelected(!underMouse->nodeBiRef->GetSelected());
if (underMouse->nodeBiRef->GetGraphNode() != nullptr) {
Widget::Node * nodePtr = underMouse->nodeBiRef->GetGraphNode();
nodePtr->update();
}
repaint();
return true;
}
}
}
//Respond to right mouse click events (they change the layout used)
else if (e->type() == QEvent::ContextMenu) {
cout << "Right Mouse Click" << endl;
layoutMethod = (layoutMethod + 1) % 3;
issueLayout();
update();
}
return false;
}