Debian Codegraph @

src/pool-prj-mgr/pool-mgr/editors/entity_editor.cpp

raw
1 #include "entity_editor.hpp"
2 #include <iostream>
3 #include "pool/entity.hpp"
4 #include <glibmm.h>
5 #include "dialogs/pool_browser_dialog.hpp"
6 #include "widgets/pool_browser.hpp"
7 #include "widgets/tag_entry.hpp"
8 #include "util/pool_completion.hpp"
9 #include "util/gtk_util.hpp"
10 #include "widgets/help_button.hpp"
11 #include "help_texts.hpp"
12 #include "pool/ipool.hpp"
13 #include "util/util.hpp"
14 #include "checks/check_entity.hpp"
15 #include "nlohmann/json.hpp"
16
17 namespace horizon {
18
19 class GateEditor : public Gtk::Box {
20 public:
21 GateEditor(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, class Gate *g, EntityEditor *pa);
22 static GateEditor *create(class Gate *g, EntityEditor *pa);
23 class Gate *gate;
24 EntityEditor *parent;
25 void reload();
26
27 private:
28 Gtk::Entry *name_entry = nullptr;
29 Gtk::Entry *suffix_entry = nullptr;
30 Gtk::SpinButton *swap_group_spin_button = nullptr;
31 Gtk::Label *unit_label = nullptr;
32 };
33
34 GateEditor::GateEditor(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, Gate *g, EntityEditor *pa)
35 : Gtk::Box(cobject), gate(g), parent(pa)
36 {
37 x->get_widget("gate_name", name_entry);
38 x->get_widget("gate_suffix", suffix_entry);
39 x->get_widget("gate_swap_group", swap_group_spin_button);
40 x->get_widget("gate_unit", unit_label);
41 parent->sg_name->add_widget(*name_entry);
42 parent->sg_suffix->add_widget(*suffix_entry);
43 parent->sg_swap_group->add_widget(*swap_group_spin_button);
44 parent->sg_unit->add_widget(*unit_label);
45 widget_remove_scroll_events(*swap_group_spin_button);
46
47 entry_add_sanitizer(name_entry);
48 entry_add_sanitizer(suffix_entry);
49
50 parent->label_make_item_link(*unit_label, ObjectType::UNIT);
51
52 name_entry->set_text(gate->name);
53 name_entry->signal_changed().connect([this] {
54 gate->name = name_entry->get_text();
55 parent->set_needs_save();
56 });
57
58 suffix_entry->set_text(gate->suffix);
59 suffix_entry->signal_changed().connect([this] {
60 gate->suffix = suffix_entry->get_text();
61 parent->set_needs_save();
62 });
63
64 swap_group_spin_button->set_value(gate->swap_group);
65 swap_group_spin_button->signal_value_changed().connect([this] {
66 gate->swap_group = swap_group_spin_button->get_value_as_int();
67 parent->set_needs_save();
68 });
69
70 reload();
71 }
72
73 void GateEditor::reload()
74 {
75 unit_label->set_markup("<a href=\"" + (std::string)gate->unit->uuid + "\">"
76 + Glib::Markup::escape_text(gate->unit->name) + "</a>");
77 }
78
79 GateEditor *GateEditor::create(Gate *g, EntityEditor *pa)
80 {
81 GateEditor *w;
82 Glib::RefPtr<Gtk::Builder> x = Gtk::Builder::create();
83 static const std::vector<Glib::ustring> widgets = {"gate_editor", "adjustment1"};
84 x->add_from_resource("/org/horizon-eda/horizon/pool-prj-mgr/pool-mgr/editors/entity_editor.ui", widgets);
85 x->get_widget_derived("gate_editor", w, g, pa);
86 w->reference();
87 return w;
88 }
89
90 void EntityEditor::bind_entry(Gtk::Entry *e, std::string &s)
91 {
92 e->signal_changed().connect([this, e, &s] {
93 if (is_loading())
94 return;
95 s = e->get_text();
96 set_needs_save();
97 });
98 }
99
100 EntityEditor::EntityEditor(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, const std::string &fn,
101 IPool &p)
102 : Gtk::Box(cobject), PoolEditorBase(fn, p),
103 entity(filename.size() ? Entity::new_from_file(filename, pool) : Entity(UUID::random()))
104 {
105 history_append("init");
106
107 x->get_widget("entity_name", name_entry);
108 x->get_widget("entity_manufacturer", manufacturer_entry);
109 {
110 Gtk::Box *tag_box;
111 x->get_widget("entity_tags", tag_box);
112 tag_entry = Gtk::manage(new TagEntry(pool, ObjectType::ENTITY, true));
113 tag_entry->show();
114 tag_box->pack_start(*tag_entry, true, true, 0);
115 }
116 x->get_widget("entity_prefix", prefix_entry);
117 x->get_widget("entity_gates", gates_listbox);
118 x->get_widget("gate_add", add_button);
119 x->get_widget("gate_delete", delete_button);
120 entry_add_sanitizer(name_entry);
121 entry_add_sanitizer(manufacturer_entry);
122 entry_add_sanitizer(prefix_entry);
123
124 HelpButton::pack_into(x, "box_swap_group", HelpTexts::ENTITY_GATE_SWAP_GROUP);
125 HelpButton::pack_into(x, "box_suffix", HelpTexts::ENTITY_GATE_SUFFIX);
126 HelpButton::pack_into(x, "box_name", HelpTexts::ENTITY_GATE_NAME);
127
128 sg_name = decltype(sg_name)::cast_dynamic(x->get_object("sg_name"));
129 sg_suffix = decltype(sg_name)::cast_dynamic(x->get_object("sg_suffix"));
130 sg_swap_group = decltype(sg_name)::cast_dynamic(x->get_object("sg_swap_group"));
131 sg_unit = decltype(sg_name)::cast_dynamic(x->get_object("sg_unit"));
132
133 bind_entry(name_entry, entity.name);
134 bind_entry(manufacturer_entry, entity.manufacturer);
135 manufacturer_entry->set_completion(create_pool_manufacturer_completion(pool));
136 bind_entry(prefix_entry, entity.prefix);
137
138 tag_entry->signal_changed().connect([this] {
139 if (is_loading())
140 return;
141 entity.tags = tag_entry->get_tags();
142 set_needs_save();
143 });
144
145 sort_helper.attach("gate", x);
146 sort_helper.signal_changed().connect(sigc::mem_fun(*this, &EntityEditor::sort));
147 sort_helper.set_sort("gate", Gtk::SORT_ASCENDING);
148
149 add_button->signal_clicked().connect(sigc::mem_fun(*this, &EntityEditor::handle_add));
150 delete_button->signal_clicked().connect(sigc::mem_fun(*this, &EntityEditor::handle_delete));
151
152 load();
153 }
154
155 void EntityEditor::load()
156 {
157 auto loading_setter = set_loading();
158
159 name_entry->set_text(entity.name);
160 manufacturer_entry->set_text(entity.manufacturer);
161 prefix_entry->set_text(entity.prefix);
162
163 tag_entry->set_tags(entity.tags);
164
165 for (auto ch : gates_listbox->get_children()) {
166 delete ch;
167 }
168 for (auto &it : entity.gates) {
169 auto ed = GateEditor::create(&it.second, this);
170 ed->show_all();
171 gates_listbox->append(*ed);
172 ed->unreference();
173 }
174 }
175
176 void EntityEditor::sort()
177 {
178 if (sort_helper.get_column() == "gate") {
179 gates_listbox->set_sort_func([this](Gtk::ListBoxRow *a, Gtk::ListBoxRow *b) {
180 auto na = dynamic_cast<GateEditor *>(a->get_child())->gate->name;
181 auto nb = dynamic_cast<GateEditor *>(b->get_child())->gate->name;
182 return sort_helper.transform_order(strcmp_natural(na, nb));
183 });
184 }
185 gates_listbox->invalidate_sort();
186 gates_listbox->unset_sort_func();
187 }
188
189 void EntityEditor::reload()
190 {
191 entity.update_refs(pool);
192 auto children = gates_listbox->get_children();
193 for (auto &ch : children) {
194 auto row = dynamic_cast<Gtk::ListBoxRow *>(ch);
195 auto ed = dynamic_cast<GateEditor *>(row->get_child());
196 ed->reload();
197 }
198 }
199
200 void EntityEditor::handle_delete()
201 {
202 auto rows = gates_listbox->get_selected_rows();
203 if (rows.size() == 0)
204 return;
205 std::set<int> indices;
206 std::set<UUID> uuids;
207 for (auto &row : rows) {
208 uuids.insert(dynamic_cast<GateEditor *>(row->get_child())->gate->uuid);
209 indices.insert(row->get_index());
210 }
211 for (auto &row : rows) {
212 delete row;
213 }
214 for (auto it = entity.gates.begin(); it != entity.gates.end();) {
215 if (uuids.find(it->first) != uuids.end()) {
216 entity.gates.erase(it++);
217 }
218 else {
219 it++;
220 }
221 }
222 for (auto index : indices) {
223 auto row = gates_listbox->get_row_at_index(index);
224 if (row)
225 gates_listbox->select_row(*row);
226 }
227 set_needs_save();
228 }
229
230
231 void EntityEditor::handle_add()
232 {
233 auto top = dynamic_cast<Gtk::Window *>(get_ancestor(GTK_TYPE_WINDOW));
234 PoolBrowserDialog dia(top, ObjectType::UNIT, pool);
235 dia.get_browser().sort_by_mtime();
236 if (dia.run() == Gtk::RESPONSE_OK) {
237 auto unit_uu = dia.get_browser().get_selected();
238 if (unit_uu) {
239 auto uu = UUID::random();
240 auto gate = &entity.gates.emplace(uu, uu).first->second;
241 gate->unit = pool.get_unit(unit_uu);
242
243 auto ed = GateEditor::create(gate, this);
244 ed->show_all();
245 gates_listbox->append(*ed);
246 ed->unreference();
247
248 auto children = gates_listbox->get_children();
249 for (auto &ch : children) {
250 auto row = dynamic_cast<Gtk::ListBoxRow *>(ch);
251 auto edi = dynamic_cast<GateEditor *>(row->get_child());
252 if (edi->gate->uuid == uu) {
253 gates_listbox->unselect_all();
254 gates_listbox->select_row(*row);
255 break;
256 }
257 }
258
259 gates_listbox->invalidate_sort();
260 set_needs_save();
261 }
262 }
263 }
264
265 void EntityEditor::save_as(const std::string &fn)
266 {
267 unset_needs_save();
268 filename = fn;
269 save_json_to_file(fn, entity.serialize());
270 }
271
272 std::string EntityEditor::get_name() const
273 {
274 return entity.name;
275 }
276
277 const UUID &EntityEditor::get_uuid() const
278 {
279 return entity.uuid;
280 }
281
282 RulesCheckResult EntityEditor::run_checks() const
283 {
284 return check_entity(entity);
285 }
286
287 const FileVersion &EntityEditor::get_version() const
288 {
289 return entity.version;
290 }
291
292 ObjectType EntityEditor::get_type() const
293 {
294 return ObjectType::ENTITY;
295 }
296
297 class HistoryItemEntity : public HistoryManager::HistoryItem {
298 public:
299 HistoryItemEntity(const Entity &e, const std::string &cm) : HistoryManager::HistoryItem(cm), entity(e)
300 {
301 }
302 Entity entity;
303 };
304
305 std::unique_ptr<HistoryManager::HistoryItem> EntityEditor::make_history_item(const std::string &comment)
306 {
307 return std::make_unique<HistoryItemEntity>(entity, comment);
308 }
309
310 void EntityEditor::history_load(const HistoryManager::HistoryItem &it)
311 {
312 const auto &x = dynamic_cast<const HistoryItemEntity &>(it);
313 entity = x.entity;
314 entity.update_refs(pool);
315 load();
316 }
317
318 EntityEditor *EntityEditor::create(const std::string &fn, class IPool &p)
319 {
320 EntityEditor *w;
321 Glib::RefPtr<Gtk::Builder> x = Gtk::Builder::create();
322 x->add_from_resource("/org/horizon-eda/horizon/pool-prj-mgr/pool-mgr/editors/entity_editor.ui");
323 x->get_widget_derived("entity_editor", w, fn, p);
324 w->reference();
325 return w;
326 }
327 } // namespace horizon