-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.cpp
133 lines (124 loc) · 4.22 KB
/
node.cpp
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
// SPDX-License-Identifier: BSD-3-Clause
#include "node.h"
static int instantiationCounter = 0;
int nodeDrawingStyle = 2; // 1 - ellipse, 2 - rectangle
// grey for level 0, pastel colors for levels 1 - 5
QColor CategoryColor[] = {
QColor(206, 206, 206), QColor(161, 224, 234), QColor(191, 238, 180),
QColor(246, 227, 168), QColor(238, 195, 152), QColor(223, 163, 163),
};
Node::Node(QPointF center, QString label, int category)
: QGraphicsItem::QGraphicsItem(nullptr) {
QGraphicsItem::setPos(center);
setFlag(ItemIsSelectable);
setFlag(ItemIsMovable);
nodelabel = label;
setZValue(2);
this->category = category; // sets default category 0 and thus default color
ID = instantiationCounter;
#ifndef NDEBUG
qDebug() << "Node Constructor ID: " << ID;
#endif
instantiationCounter++;
}
Node::Node(QJsonObject json) : QGraphicsItem::QGraphicsItem(nullptr) {
setFlag(ItemIsSelectable);
setFlag(ItemIsMovable);
QString nodelabelname = "nodelabel";
if (json.contains(nodelabelname) && json[nodelabelname].isString()) {
nodelabel = json[nodelabelname].toString();
}
QString nodecentername = "nodecenter";
if (json.contains(nodecentername) && json[nodecentername].isObject()) {
QJsonObject jsoncenter = json[nodecentername].toObject();
QPoint center =
QPoint(jsoncenter["x"].toDouble(), jsoncenter["y"].toDouble());
QGraphicsItem::setPos(center);
}
QString IDname = "ID";
if (json.contains(IDname) && json[IDname].isDouble()) {
ID = json[IDname].toInt();
// ensure that instantiationCounter is at least as high as highest ID
// already in saved file
if (instantiationCounter < ID) {
instantiationCounter = ID;
}
instantiationCounter++;
}
QString categoryname = "category";
if (json.contains(categoryname) && json[categoryname].isDouble()) {
category = json[categoryname].toInt();
}
setZValue(2);
#ifndef NDEBUG
qDebug() << "Node Constructor ID: " << ID;
#endif
}
Node::~Node() {
#ifndef NDEBUG
qDebug() << "Node Destructor ID: " << ID;
#endif
// QGraphicsItem::~QGraphicsItem();
}
QJsonObject Node::returnJsonObj() {
QJsonObject obj;
obj.insert(QString("ID"), QJsonValue(ID));
obj.insert(QString("nodelabel"), QJsonValue(nodelabel));
obj.insert(QString("category"), QJsonValue(category));
QJsonObject center;
center.insert(QString("x"), QJsonValue(scenePos().x()));
center.insert(QString("y"), QJsonValue(scenePos().y()));
obj.insert(QString("nodecenter"), QJsonValue(center));
return obj;
}
QRectF Node::boundingRect() const {
// default fontsize for drawing is determined, then we use a bounding box of
// the text drawn with an increased font size to determine a bounding box that
// will fit our content
int increasedFontSize = QApplication::font().pointSize();
if (nodeDrawingStyle == 1) // ellipse
{
increasedFontSize += 12;
} else if (nodeDrawingStyle == 2) // rect
{
increasedFontSize += 4;
}
QFont font("Helvetica", increasedFontSize);
QFontMetrics fm(font);
QRectF rect = fm.boundingRect(QRect(), Qt::AlignLeft, nodelabel, 0, nullptr);
// 0,0 is the center of our node
rect.moveCenter(QPointF(0, 0));
return rect.normalized();
}
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) {
QRectF rect = boundingRect();
QPen pen(Qt::black);
if (QGraphicsItem::isSelected()) {
pen.setColor(
QColor(238, 54, 54)); // use red color for outer rim when selected
}
pen.setWidth(3);
painter->setPen(pen);
// painter->setFont(QFont("Helvetica",10));
QBrush brush(CategoryColor[category]);
painter->setBrush(brush);
if (nodeDrawingStyle == 1) {
painter->drawEllipse(rect);
} else if (nodeDrawingStyle == 2) {
pen.setWidth(2);
painter->drawRoundedRect(rect, 5, 5);
}
if (QGraphicsItem::isSelected()) {
pen.setColor(Qt::black); // unless we reset the pen color, we would have
// red text too
}
painter->setPen(pen);
painter->drawText(rect, Qt::AlignCenter, nodelabel);
// qDebug() << painter->fontInfo().pointSize();
}
int Node::type() const {
// Enable the use of qgraphicsitem_cast with this item.
return Type;
}
QString Node::getHexFillColor() { return CategoryColor[category].name(); }