1 #include "unit_info_box.hpp"
2 #include "where_used_box.hpp"
3 #include "util/util.hpp"
4 #include "util/gtk_util.hpp"
5
6 namespace horizon {
7
8 class PinDirectionMap {
9 public:
get()10 const std::map<Pin::Direction, std::string> &get()
11 {
12 if (!m) {
13 m = new std::map<Pin::Direction, std::string>;
14 for (const auto &it : Pin::direction_names) {
15 m->emplace(it.first, it.second);
16 }
17 }
18 return *m;
19 }
20
21 private:
22 std::map<Pin::Direction, std::string> *m = nullptr;
23 };
24
25 static PinDirectionMap pin_direction_map;
26
UnitInfoBox(BaseObjectType * cobject,const Glib::RefPtr<Gtk::Builder> & x,IPool & p)27 UnitInfoBox::UnitInfoBox(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, IPool &p)
28 : Gtk::Box(cobject), pool(p)
29 {
30
31 where_used_box = Gtk::manage(new WhereUsedBox(pool));
32 where_used_box->show();
33 where_used_box->signal_goto().connect([this](ObjectType type, UUID uu) { s_signal_goto.emit(type, uu); });
34 Gtk::Box *bo;
35 x->get_widget("info_where_used_box", bo);
36 bo->pack_start(*where_used_box, true, true, 0);
37 where_used_box->show();
38
39 Gtk::Grid *grid;
40 x->get_widget("info_grid", grid);
41 {
42 auto la = Gtk::manage(new Gtk::Label("Name"));
43 la->get_style_context()->add_class("dim-label");
44 la->set_xalign(1);
45 grid->attach(*la, 0, 0, 1, 1);
46 label_name = Gtk::manage(new Gtk::Label);
47 label_name->set_xalign(0);
48 label_name->set_hexpand(true);
49 grid->attach(*label_name, 1, 0, 1, 1);
50 }
51 {
52 auto la = Gtk::manage(new Gtk::Label("Manufacturer"));
53 la->get_style_context()->add_class("dim-label");
54 la->set_xalign(1);
55 grid->attach(*la, 2, 0, 1, 1);
56 label_manufacturer = Gtk::manage(new Gtk::Label);
57 label_manufacturer->set_xalign(0);
58 label_manufacturer->set_hexpand(true);
59 grid->attach(*label_manufacturer, 3, 0, 1, 1);
60 }
61
62
63 x->get_widget("info_tv", view);
64
65 store = Gtk::ListStore::create(list_columns);
66 store->set_sort_func(list_columns.primary_name,
67 [this](const Gtk::TreeModel::iterator &ia, const Gtk::TreeModel::iterator &ib) {
68 Gtk::TreeModel::Row ra = *ia;
69 Gtk::TreeModel::Row rb = *ib;
70 Glib::ustring a = ra[list_columns.primary_name];
71 Glib::ustring b = rb[list_columns.primary_name];
72 return strcmp_natural(a, b);
73 });
74
75
76 view->set_model(store);
77 {
78 auto cr = Gtk::manage(new Gtk::CellRendererText());
79 auto tvc = Gtk::manage(new Gtk::TreeViewColumn("Direction", *cr));
80 tvc->set_cell_data_func(*cr, [this](Gtk::CellRenderer *tcr, const Gtk::TreeModel::iterator &it) {
81 Gtk::TreeModel::Row row = *it;
82 auto mcr = dynamic_cast<Gtk::CellRendererText *>(tcr);
83 mcr->property_text() = pin_direction_map.get().at(row[list_columns.direction]);
84 });
85 view->append_column(*tvc);
86 }
87 {
88 auto i = view->append_column("Name", list_columns.primary_name);
89 view->get_column(i - 1)->set_sort_column(list_columns.primary_name);
90 }
91 // view->append_column("Swap group", list_columns.swap_group);
92 view->append_column("Alt. names", list_columns.alt_names);
93 }
94
create(IPool & p)95 UnitInfoBox *UnitInfoBox::create(IPool &p)
96 {
97 UnitInfoBox *w;
98 Glib::RefPtr<Gtk::Builder> x = Gtk::Builder::create();
99 x->add_from_resource("/org/horizon-eda/horizon/pool-prj-mgr/window.ui", "info_box");
100 x->get_widget_derived("info_box", w, p);
101 w->reference();
102 return w;
103 }
104
load(const Unit * unit)105 void UnitInfoBox::load(const Unit *unit)
106 {
107 if (unit)
108 where_used_box->load(ObjectType::UNIT, unit->uuid);
109 else
110 where_used_box->clear();
111 label_manufacturer->set_label(unit ? unit->manufacturer : "");
112 label_name->set_label(unit ? unit->name : "");
113
114 store->clear();
115 if (unit) {
116 for (const auto &it : unit->pins) {
117 Gtk::TreeModel::Row row = *store->append();
118 row[list_columns.direction] = it.second.direction;
119 row[list_columns.primary_name] = it.second.primary_name;
120 std::string alts;
121 for (const auto &it_alt : it.second.names) {
122 alts += it_alt + ", ";
123 }
124 if (alts.size()) {
125 alts.pop_back();
126 alts.pop_back();
127 }
128 row[list_columns.alt_names] = alts;
129 }
130 }
131 }
132 } // namespace horizon
133