Debian Codegraph @

src/pool-prj-mgr/pool-mgr/duplicate/duplicate_entity.cpp

raw
1 #include "duplicate_entity.hpp"
2 #include "duplicate_unit.hpp"
3 #include "duplicate_window.hpp"
4 #include "pool/pool.hpp"
5 #include "pool/part.hpp"
6 #include "pool/unit.hpp"
7 #include "pool/symbol.hpp"
8 #include "util/gtk_util.hpp"
9 #include "util/util.hpp"
10 #include "widgets/location_entry.hpp"
11 #include "nlohmann/json.hpp"
12
13 namespace horizon {
14 DuplicateEntityWidget::DuplicateEntityWidget(Pool &p, const UUID &entity_uuid, Gtk::Box *ubox, bool optional)
15 : Gtk::Box(Gtk::ORIENTATION_VERTICAL, 10), pool(p), entity(*pool.get_entity(entity_uuid))
16 {
17 auto la = Gtk::manage(new Gtk::Label);
18 la->set_markup("<b>Entity: " + entity.name + "</b>");
19 la->set_xalign(0);
20 la->show();
21 if (!optional) {
22 pack_start(*la, false, false, 0);
23 }
24 else {
25 auto explain_label = Gtk::manage(new Gtk::Label);
26 explain_label->get_style_context()->add_class("dim-label");
27 explain_label->set_xalign(0);
28 explain_label->set_text("The new part will reference the new entity");
29 explain_label->show();
30 explain_label->set_margin_left(20);
31
32
33 auto cb = Gtk::manage(new Gtk::CheckButton);
34 cb->add(*la);
35 cb->show();
36 cb->set_active(true);
37 cb->signal_toggled().connect([this, cb, explain_label] {
38 grid->set_visible(cb->get_active());
39 if (cb->get_active()) {
40 explain_label->set_text("The new part will reference the new entity");
41 }
42 else {
43 explain_label->set_text("The new part will reference the existing entity");
44 }
45 for (auto ch : unit_box->get_children()) {
46 if (auto c = dynamic_cast<DuplicateUnitWidget *>(ch)) {
47 c->set_visible(cb->get_active());
48 }
49 }
50 s_signal_changed.emit();
51 });
52 pack_start(*cb, false, false, 0);
53 pack_start(*explain_label, false, false, 0);
54 }
55
56 grid = Gtk::manage(new Gtk::Grid);
57 grid->set_row_spacing(10);
58 grid->set_column_spacing(10);
59 if (optional)
60 grid->set_margin_left(20);
61
62 int top = 0;
63
64 name_entry = Gtk::manage(new Gtk::Entry);
65 name_entry->set_text(entity.name + " (Copy)");
66 name_entry->set_hexpand(true);
67 grid_attach_label_and_widget(grid, "Name", name_entry, top);
68
69 location_entry = Gtk::manage(new LocationEntry(pool.get_base_path()));
70 location_entry->set_append_json(true);
71 location_entry->set_rel_filename(
72 DuplicateUnitWidget::insert_filename(pool.get_rel_filename(ObjectType::ENTITY, entity.uuid), "-copy"));
73 location_entry->signal_changed().connect([this] { s_signal_changed.emit(); });
74 grid_attach_label_and_widget(grid, "Filename", location_entry, top);
75
76 grid->show_all();
77 grid->set_margin_bottom(10);
78
79 pack_start(*grid, false, false, 0);
80
81
82 unit_box = ubox;
83
84 std::set<UUID> units;
85 for (const auto &it : entity.gates) {
86 units.insert(it.second.unit->uuid);
87 }
88
89 for (const auto &uu : units) {
90 auto wu = Gtk::manage(new DuplicateUnitWidget(pool, uu, true));
91 wu->signal_changed().connect([this] { s_signal_changed.emit(); });
92 unit_box->pack_start(*wu, false, false, 0);
93 wu->show();
94 wu->set_margin_start(10);
95 wu->set_margin_bottom(10);
96 }
97 }
98
99 UUID DuplicateEntityWidget::get_uuid() const
100 {
101 return entity.uuid;
102 }
103
104 UUID DuplicateEntityWidget::duplicate(std::vector<std::string> *filenames)
105 {
106 if (grid->get_visible()) {
107 Entity new_entity(entity);
108 new_entity.uuid = UUID::random();
109 new_entity.name = name_entry->get_text();
110 auto new_entity_json = new_entity.serialize();
111
112 std::map<UUID, UUID> unit_map;
113 for (auto ch : unit_box->get_children()) {
114 if (auto c = dynamic_cast<DuplicateUnitWidget *>(ch)) {
115 auto old_unit_uuid = c->get_uuid();
116 auto new_unit_uuid = c->duplicate(filenames);
117 unit_map[old_unit_uuid] = new_unit_uuid;
118 }
119 }
120 auto &o = new_entity_json.at("gates");
121 for (auto it = o.begin(); it != o.end(); ++it) {
122 auto &v = it.value();
123 UUID unit_uu(v.at("unit").get<std::string>());
124 v["unit"] = (std::string)unit_map.at(unit_uu);
125 }
126 auto entity_filename = location_entry->get_filename();
127 if (filenames)
128 filenames->push_back(entity_filename);
129 ensure_parent_dir(entity_filename);
130 save_json_to_file(entity_filename, new_entity_json);
131 return new_entity.uuid;
132 }
133 else {
134 return entity.uuid;
135 }
136 }
137
138 bool DuplicateEntityWidget::check_valid()
139 {
140 if (!grid->get_visible())
141 return true;
142 bool valid = true;
143 valid = location_entry->check_ends_json() && valid;
144 for (auto ch : unit_box->get_children()) {
145 if (auto c = dynamic_cast<DuplicateUnitWidget *>(ch)) {
146 valid = c->check_valid() && valid;
147 }
148 }
149 return valid;
150 }
151
152 } // namespace horizon