Skip to content

Commit

Permalink
adds storing and loading of new categories as well as exports colors
Browse files Browse the repository at this point in the history
  • Loading branch information
stweise committed Jan 1, 2021
1 parent 6ae5560 commit d8e8d73
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
21 changes: 16 additions & 5 deletions graphicsscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ void GraphicsScene::load() {
if (json.contains(nodesarrayname) && json[nodesarrayname].isArray()) {
QJsonArray nodearray = json[nodesarrayname].toArray();
for (int i = 0; i < nodearray.size(); ++i) {
Node *node = new Node(nodearray[i].toObject());
QJsonObject jsonnode = nodearray[i].toObject();
Node *node = nullptr;
if (version < 3) {
QJsonObject local = jsonnode;
local.insert(QString("category"), QJsonValue(0));
node = new Node(local);
}
if (version >= 3) {
node = new Node(nodearray[i].toObject());
}
// qDebug() << node;
addItem(node);
nodes.push_back(node);
Expand Down Expand Up @@ -168,7 +177,7 @@ void GraphicsScene::load() {
}

void GraphicsScene::writeJsonFromScene(QJsonObject &json) {
json["version"] = 2;
json["version"] = 3;
// create array of all the nodes
QJsonArray nodearray;
std::vector<Node *>::iterator nit = nodes.begin();
Expand Down Expand Up @@ -209,8 +218,10 @@ void GraphicsScene::exportToDot() {
while (nit != nodes.end()) {
QString cleanedLabel = (*nit)->nodelabel;
cleanedLabel.replace("\n", "\\n");
stream << "node" << (*nit)->ID << " [label=\"" << cleanedLabel
<< "\"];\n";
stream << "node" << (*nit)->ID << " [label=\"" << cleanedLabel << "\""
<< ", style=filled, fillcolor=\"" << (*nit)->getHexFillColor()
<< "\""
<< "];\n";
++nit;
}

Expand Down Expand Up @@ -260,7 +271,7 @@ void GraphicsScene::mouseDoubleClickEvent(
ok = !!d.exec();
QString text = d.getText();
if (ok && !text.isEmpty()) {
Node *node = new Node(mouseEvent->scenePos(), text);
Node *node = new Node(mouseEvent->scenePos(), text, 0);
addItem(node);
nodes.push_back(node);
}
Expand Down
13 changes: 10 additions & 3 deletions node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ QColor CategoryColor[] = {
QColor(246, 227, 168), QColor(238, 195, 152), QColor(223, 163, 163),
};

Node::Node(QPointF center, QString label)
Node::Node(QPointF center, QString label, int category)
: QGraphicsItem::QGraphicsItem(nullptr) {
QGraphicsItem::setPos(center);
setFlag(ItemIsSelectable);
setFlag(ItemIsMovable);
nodelabel = label;
setZValue(2);
category = 0; // sets default category 0 and thus default color
this->category = category; // sets default category 0 and thus default color
ID = instantiationCounter;
qDebug() << "Node Constructor ID: " << ID;
instantiationCounter++;
Expand Down Expand Up @@ -50,9 +50,13 @@ Node::Node(QJsonObject json) : QGraphicsItem::QGraphicsItem(nullptr) {
if (instantiationCounter < ID) {
instantiationCounter = ID;
}
instantiationCounter++;
}
QString categoryname = "category";
if (json.contains(categoryname) && json[categoryname].isDouble()) {
category = json[categoryname].toInt();
}
setZValue(2);
category = 0;
qDebug() << "Node Constructor ID: " << ID;
}

Expand All @@ -64,6 +68,7 @@ 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()));
Expand Down Expand Up @@ -123,3 +128,5 @@ int Node::type() const {
// Enable the use of qgraphicsitem_cast with this item.
return Type;
}

QString Node::getHexFillColor() { return CategoryColor[category].name(); }
3 changes: 2 additions & 1 deletion node.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Node : public QGraphicsItem {
public:
Node(QPointF center, QString label);
Node(QPointF center, QString label, int category);
Node(QJsonObject json);
~Node();

Expand All @@ -17,6 +17,7 @@ class Node : public QGraphicsItem {
QWidget* widget);
virtual int type() const;
QJsonObject returnJsonObj();
QString getHexFillColor();

int ID;
QString nodelabel;
Expand Down

0 comments on commit d8e8d73

Please sign in to comment.