Skip to content

Commit 8a3bcf2

Browse files
committed
Add graph import functionalities in json_from
1 parent 21528cb commit 8a3bcf2

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

GNodeGUI/include/gnodegui/graph_viewer.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ class GraphViewer : public QGraphicsView
5555

5656
std::vector<std::string> get_selected_node_ids();
5757

58-
void json_from(nlohmann::json json);
58+
// prefix_id can be usefull when importing a graph into an existing
59+
// one, to avoid duplicate node ids
60+
void json_from(nlohmann::json json,
61+
bool clear_existing_content = true,
62+
const std::string &prefix_id = "");
5963

6064
nlohmann::json json_to() const;
6165

GNodeGUI/src/graph_viewer.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -581,13 +581,17 @@ bool GraphViewer::is_item_static(QGraphicsItem *item)
581581
this->static_items.end());
582582
}
583583

584-
void GraphViewer::json_from(nlohmann::json json)
584+
void GraphViewer::json_from(nlohmann::json json,
585+
bool clear_existing_content,
586+
const std::string &prefix_id)
585587
{
586588
// generate graph from json data
587-
this->clear();
588-
589-
this->id = json["id"];
590-
this->current_link_type = json["current_link_type"].get<LinkType>();
589+
if (clear_existing_content)
590+
{
591+
this->clear();
592+
this->id = json["id"];
593+
this->current_link_type = json["current_link_type"].get<LinkType>();
594+
}
591595

592596
if (!json["groups"].is_null())
593597
{
@@ -603,7 +607,7 @@ void GraphViewer::json_from(nlohmann::json json)
603607
{
604608
for (auto &json_node : json["nodes"])
605609
{
606-
std::string nid = json_node["id"];
610+
std::string nid = prefix_id + json_node["id"].get<std::string>();
607611

608612
float x = json_node["scene_position.x"];
609613
float y = json_node["scene_position.y"];
@@ -623,8 +627,8 @@ void GraphViewer::json_from(nlohmann::json json)
623627
{
624628
for (auto &json_link : json["links"])
625629
{
626-
std::string node_out_id = json_link["node_out_id"];
627-
std::string node_in_id = json_link["node_in_id"];
630+
std::string node_out_id = prefix_id + json_link["node_out_id"].get<std::string>();
631+
std::string node_in_id = prefix_id + json_link["node_in_id"].get<std::string>();
628632
std::string port_out_id = json_link["port_out_id"];
629633
std::string port_in_id = json_link["port_in_id"];
630634

0 commit comments

Comments
 (0)