Debian Codegraph @

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

raw
1 #include "unit_editor.hpp"
2 #include <iostream>
3 #include "pool/unit.hpp"
4 #include "util/util.hpp"
5 #include <glibmm.h>
6 #include <iomanip>
7 #include "util/pool_completion.hpp"
8 #include "util/gtk_util.hpp"
9 #include "widgets/help_button.hpp"
10 #include "help_texts.hpp"
11 #include "widgets/pin_names_editor.hpp"
12 #include "nlohmann/json.hpp"
13 #include "checks/check_unit.hpp"
14
15 namespace horizon {
16
17 class PinEditor : public Gtk::Box {
18 public:
19 PinEditor(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, class Pin *p, UnitEditor *pa);
20 static PinEditor *create(class Pin *p, UnitEditor *pa);
21 class Pin *pin;
22 UnitEditor *parent;
23 void focus();
24
25 private:
26 Gtk::Entry *name_entry = nullptr;
27 PinNamesEditor *names_editor = nullptr;
28 Gtk::ComboBoxText *dir_combo = nullptr;
29 };
30
31 PinEditor::PinEditor(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, Pin *p, UnitEditor *pa)
32 : Gtk::Box(cobject), pin(p), parent(pa)
33 {
34 x->get_widget("pin_name", name_entry);
35 x->get_widget("pin_direction", dir_combo);
36 entry_add_sanitizer(name_entry);
37 parent->sg_name->add_widget(*name_entry);
38 parent->sg_direction->add_widget(*dir_combo);
39 widget_remove_scroll_events(*dir_combo);
40
41 {
42 Gtk::Box *pin_names_box;
43 x->get_widget("pin_names_box", pin_names_box);
44 names_editor = Gtk::manage(new PinNamesEditor(pin->names));
45 pin_names_box->pack_start(*names_editor, true, true, 0);
46 names_editor->show();
47 }
48 parent->sg_names->add_widget(*names_editor);
49
50
51 for (const auto &it : Pin::direction_names) {
52 dir_combo->append(std::to_string(static_cast<int>(it.first)), it.second);
53 }
54
55 name_entry->set_text(pin->primary_name);
56 name_entry->signal_changed().connect([this] {
57 pin->primary_name = name_entry->get_text();
58 parent->set_needs_save();
59 });
60 name_entry->signal_activate().connect([this] { parent->handle_activate(this); });
61 name_entry->signal_focus_in_event().connect([this](GdkEventFocus *ev) {
62 auto this_row = dynamic_cast<Gtk::ListBoxRow *>(get_ancestor(GTK_TYPE_LIST_BOX_ROW));
63 if (!this_row->is_selected()) {
64 auto lb = dynamic_cast<Gtk::ListBox *>(get_ancestor(GTK_TYPE_LIST_BOX));
65 lb->unselect_all();
66 lb->select_row(*this_row);
67 }
68 return false;
69 });
70
71 names_editor->signal_changed().connect([this] { parent->set_needs_save(); });
72
73 auto propagate = [this](std::function<void(PinEditor *)> fn) {
74 auto lb = dynamic_cast<Gtk::ListBox *>(get_ancestor(GTK_TYPE_LIST_BOX));
75 auto this_row = dynamic_cast<Gtk::ListBoxRow *>(get_ancestor(GTK_TYPE_LIST_BOX_ROW));
76 auto rows = lb->get_selected_rows();
77 if (rows.size() > 1 && this_row->is_selected()) {
78 for (auto &row : rows) {
79 if (auto ed = dynamic_cast<PinEditor *>(row->get_child())) {
80 fn(ed);
81 }
82 }
83 }
84 };
85
86 dir_combo->set_active_id(std::to_string(static_cast<int>(pin->direction)));
87 dir_combo->signal_changed().connect([this, propagate] {
88 pin->direction = static_cast<Pin::Direction>(std::stoi(dir_combo->get_active_id()));
89 if (parent->propagating)
90 return;
91 parent->propagating = true;
92 propagate([this](PinEditor *ed) { ed->dir_combo->set_active_id(dir_combo->get_active_id()); });
93 parent->set_needs_save();
94 parent->propagating = false;
95 });
96 }
97
98 PinEditor *PinEditor::create(Pin *p, UnitEditor *pa)
99 {
100 PinEditor *w;
101 Glib::RefPtr<Gtk::Builder> x = Gtk::Builder::create();
102 static const std::vector<Glib::ustring> widgets = {"pin_editor"};
103 x->add_from_resource("/org/horizon-eda/horizon/pool-prj-mgr/pool-mgr/editors/unit_editor.ui", widgets);
104 x->get_widget_derived("pin_editor", w, p, pa);
105 w->reference();
106 return w;
107 }
108
109 void PinEditor::focus()
110 {
111 name_entry->grab_focus();
112 }
113
114 void UnitEditor::sort()
115 {
116 if (sort_helper.get_column() == "pin") {
117 pins_listbox->set_sort_func([this](Gtk::ListBoxRow *a, Gtk::ListBoxRow *b) {
118 auto na = dynamic_cast<PinEditor *>(a->get_child())->pin->primary_name;
119 auto nb = dynamic_cast<PinEditor *>(b->get_child())->pin->primary_name;
120 return sort_helper.transform_order(strcmp_natural(na, nb));
121 });
122 }
123 else if (sort_helper.get_column() == "direction") {
124 pins_listbox->set_sort_func([this](Gtk::ListBoxRow *a, Gtk::ListBoxRow *b) {
125 auto da = static_cast<int>(dynamic_cast<PinEditor *>(a->get_child())->pin->direction);
126 auto db = static_cast<int>(dynamic_cast<PinEditor *>(b->get_child())->pin->direction);
127 return sort_helper.transform_order(da - db);
128 });
129 }
130 pins_listbox->invalidate_sort();
131 pins_listbox->unset_sort_func();
132 }
133
134 UnitEditor::UnitEditor(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, const std::string &fn,
135 class IPool &p)
136 : Gtk::Box(cobject), PoolEditorBase(fn, p),
137 unit(filename.size() ? Unit::new_from_file(filename) : Unit(UUID::random()))
138 {
139 history_append("init");
140 x->get_widget("unit_name", name_entry);
141 x->get_widget("unit_manufacturer", manufacturer_entry);
142 x->get_widget("unit_pins", pins_listbox);
143 x->get_widget("pin_add", add_button);
144 x->get_widget("pin_delete", delete_button);
145 x->get_widget("cross_probing", cross_probing_cb);
146 x->get_widget("pin_count", pin_count_label);
147 entry_add_sanitizer(name_entry);
148 entry_add_sanitizer(manufacturer_entry);
149
150 sg_name = decltype(sg_name)::cast_dynamic(x->get_object("sg_name"));
151 sg_direction = decltype(sg_direction)::cast_dynamic(x->get_object("sg_direction"));
152 sg_names = decltype(sg_names)::cast_dynamic(x->get_object("sg_names"));
153
154 HelpButton::pack_into(x, "box_names", HelpTexts::UNIT_PIN_ALT_NAMES);
155
156 name_entry->signal_changed().connect([this] {
157 if (is_loading())
158 return;
159 unit.name = name_entry->get_text();
160 set_needs_save();
161 });
162 manufacturer_entry->set_completion(create_pool_manufacturer_completion(pool));
163 manufacturer_entry->signal_changed().connect([this] {
164 if (is_loading())
165 return;
166 unit.manufacturer = manufacturer_entry->get_text();
167 set_needs_save();
168 });
169
170 delete_button->signal_clicked().connect(sigc::mem_fun(*this, &UnitEditor::handle_delete));
171 add_button->signal_clicked().connect(sigc::mem_fun(*this, &UnitEditor::handle_add));
172
173 pins_listbox->signal_key_press_event().connect([this](GdkEventKey *ev) {
174 if (ev->keyval == GDK_KEY_Delete) {
175 handle_delete();
176 return true;
177 }
178 return false;
179 });
180
181 sort_helper.attach("pin", x);
182 sort_helper.attach("direction", x);
183 sort_helper.signal_changed().connect(sigc::mem_fun(*this, &UnitEditor::sort));
184
185 label_set_tnum(pin_count_label);
186
187 load();
188 }
189
190 void UnitEditor::load()
191 {
192 auto loading_setter = set_loading();
193
194 name_entry->set_text(unit.name);
195 manufacturer_entry->set_text(unit.manufacturer);
196 for (auto ch : pins_listbox->get_children()) {
197 delete ch;
198 }
199 for (auto &it : unit.pins) {
200 auto ed = PinEditor::create(&it.second, this);
201 ed->show_all();
202 pins_listbox->append(*ed);
203 ed->unreference();
204 }
205 sort_helper.set_sort("pin", Gtk::SORT_ASCENDING);
206 update_pin_count();
207 }
208
209 void UnitEditor::handle_delete()
210 {
211 auto rows = pins_listbox->get_selected_rows();
212 if (rows.size() == 0)
213 return;
214 std::set<int> indices;
215 std::set<UUID> uuids;
216 for (auto &row : rows) {
217 uuids.insert(dynamic_cast<PinEditor *>(row->get_child())->pin->uuid);
218 indices.insert(row->get_index());
219 }
220 for (auto &row : rows) {
221 delete row;
222 }
223 for (auto it = unit.pins.begin(); it != unit.pins.end();) {
224 if (uuids.find(it->first) != uuids.end()) {
225 unit.pins.erase(it++);
226 }
227 else {
228 it++;
229 }
230 }
231 for (auto index : indices) {
232 auto row = pins_listbox->get_row_at_index(index);
233 if (row)
234 pins_listbox->select_row(*row);
235 }
236 set_needs_save();
237 update_pin_count();
238 }
239
240 static std::string inc_pin_name(const std::string &s, int inc = 1)
241 {
242 Glib::ustring u(s);
243 Glib::MatchInfo ma;
244 const auto regex = Glib::Regex::create("^(\\D*)(\\d+)(\\D*)\\d*$");
245 if (regex->match(u, ma)) {
246 auto number_str = ma.fetch(2);
247 auto number = std::stoi(number_str) + inc;
248 std::stringstream ss;
249 ss << ma.fetch(1);
250 ss << std::setfill('0') << std::setw(number_str.size()) << number;
251 ss << ma.fetch(3);
252 return ss.str();
253 }
254 else {
255 return s;
256 }
257 }
258
259 void UnitEditor::handle_add()
260 {
261 const Pin *pin_selected = nullptr;
262 auto rows = pins_listbox->get_selected_rows();
263 for (auto &row : rows) {
264 pin_selected = dynamic_cast<PinEditor *>(row->get_child())->pin;
265 }
266
267 auto uu = UUID::random();
268 auto pin = &unit.pins.emplace(uu, uu).first->second;
269 if (pin_selected) {
270 pin->swap_group = pin_selected->swap_group;
271 pin->direction = pin_selected->direction;
272 pin->primary_name = inc_pin_name(pin_selected->primary_name);
273 pin->names = pin_selected->names;
274 }
275
276 int index = 0;
277 {
278 auto children = pins_listbox->get_children();
279 index = children.size();
280 int i = 0;
281 for (auto &ch : children) {
282 auto row = dynamic_cast<Gtk::ListBoxRow *>(ch);
283 auto ed_row = dynamic_cast<PinEditor *>(row->get_child());
284 if (ed_row->pin == pin_selected) {
285 index = i;
286 break;
287 }
288 i++;
289 }
290 }
291
292
293 auto ed = PinEditor::create(pin, this);
294 ed->show_all();
295 pins_listbox->insert(*ed, index + 1);
296 ed->unreference();
297
298 auto children = pins_listbox->get_children();
299 for (auto &ch : children) {
300 auto row = dynamic_cast<Gtk::ListBoxRow *>(ch);
301 auto ed_row = dynamic_cast<PinEditor *>(row->get_child());
302 if (ed_row->pin->uuid == uu) {
303 pins_listbox->unselect_all();
304 pins_listbox->select_row(*row);
305 ed_row->focus();
306 listbox_ensure_row_visible_new(pins_listbox, row);
307 break;
308 }
309 }
310 set_needs_save();
311 update_pin_count();
312 }
313
314 void UnitEditor::handle_activate(PinEditor *ed)
315 {
316 auto children = pins_listbox->get_children();
317 size_t i = 0;
318 for (auto &ch : children) {
319 auto row = dynamic_cast<Gtk::ListBoxRow *>(ch);
320 auto ed_row = dynamic_cast<PinEditor *>(row->get_child());
321 if (ed_row == ed) {
322 if (i == children.size() - 1) {
323 pins_listbox->unselect_all();
324 pins_listbox->select_row(*row);
325 handle_add();
326 }
327 else {
328 auto row_next = dynamic_cast<Gtk::ListBoxRow *>(children.at(i + 1));
329 auto ed_next = dynamic_cast<PinEditor *>(row_next->get_child());
330 pins_listbox->unselect_all();
331 pins_listbox->select_row(*row_next);
332 listbox_ensure_row_visible(pins_listbox, row_next);
333 ed_next->focus();
334 }
335 return;
336 }
337 i++;
338 }
339 }
340
341 void UnitEditor::update_pin_count()
342 {
343 const uint32_t n = unit.pins.size();
344 std::string s = std::to_string(n);
345 if (n == 1)
346 s += " pin";
347 else
348 s += " pins";
349 pin_count_label->set_text(s);
350 }
351
352 void UnitEditor::select(const ItemSet &items)
353 {
354 if (!cross_probing_cb->get_active())
355 return;
356
357 pins_listbox->unselect_all();
358 for (auto &ch : pins_listbox->get_children()) {
359 auto row = dynamic_cast<Gtk::ListBoxRow *>(ch);
360 auto ed_row = dynamic_cast<PinEditor *>(row->get_child());
361 if (items.count({ObjectType::SYMBOL_PIN, ed_row->pin->uuid})) {
362 pins_listbox->select_row(*row);
363 }
364 }
365 }
366
367 void UnitEditor::save_as(const std::string &fn)
368 {
369 unset_needs_save();
370 filename = fn;
371 save_json_to_file(fn, unit.serialize());
372 }
373
374 std::string UnitEditor::get_name() const
375 {
376 return unit.name;
377 }
378
379 const UUID &UnitEditor::get_uuid() const
380 {
381 return unit.uuid;
382 }
383
384 RulesCheckResult UnitEditor::run_checks() const
385 {
386 return check_unit(unit);
387 }
388
389 const FileVersion &UnitEditor::get_version() const
390 {
391 return unit.version;
392 }
393
394 unsigned int UnitEditor::get_required_version() const
395 {
396 return unit.get_required_version();
397 }
398
399 ObjectType UnitEditor::get_type() const
400 {
401 return ObjectType::UNIT;
402 }
403
404 class HistoryItemUnit : public HistoryManager::HistoryItem {
405 public:
406 HistoryItemUnit(const Unit &u, const std::string &cm) : HistoryManager::HistoryItem(cm), unit(u)
407 {
408 }
409 Unit unit;
410 };
411
412 std::unique_ptr<HistoryManager::HistoryItem> UnitEditor::make_history_item(const std::string &comment)
413 {
414 return std::make_unique<HistoryItemUnit>(unit, comment);
415 }
416
417 void UnitEditor::history_load(const HistoryManager::HistoryItem &it)
418 {
419 const auto &x = dynamic_cast<const HistoryItemUnit &>(it);
420 unit = x.unit;
421 load();
422 }
423
424 UnitEditor *UnitEditor::create(const std::string &fn, class IPool &p)
425 {
426 UnitEditor *w;
427 Glib::RefPtr<Gtk::Builder> x = Gtk::Builder::create();
428 x->add_from_resource("/org/horizon-eda/horizon/pool-prj-mgr/pool-mgr/editors/unit_editor.ui");
429 x->get_widget_derived("unit_editor", w, fn, p);
430 w->reference();
431 return w;
432 }
433
434 } // namespace horizon