Skip to content

Commit d1028fd

Browse files
committed
Add import graph functionality (#33), fix endless loop when loading a default graph at Graph constructor (#259)
1 parent 415d4aa commit d1028fd

File tree

6 files changed

+93
-21
lines changed

6 files changed

+93
-21
lines changed

Hesiod/include/hesiod/graph_editor.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ class GraphEditor : public QObject, public GraphNode
4242

4343
gngui::GraphViewer *get_p_viewer() { return this->viewer.get(); }
4444

45-
void json_from(nlohmann::json const &json, bool override_config = true);
45+
void json_from(nlohmann::json const &json,
46+
bool override_config = true,
47+
bool clear_existing_content = true,
48+
const std::string &prefix_id = "");
4649

4750
nlohmann::json json_to() const;
4851

4952
void load_from_file(const std::filesystem::path &load_fname,
50-
bool override_config = true);
53+
bool override_config = true,
54+
bool clear_existing_content = true);
5155

5256
void update(); // GNode::Graph
5357

@@ -69,6 +73,8 @@ public Q_SLOTS:
6973

7074
void on_graph_clear_request();
7175

76+
void on_graph_import_request();
77+
7278
void on_graph_load_request();
7379

7480
void on_graph_new_request();
@@ -115,6 +121,8 @@ public Q_SLOTS:
115121

116122
bool update_node_on_new_link = true;
117123

124+
bool is_very_first_load = true;
125+
118126
void set_fname(const std::filesystem::path &new_fname);
119127

120128
void set_fname(const std::string &new_fname);

Hesiod/include/hesiod/model/graph_node.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class GraphNode : public gnode::Graph
3535

3636
ModelConfig *get_config_ref() { return this->config.get(); }
3737

38-
void json_from(nlohmann::json const &json, bool override_config = true);
38+
void json_from(nlohmann::json const &json,
39+
bool override_config = true,
40+
bool clear_existing_content = true,
41+
const std::string &prefix_id = "");
3942

4043
nlohmann::json json_to() const;
4144

Hesiod/src/graph_editor.cpp

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ GraphEditor::GraphEditor(const std::string &id,
7373
this,
7474
&GraphEditor::on_graph_clear_request);
7575

76+
this->connect(this->viewer.get(),
77+
&gngui::GraphViewer::graph_import_request,
78+
this,
79+
&GraphEditor::on_graph_import_request);
80+
7681
this->connect(this->viewer.get(),
7782
&gngui::GraphViewer::graph_load_request,
7883
this,
@@ -177,9 +182,15 @@ void GraphEditor::clear()
177182
this->graph_viewer_enable();
178183
}
179184

180-
void GraphEditor::json_from(nlohmann::json const &json, bool override_config)
185+
void GraphEditor::json_from(nlohmann::json const &json,
186+
bool override_config,
187+
bool clear_existing_content,
188+
const std::string &prefix_id)
181189
{
182-
GraphNode::json_from(json["graph_node"], override_config);
190+
GraphNode::json_from(json["graph_node"],
191+
override_config,
192+
clear_existing_content,
193+
prefix_id);
183194

184195
std::string version_file = json["Hesiod version"];
185196
std::string version = "v" + std::to_string(HESIOD_VERSION_MAJOR) + "." +
@@ -200,7 +211,7 @@ void GraphEditor::json_from(nlohmann::json const &json, bool override_config)
200211
// to prevent nodes update at each link creation when the loading
201212
// the graph (very slooow)
202213
this->update_node_on_new_link = false;
203-
this->viewer->json_from(json["graph_viewer"]);
214+
this->viewer->json_from(json["graph_viewer"], clear_existing_content, prefix_id);
204215
this->update_node_on_new_link = true;
205216
}
206217
}
@@ -222,7 +233,8 @@ nlohmann::json GraphEditor::json_to() const
222233
}
223234

224235
void GraphEditor::load_from_file(const std::filesystem::path &load_fname,
225-
bool override_config)
236+
bool override_config,
237+
bool clear_existing_content)
226238
{
227239
this->graph_viewer_disable();
228240

@@ -236,14 +248,35 @@ void GraphEditor::load_from_file(const std::filesystem::path &load_fname,
236248
file.close();
237249
LOG->trace("JSON successfully loaded from {}", load_fname.string());
238250

239-
// this->set_fname(load_fname);
251+
// to avoid some endless loop when the graph is created but the
252+
// main window does not exist yet
253+
if (!this->is_very_first_load)
254+
this->set_fname(load_fname);
240255

241-
this->clear();
242-
if (this->viewer)
243-
this->viewer->clear();
256+
// not cleared when importing for instance
257+
std::string prefix_id = "";
244258

245-
this->json_from(json, override_config);
259+
if (clear_existing_content)
260+
{
261+
this->clear();
262+
if (this->viewer)
263+
this->viewer->clear();
264+
}
265+
else
266+
{
267+
// if a graph is imported, used the current node id count to
268+
// prefix all the node from the imported graph to avoid any
269+
// duplicate
270+
uint current_id_count = this->get_id_count() + 1;
271+
this->set_id_count(current_id_count);
272+
273+
prefix_id = std::to_string(current_id_count) + "/";
274+
}
275+
276+
this->json_from(json, override_config, clear_existing_content, prefix_id);
246277
this->update();
278+
279+
this->is_very_first_load = false;
247280
}
248281
else
249282
LOG->error("Could not open file {} to load JSON", load_fname.string());
@@ -301,6 +334,28 @@ void GraphEditor::on_graph_clear_request()
301334
}
302335
}
303336

337+
void GraphEditor::on_graph_import_request()
338+
{
339+
LOG->trace("GraphEditor::on_graph_import_request");
340+
341+
std::filesystem::path path = this->fname.parent_path();
342+
343+
QString load_fname = QFileDialog::getOpenFileName(this->viewer.get(),
344+
"Import...",
345+
path.string().c_str(),
346+
"Hesiod files (*.hsd)");
347+
348+
if (!load_fname.isNull() && !load_fname.isEmpty())
349+
{
350+
bool override_config = true;
351+
bool clear_existing_content = false;
352+
353+
this->load_from_file(load_fname.toStdString(),
354+
override_config,
355+
clear_existing_content);
356+
}
357+
}
358+
304359
void GraphEditor::on_graph_load_request()
305360
{
306361
LOG->trace("GraphEditor::on_graph_load_request");

Hesiod/src/model/graph_node.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ GraphNode::GraphNode(const std::string &id, std::shared_ptr<ModelConfig> config)
1919
this->config->log_debug();
2020
}
2121

22-
void GraphNode::json_from(nlohmann::json const &json, bool override_config)
22+
void GraphNode::json_from(nlohmann::json const &json,
23+
bool override_config,
24+
bool clear_existing_content,
25+
const std::string &prefix_id)
2326
{
2427
LOG->trace("GraphNode::json_from, graph {}", this->get_id());
2528

26-
this->clear();
27-
this->set_id(json["id"]);
28-
this->set_id_count(json["id_count"]);
29+
if (clear_existing_content)
30+
{
31+
this->clear();
32+
this->set_id(json["id"]);
33+
this->set_id_count(json["id_count"]);
34+
}
2935

3036
// if set to false, the actual state of the configuration is used
3137
// (can for instance be used when modifying the configuration of an
@@ -43,17 +49,17 @@ void GraphNode::json_from(nlohmann::json const &json, bool override_config)
4349
std::string node_type = json_node["label"];
4450
std::shared_ptr<gnode::Node> node = node_factory(node_type, this->config);
4551

46-
this->add_node(node, json_node["id"]);
52+
this->add_node(node, prefix_id + json_node["id"].get<std::string>());
4753

4854
// set its parameters
4955
dynamic_cast<BaseNode *>(node.get())->json_from(json_node);
5056
}
5157

5258
// links
5359
for (auto &json_link : json["links"])
54-
this->new_link(json_link["node_id_from"].get<std::string>(),
60+
this->new_link(prefix_id + json_link["node_id_from"].get<std::string>(),
5561
json_link["port_id_from"].get<std::string>(),
56-
json_link["node_id_to"].get<std::string>(),
62+
prefix_id + json_link["node_id_to"].get<std::string>(),
5763
json_link["port_id_to"].get<std::string>());
5864
}
5965

0 commit comments

Comments
 (0)