Debian Codegraph @

src/pool-prj-mgr/pool-mgr/part_wizard/part_wizard.cpp

raw
1 #include "part_wizard.hpp"
2 #include "pool/package.hpp"
3 #include "util/util.hpp"
4 #include "util/geom_util.hpp"
5 #include "pad_editor.hpp"
6 #include "gate_editor.hpp"
7 #include "widgets/location_entry.hpp"
8 #include "util/str_util.hpp"
9 #include "util/gtk_util.hpp"
10 #include "util/pool_completion.hpp"
11 #include "nlohmann/json.hpp"
12 #include "pool-prj-mgr/pool-prj-mgr-app_win.hpp"
13 #include "util/csv.hpp"
14 #include "widgets/tag_entry.hpp"
15 #include "widgets/pool_browser_package.hpp"
16 #include "widgets/preview_canvas.hpp"
17 #include "canvas/canvas_gl.hpp"
18
19 namespace horizon {
20 LocationEntry *PartWizard::pack_location_entry(const Glib::RefPtr<Gtk::Builder> &x, const std::string &w,
21 Gtk::Button **button_other)
22 {
23 auto en = Gtk::manage(new LocationEntry(pool_base_path));
24 en->set_append_json(true);
25 if (button_other) {
26 *button_other = Gtk::manage(new Gtk::Button());
27 en->pack_start(**button_other, false, false);
28 (*button_other)->show();
29 }
30 Gtk::Box *box;
31 x->get_widget(w, box);
32 box->pack_start(*en, true, true, 0);
33 en->show();
34 return en;
35 }
36
37 PartWizard::PartWizard(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, const UUID &pkg_uuid,
38 const std::string &bp, class Pool &po, PoolProjectManagerAppWindow &aw)
39 : Gtk::Window(cobject), pool_base_path(bp), pool(po), part(UUID::random()),
40 entity(std::make_shared<Entity>(UUID::random())), appwin(aw), state_store(this, "part-wizard")
41 {
42 part.entity = entity;
43 x->get_widget("header", header);
44 x->get_widget("stack", stack);
45 x->get_widget("button_back", button_back);
46 x->get_widget("button_next", button_next);
47 x->get_widget("button_finish", button_finish);
48 x->get_widget("button_select", button_select);
49
50 x->get_widget("pads_lb", pads_lb);
51 x->get_widget("link_pads", button_link_pads);
52 x->get_widget("unlink_pads", button_unlink_pads);
53 x->get_widget("import_pads", button_import_pads);
54 x->get_widget("page_assign", page_assign);
55 x->get_widget("page_edit", page_edit);
56 x->get_widget("edit_left_box", edit_left_box);
57
58 x->get_widget("entity_name", entity_name_entry);
59 x->get_widget("entity_name_from_mpn", entity_name_from_mpn_button);
60 x->get_widget("entity_prefix", entity_prefix_entry);
61 {
62 Gtk::Box *tag_box;
63 x->get_widget("entity_tags", tag_box);
64 entity_tags_entry = Gtk::manage(new TagEntry(pool, ObjectType::ENTITY, true));
65 entity_tags_entry->show();
66 tag_box->pack_start(*entity_tags_entry, true, true, 0);
67 }
68
69 x->get_widget("part_mpn", part_mpn_entry);
70 x->get_widget("part_value", part_value_entry);
71 x->get_widget("part_manufacturer", part_manufacturer_entry);
72 x->get_widget("part_description", part_description_entry);
73 x->get_widget("part_datasheet", part_datasheet_entry);
74 {
75 Gtk::Box *tag_box;
76 x->get_widget("part_tags", tag_box);
77 part_tags_entry = Gtk::manage(new TagEntry(pool, ObjectType::PART, true));
78 part_tags_entry->show();
79 tag_box->pack_start(*part_tags_entry, true, true, 0);
80 }
81 x->get_widget("part_autofill", part_autofill_button);
82 x->get_widget("steps_grid", steps_grid);
83
84 {
85 Gtk::Box *canvas_box;
86 GET_WIDGET(canvas_box);
87 canvas = Gtk::manage(new PreviewCanvas(pool, true));
88 canvas->show();
89 canvas->signal_size_allocate().connect([this](const auto &alloc) {
90 if (!(alloc == canvas_alloc)) {
91 float pad = 1_mm;
92 auto bb = pad_bbox(canvas->get_canvas().get_bbox(true), pad);
93 canvas->get_canvas().zoom_to_bbox(bb);
94 canvas_alloc = alloc;
95 }
96 });
97 canvas_box->pack_start(*canvas, true, true, 0);
98 }
99
100 pads_lb->signal_selected_rows_changed().connect([this] {
101 std::set<UUID> pads_selected;
102 for (auto row : pads_lb->get_selected_rows()) {
103 auto ed = dynamic_cast<PadEditor *>(row->get_child());
104 for (auto pad : ed->get_pads()) {
105 pads_selected.insert(pad->uuid);
106 }
107 }
108 auto &ca = canvas->get_canvas();
109 ca.set_flags_all(0, TriangleInfo::FLAG_HIGHLIGHT);
110 ca.set_highlight_enabled(pads_selected.size());
111 for (const auto &it : pads_selected) {
112 ObjectRef ref(ObjectType::PAD, it);
113 ca.set_flags(ref, TriangleInfo::FLAG_HIGHLIGHT, 0);
114 }
115 });
116
117 entry_add_sanitizer(entity_name_entry);
118 entry_add_sanitizer(entity_prefix_entry);
119 entry_add_sanitizer(part_mpn_entry);
120 entry_add_sanitizer(part_value_entry);
121 entry_add_sanitizer(part_manufacturer_entry);
122 entry_add_sanitizer(part_description_entry);
123 entry_add_sanitizer(part_datasheet_entry);
124
125 part_manufacturer_entry->set_completion(create_pool_manufacturer_completion(pool));
126
127 part_mpn_entry->signal_changed().connect(sigc::mem_fun(*this, &PartWizard::update_can_finish));
128 entity_name_entry->signal_changed().connect(sigc::mem_fun(*this, &PartWizard::update_can_finish));
129 entity_prefix_entry->signal_changed().connect(sigc::mem_fun(*this, &PartWizard::update_can_finish));
130
131 part_autofill_button->signal_clicked().connect(sigc::mem_fun(*this, &PartWizard::autofill));
132
133 part_location_entry = pack_location_entry(x, "part_location_box");
134 part_location_entry->set_filename(Glib::build_filename(pool_base_path, "parts"));
135 part_location_entry->signal_changed().connect(sigc::mem_fun(*this, &PartWizard::update_can_finish));
136 {
137 Gtk::Button *from_part_button;
138 entity_location_entry = pack_location_entry(x, "entity_location_box", &from_part_button);
139 from_part_button->set_label("From part");
140 from_part_button->signal_clicked().connect([this] {
141 auto rel = get_rel_part_filename();
142 entity_location_entry->set_filename(Glib::build_filename(pool_base_path, "entities", rel));
143 });
144 entity_location_entry->set_filename(Glib::build_filename(pool_base_path, "entities"));
145 entity_location_entry->signal_changed().connect(sigc::mem_fun(*this, &PartWizard::update_can_finish));
146 }
147
148 entity_name_from_mpn_button->signal_clicked().connect(
149 [this] { entity_name_entry->set_text(part_mpn_entry->get_text()); });
150
151 entity_prefix_entry->set_text("U");
152
153 sg_pad = Glib::RefPtr<Gtk::SizeGroup>::cast_dynamic(x->get_object("sg_pad"));
154 sg_pin_name = Glib::RefPtr<Gtk::SizeGroup>::cast_dynamic(x->get_object("sg_pin_name"));
155 sg_direction = Glib::RefPtr<Gtk::SizeGroup>::cast_dynamic(x->get_object("sg_direction"));
156 sg_alt_names = Glib::RefPtr<Gtk::SizeGroup>::cast_dynamic(x->get_object("sg_alt_names"));
157 sg_gate = Glib::RefPtr<Gtk::SizeGroup>::cast_dynamic(x->get_object("sg_gate"));
158
159 gate_name_store = Gtk::ListStore::create(list_columns);
160
161 {
162 Gtk::TreeModel::Row row = *(gate_name_store->append());
163 row[list_columns.name] = "Main";
164 }
165
166 pads_lb->set_sort_func([](Gtk::ListBoxRow *a, Gtk::ListBoxRow *b) {
167 auto na = dynamic_cast<PadEditor *>(a->get_child())->names.front();
168 auto nb = dynamic_cast<PadEditor *>(b->get_child())->names.front();
169 return strcmp_natural(na, nb);
170 });
171
172 button_link_pads->signal_clicked().connect(sigc::mem_fun(*this, &PartWizard::handle_link));
173 button_unlink_pads->signal_clicked().connect(sigc::mem_fun(*this, &PartWizard::handle_unlink));
174 button_import_pads->signal_clicked().connect(sigc::mem_fun(*this, &PartWizard::handle_import));
175 button_next->signal_clicked().connect(sigc::mem_fun(*this, &PartWizard::handle_next));
176 button_back->signal_clicked().connect(sigc::mem_fun(*this, &PartWizard::handle_back));
177 button_select->signal_clicked().connect(sigc::mem_fun(*this, &PartWizard::handle_select));
178 button_finish->signal_clicked().connect(sigc::mem_fun(*this, &PartWizard::handle_finish));
179
180 {
181 Gtk::Paned *page_package;
182 x->get_widget("page_package", page_package);
183 browser_package = Gtk::manage(new PoolBrowserPackage(pool));
184 browser_package->show();
185 page_package->add1(*browser_package);
186
187 auto preview = Gtk::manage(new PreviewCanvas(pool, true));
188 button_select->set_sensitive(false);
189 browser_package->signal_selected().connect([this, preview] {
190 preview->load(ObjectType::PACKAGE, browser_package->get_selected());
191 button_select->set_sensitive(browser_package->get_selected());
192 });
193 browser_package->signal_activated().connect(sigc::mem_fun(*this, &PartWizard::handle_select));
194 browser_package->go_to(pkg_uuid);
195
196 preview->show();
197 page_package->add2(*preview);
198 }
199
200 set_mode(Mode::PACKAGE);
201
202 signal_delete_event().connect([this](GdkEventAny *ev) {
203 if (processes.size()) {
204 Gtk::MessageDialog md(*this, "Can't close right now", false /* use_markup */, Gtk::MESSAGE_ERROR,
205 Gtk::BUTTONS_OK);
206 md.set_secondary_text("Close all running editors first");
207 md.run();
208 return true; // keep open
209 }
210 if (files_saved.size())
211 return false;
212
213 if (mode == Mode::PACKAGE)
214 return false;
215
216 Gtk::MessageDialog md(*this, "Really close?", false /* use_markup */, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE);
217 md.set_secondary_text(
218 "By closing the part wizard, all changes to the new part will "
219 "be lost");
220 md.add_button("Close and discard changes", 1);
221 md.add_button("Keep open", Gtk::RESPONSE_CANCEL);
222 switch (md.run()) {
223 case 1:
224 return false; // close
225
226 default:
227 return true; // keep window open
228 }
229 });
230 }
231
232 void PartWizard::set_pkg(std::shared_ptr<const Package> p)
233 {
234 if (pkg)
235 return;
236 pkg = p;
237 part.attributes[Part::Attribute::MANUFACTURER] = {false, pkg->manufacturer};
238 part.package = pkg;
239 create_pad_editors();
240 }
241
242 void PartWizard::create_pad_editors()
243 {
244 for (auto &it : pkg->pads) {
245 if (it.second.pool_padstack->type != Padstack::Type::MECHANICAL) {
246 auto ed = PadEditor::create(&it.second, this);
247 ed->pin_name_entry->signal_activate().connect([this, ed] {
248 auto row = dynamic_cast<Gtk::ListBoxRow *>(ed->get_ancestor(GTK_TYPE_LIST_BOX_ROW));
249 auto index = row->get_index();
250 if (index >= 0) {
251 if (auto nextrow = pads_lb->get_row_at_index(index + 1)) {
252 auto ed_next = dynamic_cast<PadEditor *>(nextrow->get_child());
253 ed_next->pin_name_entry->grab_focus();
254 }
255 }
256 });
257 ed->show_all();
258 pads_lb->append(*ed);
259 ed->unreference();
260 }
261 }
262 }
263
264 void PartWizard::set_mode(PartWizard::Mode mo)
265 {
266 if (mo == Mode::ASSIGN && processes.size() > 0)
267 return;
268
269 if (mo == Mode::ASSIGN) {
270 stack->set_visible_child("assign");
271 header->set_subtitle("Assign pins to package " + pkg->name);
272 }
273 else if (mo == Mode::EDIT) {
274 prepare_edit();
275 stack->set_visible_child("edit");
276 update_can_finish();
277 header->set_subtitle("Part details & symbol");
278 }
279 else if (mo == Mode::PACKAGE) {
280 prepare_edit();
281 stack->set_visible_child("package");
282 header->set_subtitle("Select package");
283 }
284
285
286 button_back->set_visible(mo == Mode::EDIT);
287 button_finish->set_visible(mo == Mode::EDIT);
288 button_next->set_visible(mo == Mode::ASSIGN);
289 button_select->set_visible(mo == Mode::PACKAGE);
290 mode = mo;
291 }
292
293 void PartWizard::handle_next()
294 {
295 if (mode == Mode::ASSIGN) {
296 set_mode(Mode::EDIT);
297 }
298 }
299
300 void PartWizard::handle_back()
301 {
302 if (processes.size())
303 return;
304 set_mode(Mode::ASSIGN);
305 }
306
307 void PartWizard::handle_select()
308 {
309 auto p = browser_package->get_selected();
310 if (p) {
311 set_pkg(pool.get_package(p));
312 set_mode(Mode::ASSIGN);
313 canvas->load(ObjectType::PACKAGE, pkg->uuid);
314 }
315 }
316
317 std::vector<std::string> PartWizard::get_filenames()
318 {
319 std::vector<std::string> filenames;
320 auto children = edit_left_box->get_children();
321 for (auto ch : children) {
322 if (auto ed = dynamic_cast<GateEditorWizard *>(ch)) {
323 auto unit_filename = ed->unit_location_entry->get_filename();
324 auto symbol_filename = ed->symbol_location_entry->get_filename();
325 filenames.push_back(unit_filename);
326 filenames.push_back(symbol_filename);
327 }
328 }
329 filenames.push_back(entity_location_entry->get_filename());
330 filenames.push_back(part_location_entry->get_filename());
331 return filenames;
332 }
333
334 void PartWizard::handle_finish()
335 {
336 auto filenames = get_filenames();
337 std::vector<std::string> filenames_existing;
338 for (const auto &filename : filenames) {
339 if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS)) {
340 filenames_existing.push_back(filename);
341 }
342 }
343 if (filenames_existing.size() > 0) {
344 Gtk::MessageDialog md(*this, "Overwrite?", false /* use_markup */, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE);
345 std::string sec = "Creating this part will overwrite these files:\n";
346 for (const auto &fn : filenames_existing) {
347 sec += fn + "\n";
348 }
349 md.set_secondary_text(sec);
350 md.add_button("Overwrite", 1);
351 md.add_button("Cancel", Gtk::RESPONSE_CANCEL);
352 switch (md.run()) {
353 case 1:
354 // nop, go ahead
355 break;
356 default:
357 return;
358 }
359 }
360
361
362 try {
363 finish();
364 files_saved = filenames;
365 Gtk::Window::close();
366 }
367 catch (const std::exception &e) {
368 Gtk::MessageDialog md(*this, "Error Saving part", false /* use_markup */, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);
369 md.set_secondary_text(e.what());
370 md.run();
371 }
372 catch (const Gio::Error &e) {
373 Gtk::MessageDialog md(*this, "Error Saving part", false /* use_markup */, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);
374 md.set_secondary_text(e.what());
375 md.run();
376 }
377 }
378
379 std::vector<std::string> PartWizard::get_files_saved() const
380 {
381 return files_saved;
382 }
383
384 std::string PartWizard::get_rel_part_filename()
385 {
386 auto part_fn = Gio::File::create_for_path(part_location_entry->get_filename());
387 auto part_base = Gio::File::create_for_path(Glib::build_filename(pool_base_path, "parts"));
388 auto rel = part_base->get_relative_path(part_fn);
389 return rel;
390 }
391
392 void PartWizard::finish()
393 {
394 entity->name = entity_name_entry->get_text();
395 entity->prefix = entity_prefix_entry->get_text();
396 entity->tags = entity_tags_entry->get_tags();
397
398 part.attributes[Part::Attribute::MPN] = {false, part_mpn_entry->get_text()};
399 part.attributes[Part::Attribute::VALUE] = {false, part_value_entry->get_text()};
400 part.attributes[Part::Attribute::MANUFACTURER] = {false, part_manufacturer_entry->get_text()};
401 part.attributes[Part::Attribute::DATASHEET] = {false, part_datasheet_entry->get_text()};
402 part.attributes[Part::Attribute::DESCRIPTION] = {false, part_description_entry->get_text()};
403 part.tags = part_tags_entry->get_tags();
404
405 entity->manufacturer = part.get_manufacturer();
406
407 std::vector<std::string> filenames;
408 filenames.push_back(entity_location_entry->get_filename());
409 filenames.push_back(part_location_entry->get_filename());
410
411 auto children = edit_left_box->get_children();
412 for (auto ch : children) {
413 if (auto ed = dynamic_cast<GateEditorWizard *>(ch)) {
414 auto unit_filename = ed->unit_location_entry->get_filename();
415 auto symbol_filename = ed->symbol_location_entry->get_filename();
416 filenames.push_back(unit_filename);
417 filenames.push_back(symbol_filename);
418
419 auto unit = units.at(ed->gate->name);
420 assert(unit == ed->gate->unit);
421
422 ed->gate->suffix = ed->suffix_entry->get_text();
423 unit->name = ed->unit_name_entry->get_text();
424 unit->manufacturer = part.get_manufacturer();
425 }
426 }
427
428 for (const auto &it : filenames) {
429 if (!endswith(it, ".json")) {
430 throw std::runtime_error("Filename " + it + " doesn't end in .json");
431 }
432 }
433
434 for (const auto &it : filenames) {
435 auto dir = Glib::path_get_dirname(it);
436 if (!Glib::file_test(dir, Glib::FILE_TEST_IS_DIR)) {
437 Gio::File::create_for_path(dir)->make_directory_with_parents();
438 }
439 }
440
441 for (auto ch : children) {
442 if (auto ed = dynamic_cast<GateEditorWizard *>(ch)) {
443 auto unit_filename = ed->unit_location_entry->get_filename();
444 save_json_to_file(unit_filename, ed->gate->unit->serialize());
445
446 auto symbol_filename_src = pool.get_tmp_filename(ObjectType::SYMBOL, symbols.at(ed->gate->unit->uuid));
447 auto symbol_filename_dest = ed->symbol_location_entry->get_filename();
448 auto sym = Symbol::new_from_file(symbol_filename_src, pool);
449 sym.name = ed->symbol_name_entry->get_text();
450 save_json_to_file(symbol_filename_dest, sym.serialize());
451 }
452 }
453 save_json_to_file(entity_location_entry->get_filename(), part.entity->serialize());
454 save_json_to_file(part_location_entry->get_filename(), part.serialize());
455 }
456
457 void PartWizard::handle_link()
458 {
459 auto rows = pads_lb->get_selected_rows();
460 std::deque<PadEditor *> editors;
461
462 for (auto row : rows) {
463 auto ed = dynamic_cast<PadEditor *>(row->get_child());
464 editors.push_back(ed);
465 }
466 link_pads(editors);
467 update_pin_warnings();
468 }
469
470 void PartWizard::link_pads(const std::deque<PadEditor *> &editors)
471 {
472 PadEditor *target = nullptr;
473 if (editors.size() < 2)
474 return;
475 for (auto ed : editors) {
476 if (ed->pads.size() > 1) {
477 target = ed;
478 break;
479 }
480 }
481 if (!target) {
482 for (auto &ed : editors) {
483 if (ed->pin_name_entry->get_text().size())
484 target = ed;
485 }
486 }
487 if (!target) {
488 target = editors.front();
489 }
490 for (auto ed : editors) {
491 if (ed != target) {
492 target->pads.insert(ed->pads.begin(), ed->pads.end());
493 }
494 }
495 target->update_names();
496 for (auto ed : editors) {
497 if (ed != target) {
498 auto row = dynamic_cast<Gtk::ListBoxRow *>(ed->get_ancestor(GTK_TYPE_LIST_BOX_ROW));
499 delete row;
500 }
501 }
502 pads_lb->invalidate_sort();
503 }
504
505 void PartWizard::handle_unlink()
506 {
507 auto rows = pads_lb->get_selected_rows();
508 for (auto row : rows) {
509 auto ed = dynamic_cast<PadEditor *>(row->get_child());
510 if (ed->pads.size() > 1) {
511 auto pad_keep = *ed->pads.begin();
512 for (auto pad : ed->pads) {
513 if (pad != pad_keep) {
514 auto ed_new = PadEditor::create(pad, this);
515 ed_new->show_all();
516 pads_lb->append(*ed_new);
517 ed_new->unreference();
518 ed_new->pin_name_entry->set_text(ed->pin_name_entry->get_text());
519 ed_new->dir_combo->set_active_id(ed->dir_combo->get_active_id());
520 ed_new->combo_gate_entry->set_text(ed->combo_gate_entry->get_text());
521 ed_new->update_names();
522 }
523 }
524
525 ed->pads = {pad_keep};
526 ed->update_names();
527 }
528 }
529 pads_lb->invalidate_sort();
530 }
531
532 void PartWizard::handle_import()
533 {
534 GtkFileChooserNative *native =
535 gtk_file_chooser_native_new("Open", gobj(), GTK_FILE_CHOOSER_ACTION_OPEN, "_Open", "_Cancel");
536 auto chooser = Glib::wrap(GTK_FILE_CHOOSER(native));
537 {
538 auto filter = Gtk::FileFilter::create();
539 filter->set_name("Supported files (csv and json)");
540 filter->add_pattern("*.csv");
541 filter->add_pattern("*.json");
542 chooser->add_filter(filter);
543 }
544 {
545 auto filter = Gtk::FileFilter::create();
546 filter->set_name("All files");
547 filter->add_pattern("*");
548 chooser->add_filter(filter);
549 }
550 if (gtk_native_dialog_run(GTK_NATIVE_DIALOG(native)) == GTK_RESPONSE_ACCEPT) {
551 auto filename = chooser->get_filename();
552 try {
553 auto ifs = make_ifstream(filename);
554 if (!ifs.is_open()) {
555 throw std::runtime_error("file " + filename + " not opened");
556 }
557 if (endswith(filename, ".json")) {
558 json j;
559 ifs >> j;
560 ifs.close();
561 import_pads(j);
562 }
563 else {
564 CSV::Csv csv;
565 ifs >> csv;
566 ifs.close();
567 import_pads(csv);
568 }
569 }
570 catch (const std::exception &e) {
571 Gtk::MessageDialog md(*this, "Error importing", false /* use_markup */, Gtk::MESSAGE_ERROR,
572 Gtk::BUTTONS_OK);
573 md.set_secondary_text(e.what());
574 md.run();
575 }
576 catch (...) {
577 Gtk::MessageDialog md(*this, "Error importing", false /* use_markup */, Gtk::MESSAGE_ERROR,
578 Gtk::BUTTONS_OK);
579 md.set_secondary_text("unknown error");
580 md.run();
581 }
582 }
583 }
584
585 void PartWizard::import_pads(CSV::Csv &csv)
586 {
587 std::map<std::string, PadImportItem> items;
588 /* Expand number of fields for safe and easy access. */
589 csv.expand(4);
590 for (auto &line : csv) {
591 std::string pad_name = line[0];
592 std::string pin_name = line[1];
593 std::string dir = line[2];
594 std::string gate_name = line[3];
595 trim(pad_name);
596 if (pad_name.empty()) {
597 continue;
598 }
599 auto &item = items[pad_name];
600 item.pin = pin_name;
601 trim(dir);
602 for (auto &c : dir) {
603 c = tolower(c);
604 if (c == ' ') {
605 c = '_';
606 }
607 }
608 item.direction = Pin::direction_lut.lookup(dir, item.direction);
609 if (!gate_name.empty()) {
610 item.gate = gate_name;
611 }
612 if (line.size() >= 5) {
613 for (size_t i = 4; i < line.size(); i++) {
614 std::string alt = line[i];
615 trim(alt);
616 if (alt.size()) {
617 item.alt.push_back({alt, item.direction});
618 }
619 }
620 }
621 }
622 import_pads(items);
623 }
624
625 void PartWizard::import_pads(const json &j)
626 {
627 std::map<std::string, PadImportItem> items;
628 for (auto it = j.cbegin(); it != j.cend(); ++it) {
629 std::string pad_name = it.key();
630 const json &v = it.value();
631 auto &item = items[pad_name];
632 item.pin = v.value("pin", "");
633 item.gate = v.value("gate", "Main");
634 item.direction = Pin::direction_lut.lookup(v.value("direction", ""), Pin::Direction::INPUT);
635 if (v.count("alt")) {
636 for (const auto &a : v.at("alt")) {
637 if (a.is_string()) {
638 item.alt.push_back({a.get<std::string>(), item.direction});
639 }
640 else if (a.is_object()) {
641 item.alt.push_back({a.at("name").get<std::string>(),
642 Pin::direction_lut.lookup(a.value("direction", ""), item.direction)});
643 }
644 }
645 }
646 }
647 import_pads(items);
648 }
649
650 void PartWizard::import_pads(const std::map<std::string, PadImportItem> &items)
651 {
652 auto chs = pads_lb->get_children();
653 for (auto ch : chs) {
654 delete ch;
655 }
656 create_pad_editors();
657 frozen = true;
658 for (auto &ch : pads_lb->get_children()) {
659 auto ed = dynamic_cast<PadEditor *>(dynamic_cast<Gtk::ListBoxRow *>(ch)->get_child());
660 auto pad_name = (*ed->pads.begin())->name;
661 if (items.count(pad_name)) {
662 const auto &it = items.at(pad_name);
663 std::string pin_name = it.pin;
664 std::string gate_name = it.gate;
665 trim(pin_name);
666 trim(gate_name);
667 ed->pin_name_entry->set_text(pin_name);
668 ed->dir_combo->set_active_id(std::to_string(static_cast<int>(it.direction)));
669 ed->combo_gate_entry->set_text(gate_name);
670 ed->pin_names.clear();
671 for (const auto &alt : it.alt) {
672 ed->pin_names.emplace(std::piecewise_construct, std::forward_as_tuple(UUID::random()),
673 std::forward_as_tuple(alt.name, alt.direction));
674 }
675 ed->pin_names_editor->reload();
676 }
677 }
678 autolink_pads();
679 frozen = false;
680 update_gate_names();
681 update_pin_warnings();
682 }
683
684 void PartWizard::autolink_pads()
685 {
686 auto pin_names = get_pin_names();
687 for (const auto &it : pin_names) {
688 if (it.second.size() > 1) {
689 std::deque<PadEditor *> pads(it.second.begin(), it.second.end());
690 link_pads(pads);
691 }
692 }
693 }
694
695 void PartWizard::update_gate_names()
696 {
697 if (frozen)
698 return;
699 std::set<std::string> names;
700 names.insert("Main");
701 for (auto &ch : pads_lb->get_children()) {
702 auto ed = dynamic_cast<PadEditor *>(dynamic_cast<Gtk::ListBoxRow *>(ch)->get_child());
703 auto name = ed->get_gate_name();
704 if (name.size()) {
705 names.insert(name);
706 }
707 }
708 std::vector<std::string> names_sorted(names.begin(), names.end());
709 std::sort(names_sorted.begin(), names_sorted.end());
710 gate_name_store->freeze_notify();
711 gate_name_store->clear();
712 for (const auto &name : names_sorted) {
713 Gtk::TreeModel::Row row = *(gate_name_store->append());
714 row[list_columns.name] = name;
715 }
716 gate_name_store->thaw_notify();
717 }
718
719 void PartWizard::prepare_edit()
720 {
721 std::set<Gate *> gates_avail;
722 update_part();
723 auto children = edit_left_box->get_children();
724 for (auto ch : children) {
725 if (auto ed = dynamic_cast<GateEditorWizard *>(ch)) {
726 if (!entity->gates.count(ed->gate.uuid)) {
727 delete ed;
728 }
729 else {
730 gates_avail.insert(ed->gate);
731 }
732 }
733 }
734
735
736 for (auto &it : entity->gates) {
737 if (!gates_avail.count(&it.second)) {
738 auto ed = GateEditorWizard::create(&it.second, this);
739 edit_left_box->pack_start(*ed, false, false, 0);
740 ed->show_all();
741 ed->edit_symbol_button->signal_clicked().connect([this, ed] {
742 auto symbol_uuid = symbols.at(ed->gate->unit->uuid);
743 auto symbol_filename = pool.get_tmp_filename(ObjectType::SYMBOL, symbol_uuid);
744 {
745 auto sym = Symbol::new_from_file(symbol_filename, pool);
746 sym.name = ed->symbol_name_entry->get_text();
747 save_json_to_file(symbol_filename, sym.serialize());
748 }
749
750 auto proc = appwin.spawn(PoolProjectManagerProcess::Type::IMP_SYMBOL, {symbol_filename});
751 if (proc.spawned) {
752 processes.emplace(symbol_filename, proc.proc);
753 symbols_open.emplace(symbol_uuid);
754 proc.proc->signal_exited().connect(
755 [this, symbol_filename, symbol_uuid, ed](int status, bool modified) {
756 processes.erase(symbol_filename);
757 symbols_open.erase(symbol_uuid);
758 {
759 auto sym = Symbol::new_from_file(symbol_filename, pool);
760 ed->symbol_name_entry->set_text(sym.name);
761 }
762 update_can_finish();
763 update_symbol_pins_mapped();
764 });
765 }
766 update_can_finish();
767 });
768
769 ed->unreference();
770 }
771 }
772
773 update_symbol_pins_mapped();
774 }
775
776 std::map<std::pair<std::string, std::string>, std::set<class PadEditor *>> PartWizard::get_pin_names()
777 {
778 std::map<std::pair<std::string, std::string>, std::set<class PadEditor *>> pin_names;
779 for (auto &ch : pads_lb->get_children()) {
780 auto ed = dynamic_cast<PadEditor *>(dynamic_cast<Gtk::ListBoxRow *>(ch)->get_child());
781 std::pair<std::string, std::string> key(ed->combo_gate_entry->get_text(), ed->pin_name_entry->get_text());
782 if (key.second.size()) {
783 pin_names[key];
784 pin_names[key].insert(ed);
785 }
786 }
787 return pin_names;
788 }
789
790 void PartWizard::update_pin_warnings()
791 {
792 if (frozen)
793 return;
794 auto pin_names = get_pin_names();
795 bool has_warning = pin_names.size() == 0;
796 for (auto &it : pin_names) {
797 std::string icon_name;
798 if (it.second.size() > 1) {
799 icon_name = "dialog-warning-symbolic";
800 has_warning = true;
801 }
802 for (auto ed : it.second) {
803 ed->pin_name_entry->set_icon_from_icon_name(icon_name, Gtk::ENTRY_ICON_SECONDARY);
804 }
805 }
806 button_next->set_sensitive(!has_warning);
807 }
808
809 void PartWizard::update_part()
810 {
811 std::set<UUID> pins_used;
812 std::set<UUID> units_used;
813 part.pad_map.clear();
814 for (auto &ch : pads_lb->get_children()) {
815 auto ed = dynamic_cast<PadEditor *>(dynamic_cast<Gtk::ListBoxRow *>(ch)->get_child());
816 std::string pin_name = ed->pin_name_entry->get_text();
817 std::string gate_name = ed->combo_gate_entry->get_text();
818 if (pin_name.size()) {
819 if (!units.count(gate_name)) {
820 auto uu = UUID::random();
821 units.emplace(gate_name, std::make_shared<Unit>(uu));
822 }
823 auto u = units.at(gate_name);
824 units_used.insert(u->uuid);
825 Pin *pin = nullptr;
826 {
827 auto pi = std::find_if(u->pins.begin(), u->pins.end(),
828 [pin_name](const auto &a) { return a.second.primary_name == pin_name; });
829 if (pi != u->pins.end()) {
830 pin = &pi->second;
831 }
832 }
833 if (!pin) {
834 auto uu = UUID::random();
835 pin = &u->pins.emplace(uu, uu).first->second;
836 }
837 pins_used.insert(pin->uuid);
838 pin->primary_name = pin_name;
839 pin->direction = static_cast<Pin::Direction>(std::stoi(ed->dir_combo->get_active_id()));
840
841 pin->names = ed->pin_names;
842
843 Gate *gate = nullptr;
844 {
845 auto gi = std::find_if(entity->gates.begin(), entity->gates.end(),
846 [gate_name](const auto &a) { return a.second.name == gate_name; });
847 if (gi != entity->gates.end()) {
848 gate = &gi->second;
849 }
850 }
851 if (!gate) {
852 auto uu = UUID::random();
853 gate = &entity->gates.emplace(uu, uu).first->second;
854 }
855 gate->name = gate_name;
856 gate->unit = units.at(gate_name);
857
858 for (auto pad : ed->pads) {
859 part.pad_map.emplace(std::piecewise_construct, std::forward_as_tuple(pad->uuid),
860 std::forward_as_tuple(gate, pin));
861 }
862 }
863 }
864
865 map_erase_if(entity->gates, [units_used](auto x) { return units_used.count(x.second.unit->uuid) == 0; });
866
867 for (auto it = units.begin(); it != units.end();) {
868 auto uu = it->second->uuid;
869 if (units_used.count(uu) == 0) {
870 {
871 auto unit_filename = pool.get_tmp_filename(ObjectType::UNIT, uu);
872 auto fi = Gio::File::create_for_path(unit_filename);
873 fi->remove();
874 }
875
876 {
877 auto sym_filename = pool.get_tmp_filename(ObjectType::SYMBOL, symbols.at(uu));
878 auto fi = Gio::File::create_for_path(sym_filename);
879 fi->remove();
880 symbols.erase(uu);
881 }
882 units.erase(it++);
883 }
884 else {
885 it++;
886 }
887 }
888 for (auto &it : units) {
889 map_erase_if(it.second->pins, [pins_used](auto x) { return pins_used.count(x.second.uuid) == 0; });
890 }
891
892
893 for (const auto &it : units) {
894 if (!symbols.count(it.second->uuid)) {
895 Symbol sym(UUID::random());
896 sym.unit = it.second;
897 sym.name = "edit me";
898 auto filename = pool.get_tmp_filename(ObjectType::SYMBOL, sym.uuid);
899 save_json_to_file(filename, sym.serialize());
900 symbols.emplace(sym.unit->uuid, sym.uuid);
901 }
902 auto filename = pool.get_tmp_filename(ObjectType::UNIT, it.second->uuid);
903 save_json_to_file(filename, it.second->serialize());
904 }
905 }
906
907 void PartWizard::update_symbol_pins_mapped()
908 {
909 for (const auto &it : symbols) {
910 const auto &unit_uu = it.first;
911 auto symbol_filename = pool.get_tmp_filename(ObjectType::SYMBOL, it.second);
912 auto sym = Symbol::new_from_file(symbol_filename, pool);
913 auto n_pins = sym.pins.size();
914 symbol_pins_mapped[unit_uu] = n_pins;
915 }
916
917 auto children = edit_left_box->get_children();
918 for (auto ch : children) {
919 if (auto ed = dynamic_cast<GateEditorWizard *>(ch)) {
920 const auto &unit_uu = ed->gate->unit->uuid;
921 ed->update_symbol_pins(symbol_pins_mapped.at(unit_uu));
922 }
923 }
924 update_steps();
925 }
926
927 void PartWizard::autofill()
928 {
929 entity_name_entry->set_text(part_mpn_entry->get_text());
930 auto rel = get_rel_part_filename();
931 entity_location_entry->set_filename(Glib::build_filename(pool_base_path, "entities", rel));
932 auto children = edit_left_box->get_children();
933 for (auto ch : children) {
934 if (auto ed = dynamic_cast<GateEditorWizard *>(ch)) {
935 std::string suffix = ed->suffix_entry->get_text();
936 trim(suffix);
937 if (suffix.size()) {
938 auto txt = part_mpn_entry->get_text() + " " + suffix;
939 ed->unit_name_entry->set_text(txt);
940 if (ed->symbol_name_entry->get_sensitive())
941 ed->symbol_name_entry->set_text(txt);
942 }
943 else {
944 auto txt = part_mpn_entry->get_text();
945 ed->unit_name_entry->set_text(txt);
946 if (ed->symbol_name_entry->get_sensitive())
947 ed->symbol_name_entry->set_text(txt);
948 }
949 ed->unit_location_entry->set_filename(
950 Glib::build_filename(pool_base_path, "units", ed->get_suffixed_filename_from_part()));
951 ed->symbol_location_entry->set_filename(
952 Glib::build_filename(pool_base_path, "symbols", ed->get_suffixed_filename_from_part()));
953 }
954 }
955 }
956
957 void PartWizard::reload()
958 {
959 part.package = pool.get_package(part.package->uuid);
960 }
961
962 void PartWizard::update_can_finish()
963 {
964 bool editors_open = processes.size() > 0;
965 button_back->set_sensitive(!editors_open);
966 valid = true;
967
968 auto check_entry_not_empty = [this](Gtk::Entry *e, const std::string &msg, bool *v = nullptr) {
969 std::string t = e->get_text();
970 trim(t);
971 if (!t.size()) {
972 entry_set_warning(e, msg);
973 valid = false;
974 }
975 else {
976 entry_set_warning(e, "");
977 }
978 if (v)
979 *v = t.size();
980 };
981
982 check_entry_not_empty(part_mpn_entry, "MPN is empty", &mpn_valid);
983 check_entry_not_empty(entity_name_entry, "Entity name is empty");
984 check_entry_not_empty(entity_prefix_entry, "Entity prefix is empty");
985 part_filename_valid = part_location_entry->check_ends_json();
986 valid = part_filename_valid && valid;
987 valid = entity_location_entry->check_ends_json() && valid;
988
989 std::set<std::string> symbol_filenames;
990 std::set<std::string> unit_filenames;
991 std::set<std::string> suffixes;
992 std::set<std::string> unit_names;
993
994 gates_valid = true;
995 int n_gates = entity->gates.size();
996
997 auto children = edit_left_box->get_children();
998 for (auto ch : children) {
999 if (auto ed = dynamic_cast<GateEditorWizard *>(ch)) {
1000 ed->unit_location_entry->set_warning("");
1001 ed->symbol_location_entry->set_warning("");
1002 entry_set_warning(ed->suffix_entry, "");
1003 entry_set_warning(ed->unit_name_entry, "");
1004
1005 valid = ed->unit_location_entry->check_ends_json() && valid;
1006 valid = ed->symbol_location_entry->check_ends_json() && valid;
1007
1008 check_entry_not_empty(ed->unit_name_entry, "Unit name is empty");
1009 std::string unit_filename = ed->unit_location_entry->get_filename();
1010 trim(unit_filename);
1011
1012 if (!unit_filenames.insert(unit_filename).second) {
1013 ed->unit_location_entry->set_warning("Duplicate unit filename");
1014 valid = false;
1015 }
1016
1017 std::string symbol_filename = ed->symbol_location_entry->get_filename();
1018 trim(symbol_filename);
1019
1020 if (!symbol_filenames.insert(symbol_filename).second) {
1021 ed->symbol_location_entry->set_warning("Duplicate symbol filename");
1022 valid = false;
1023 }
1024
1025 std::string suffix = ed->suffix_entry->get_text();
1026 trim(suffix);
1027 if (!suffixes.insert(suffix).second) {
1028 entry_set_warning(ed->suffix_entry, "Duplicate unit suffix");
1029 valid = false;
1030 gates_valid = false;
1031 }
1032 if (suffix.size() == 0 && (n_gates > 1)) {
1033 entry_set_warning(ed->suffix_entry, "Unit suffix is empty");
1034 valid = false;
1035 gates_valid = false;
1036 }
1037
1038 std::string unit_name = ed->unit_name_entry->get_text();
1039 trim(unit_name);
1040 if (!unit_names.insert(unit_name).second) {
1041 entry_set_warning(ed->unit_name_entry, "Duplicate unit name");
1042 valid = false;
1043 }
1044
1045 ed->set_can_edit_symbol_name(symbols_open.count(symbols.at(ed->gate->unit->uuid)) == 0);
1046 }
1047 }
1048 update_steps();
1049 button_finish->set_sensitive(!editors_open && valid);
1050 }
1051
1052 void PartWizard::update_steps()
1053 {
1054 auto chs = steps_grid->get_children();
1055 for (auto ch : chs) {
1056 delete ch;
1057 }
1058 int top = 0;
1059 auto add_step = [this, &top](const std::string &t, int st) {
1060 auto la = Gtk::manage(new Gtk::Label(t));
1061 la->set_halign(Gtk::ALIGN_START);
1062 steps_grid->attach(*la, 1, top, 1, 1);
1063
1064 auto im = Gtk::manage(new Gtk::Image());
1065 if (st == 1) {
1066 im->set_from_icon_name("pan-end-symbolic", Gtk::ICON_SIZE_BUTTON);
1067 }
1068 else if (st == 2) {
1069 im->set_from_icon_name("object-select-symbolic", Gtk::ICON_SIZE_BUTTON);
1070 }
1071 steps_grid->attach(*im, 0, top, 1, 1);
1072
1073 top++;
1074 };
1075
1076 int progress = 0;
1077
1078 auto compare_progress = [&progress](int x) {
1079 if (x == progress)
1080 return 1;
1081 else if (x < progress)
1082 return 2;
1083 return 0;
1084 };
1085
1086 bool all_symbol_pins_mapped = true;
1087 for (const auto &it : units) {
1088 unsigned int n_mapped = 0;
1089 if (symbol_pins_mapped.count(it.second->uuid)) {
1090 n_mapped = symbol_pins_mapped.at(it.second->uuid);
1091 }
1092 if (n_mapped < it.second->pins.size()) {
1093 all_symbol_pins_mapped = false;
1094 break;
1095 }
1096 }
1097
1098 if (entity->gates.size() == 1) {
1099 if (mpn_valid) {
1100 progress = 1;
1101 if (part_filename_valid) {
1102 progress = 2;
1103 if (valid) {
1104 progress = 3;
1105 if (all_symbol_pins_mapped) {
1106 progress = 4;
1107 }
1108 }
1109 }
1110 }
1111 add_step("Enter MPN (and Manufacturer)", compare_progress(0));
1112 add_step("Enter part filename", compare_progress(1));
1113 add_step("Press Autofill", compare_progress(2));
1114 add_step("Edit Symbol", compare_progress(3));
1115 }
1116 else {
1117 if (mpn_valid) {
1118 progress = 1;
1119 if (part_filename_valid) {
1120 progress = 2;
1121 if (gates_valid) {
1122 progress = 3;
1123 if (valid) {
1124 progress = 4;
1125 if (all_symbol_pins_mapped) {
1126 progress = 5;
1127 }
1128 }
1129 }
1130 }
1131 }
1132 add_step("Enter MPN (and Manufacturer)", compare_progress(0));
1133 add_step("Enter Part filename", compare_progress(1));
1134 add_step("Enter Gate suffixes", compare_progress(2));
1135 add_step("Press Autofill", compare_progress(3));
1136 add_step("Edit Symbols", compare_progress(4));
1137 }
1138
1139
1140 steps_grid->show_all();
1141 }
1142
1143 PartWizard *PartWizard::create(const UUID &pkg_uuid, const std::string &bp, class Pool &po,
1144 class PoolProjectManagerAppWindow &aw)
1145 {
1146 PartWizard *w;
1147 Glib::RefPtr<Gtk::Builder> x = Gtk::Builder::create();
1148 x->add_from_resource(
1149 "/org/horizon-eda/horizon/pool-prj-mgr/pool-mgr/part_wizard/"
1150 "part_wizard.ui");
1151 x->get_widget_derived("part_wizard", w, pkg_uuid, bp, po, aw);
1152 return w;
1153 }
1154
1155 PartWizard::~PartWizard()
1156 {
1157 for (auto &it : units) {
1158 auto filename = pool.get_tmp_filename(ObjectType::UNIT, it.second->uuid);
1159 Gio::File::create_for_path(filename)->remove();
1160 }
1161 for (auto &it : symbols) {
1162 auto filename = pool.get_tmp_filename(ObjectType::SYMBOL, it.second);
1163 Gio::File::create_for_path(filename)->remove();
1164 }
1165 }
1166 } // namespace horizon