| 1 |
#include "part_editor.hpp" |
| 2 |
#include "dialogs/pool_browser_dialog.hpp" |
| 3 |
#include "widgets/pool_browser_package.hpp" |
| 4 |
#include "widgets/pool_browser_part.hpp" |
| 5 |
#include "widgets/tag_entry.hpp" |
| 6 |
#include <iostream> |
| 7 |
#include "pool/part.hpp" |
| 8 |
#include "pool/pool_parametric.hpp" |
| 9 |
#include "util/util.hpp" |
| 10 |
#include "util/gtk_util.hpp" |
| 11 |
#include <glibmm.h> |
| 12 |
#include "util/pool_completion.hpp" |
| 13 |
#include "parametric.hpp" |
| 14 |
#include "pool/ipool.hpp" |
| 15 |
#include <range/v3/view.hpp> |
| 16 |
#include "util/range_util.hpp" |
| 17 |
#include "nlohmann/json.hpp" |
| 18 |
#include "checks/check_part.hpp" |
| 19 |
|
| 20 |
namespace horizon { |
| 21 |
class EntryWithInheritance : public Gtk::Box { |
| 22 |
public: |
| 23 |
EntryWithInheritance() |
| 24 |
: Glib::ObjectBase(typeid(EntryWithInheritance)), Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0), |
| 25 |
p_property_inherit(*this, "inherit"), p_property_can_inherit(*this, "can-inherit") |
| 26 |
{ |
| 27 |
entry = Gtk::manage(new Gtk::Entry); |
| 28 |
entry_add_sanitizer(entry); |
| 29 |
button = Gtk::manage(new Gtk::ToggleButton("Inherit")); |
| 30 |
|
| 31 |
pack_start(*entry, true, true, 0); |
| 32 |
pack_start(*button, false, false, 0); |
| 33 |
get_style_context()->add_class("linked"); |
| 34 |
entry->show(); |
| 35 |
button->show(); |
| 36 |
|
| 37 |
inh_binding = Glib::Binding::bind_property(button->property_active(), property_inherit(), |
| 38 |
Glib::BINDING_BIDIRECTIONAL); |
| 39 |
property_inherit().signal_changed().connect([this] { |
| 40 |
entry->set_sensitive(!property_inherit()); |
| 41 |
if (property_inherit()) { |
| 42 |
text_this = entry->get_text(); |
| 43 |
entry->set_text(text_inherit); |
| 44 |
} |
| 45 |
else { |
| 46 |
entry->set_text(text_this); |
| 47 |
} |
| 48 |
}); |
| 49 |
|
| 50 |
property_can_inherit().signal_changed().connect([this] { |
| 51 |
button->set_sensitive(property_can_inherit()); |
| 52 |
if (!property_can_inherit()) { |
| 53 |
property_inherit() = false; |
| 54 |
} |
| 55 |
}); |
| 56 |
} |
| 57 |
void set_text_inherit(const std::string &s) |
| 58 |
{ |
| 59 |
text_inherit = s; |
| 60 |
if (property_inherit()) { |
| 61 |
entry->set_text(text_inherit); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void set_text_this(const std::string &s) |
| 66 |
{ |
| 67 |
text_this = s; |
| 68 |
if (!property_inherit()) { |
| 69 |
entry->set_text(text_this); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
std::pair<bool, std::string> get_as_pair() |
| 74 |
{ |
| 75 |
if (property_inherit()) { |
| 76 |
return {true, text_this}; |
| 77 |
} |
| 78 |
else { |
| 79 |
return {false, entry->get_text()}; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
Gtk::Entry *entry = nullptr; |
| 84 |
Gtk::ToggleButton *button = nullptr; |
| 85 |
|
| 86 |
|
| 87 |
Glib::PropertyProxy<bool> property_inherit() |
| 88 |
{ |
| 89 |
return p_property_inherit.get_proxy(); |
| 90 |
} |
| 91 |
Glib::PropertyProxy<bool> property_can_inherit() |
| 92 |
{ |
| 93 |
return p_property_can_inherit.get_proxy(); |
| 94 |
} |
| 95 |
|
| 96 |
private: |
| 97 |
Glib::Property<bool> p_property_inherit; |
| 98 |
Glib::Property<bool> p_property_can_inherit; |
| 99 |
Glib::RefPtr<Glib::Binding> inh_binding; |
| 100 |
std::string text_inherit; |
| 101 |
std::string text_this; |
| 102 |
}; |
| 103 |
|
| 104 |
class OrderableMPNEditor : public Gtk::Box, public Changeable { |
| 105 |
public: |
| 106 |
OrderableMPNEditor(Part &p, const UUID &uu) : Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0), part(p), uuid(uu) |
| 107 |
{ |
| 108 |
get_style_context()->add_class("linked"); |
| 109 |
entry = Gtk::manage(new Gtk::Entry); |
| 110 |
entry->show(); |
| 111 |
entry->set_text(part.orderable_MPNs.at(uuid)); |
| 112 |
entry->signal_changed().connect([this] { s_signal_changed.emit(); }); |
| 113 |
entry_add_sanitizer(entry); |
| 114 |
pack_start(*entry, true, true, 0); |
| 115 |
|
| 116 |
auto bu = Gtk::manage(new Gtk::Button()); |
| 117 |
bu->set_image_from_icon_name("list-remove-symbolic"); |
| 118 |
bu->show(); |
| 119 |
pack_start(*bu, false, false, 0); |
| 120 |
bu->signal_clicked().connect([this] { |
| 121 |
part.orderable_MPNs.erase(uuid); |
| 122 |
s_signal_changed.emit(); |
| 123 |
delete this; |
| 124 |
}); |
| 125 |
} |
| 126 |
std::string get_MPN() |
| 127 |
{ |
| 128 |
return entry->get_text(); |
| 129 |
} |
| 130 |
const UUID &get_uuid() const |
| 131 |
{ |
| 132 |
return uuid; |
| 133 |
} |
| 134 |
|
| 135 |
void focus() |
| 136 |
{ |
| 137 |
entry->grab_focus(); |
| 138 |
} |
| 139 |
|
| 140 |
private: |
| 141 |
Part ∂ |
| 142 |
UUID uuid; |
| 143 |
Gtk::Entry *entry = nullptr; |
| 144 |
}; |
| 145 |
|
| 146 |
class FlagEditor : public Gtk::Box, public Changeable { |
| 147 |
public: |
| 148 |
FlagEditor() : Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0) |
| 149 |
{ |
| 150 |
get_style_context()->add_class("linked"); |
| 151 |
static const std::vector<std::pair<Part::FlagState, std::string>> states = { |
| 152 |
{Part::FlagState::SET, "Yes"}, |
| 153 |
{Part::FlagState::CLEAR, "No"}, |
| 154 |
{Part::FlagState::INHERIT, "Inherit"}, |
| 155 |
}; |
| 156 |
Gtk::RadioButton *group = nullptr; |
| 157 |
for (const auto &[st, name] : states) { |
| 158 |
auto bu = Gtk::manage(new Gtk::RadioButton(name)); |
| 159 |
buttons.emplace(st, bu); |
| 160 |
bu->set_mode(false); |
| 161 |
if (group) |
| 162 |
bu->join_group(*group); |
| 163 |
else |
| 164 |
group = bu; |
| 165 |
bu->show(); |
| 166 |
pack_start(*bu, false, false, 0); |
| 167 |
bu->signal_toggled().connect([this, bu] { |
| 168 |
if (bu->get_active()) { |
| 169 |
s_signal_changed.emit(); |
| 170 |
} |
| 171 |
}); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
Part::FlagState get_state() const |
| 176 |
{ |
| 177 |
for (auto [st, button] : buttons) { |
| 178 |
if (button->get_active()) |
| 179 |
return st; |
| 180 |
} |
| 181 |
return Part::FlagState::CLEAR; |
| 182 |
} |
| 183 |
|
| 184 |
void set_state(Part::FlagState st) |
| 185 |
{ |
| 186 |
buttons.at(st)->set_active(true); |
| 187 |
} |
| 188 |
|
| 189 |
void set_can_inherit(bool inh) |
| 190 |
{ |
| 191 |
buttons.at(Part::FlagState::INHERIT)->set_sensitive(inh); |
| 192 |
} |
| 193 |
|
| 194 |
private: |
| 195 |
std::map<Part::FlagState, Gtk::RadioButton *> buttons; |
| 196 |
}; |
| 197 |
|
| 198 |
PartEditor::PartEditor(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, const std::string &fn, IPool &po, |
| 199 |
PoolParametric &pp) |
| 200 |
: Gtk::Box(cobject), PoolEditorBase(fn, po), |
| 201 |
part(filename.size() ? Part::new_from_file(filename, pool) : Part(UUID::random())), pool_parametric(pp) |
| 202 |
{ |
| 203 |
history_append("init"); |
| 204 |
auto add_entry = [x](const char *name) { |
| 205 |
Gtk::Box *t; |
| 206 |
x->get_widget(name, t); |
| 207 |
auto e = Gtk::manage(new EntryWithInheritance); |
| 208 |
e->show(); |
| 209 |
t->pack_start(*e, true, true, 0); |
| 210 |
return e; |
| 211 |
}; |
| 212 |
|
| 213 |
x->get_widget("entity_label", w_entity_label); |
| 214 |
x->get_widget("package_label", w_package_label); |
| 215 |
x->get_widget("change_package_button", w_change_package_button); |
| 216 |
x->get_widget("set_base_menu_item", w_set_base_menu_item); |
| 217 |
x->get_widget("clear_base_menu_item", w_clear_base_menu_item); |
| 218 |
x->get_widget("create_base_menu_item", w_create_base_menu_item); |
| 219 |
{ |
| 220 |
Gtk::Box *model_box; |
| 221 |
x->get_widget("model_box", model_box); |
| 222 |
w_model_combo = Gtk::manage(new std::remove_pointer_t<decltype(w_model_combo)>); |
| 223 |
w_model_combo->get_cr_text().property_ellipsize() = Pango::ELLIPSIZE_END; |
| 224 |
w_model_combo->show(); |
| 225 |
model_box->pack_start(*w_model_combo, true, true, 0); |
| 226 |
} |
| 227 |
x->get_widget("model_inherit", w_model_inherit); |
| 228 |
x->get_widget("base_label", w_base_label); |
| 229 |
{ |
| 230 |
Gtk::Box *tag_box; |
| 231 |
x->get_widget("tags", tag_box); |
| 232 |
w_tags = Gtk::manage(new TagEntry(pool, ObjectType::PART, true)); |
| 233 |
w_tags->show(); |
| 234 |
tag_box->pack_start(*w_tags, true, true, 0); |
| 235 |
} |
| 236 |
x->get_widget("tags_inherit", w_tags_inherit); |
| 237 |
x->get_widget("tags_inherited", w_tags_inherited); |
| 238 |
x->get_widget("tv_pins", w_tv_pins); |
| 239 |
x->get_widget("tv_pads", w_tv_pads); |
| 240 |
x->get_widget("button_map", w_button_map); |
| 241 |
x->get_widget("button_unmap", w_button_unmap); |
| 242 |
x->get_widget("button_automap", w_button_automap); |
| 243 |
x->get_widget("button_select_pin", w_button_select_pin); |
| 244 |
x->get_widget("button_select_pads", w_button_select_pads); |
| 245 |
x->get_widget("button_copy_from_other", w_button_copy_from_other); |
| 246 |
x->get_widget("pin_stat", w_pin_stat); |
| 247 |
x->get_widget("pad_stat", w_pad_stat); |
| 248 |
x->get_widget("parametric_box", w_parametric_box); |
| 249 |
x->get_widget("parametric_table_combo", w_parametric_table_combo); |
| 250 |
x->get_widget("orderable_MPNs_label", w_orderable_MPNs_label); |
| 251 |
x->get_widget("orderable_MPNs_box", w_orderable_MPNs_box); |
| 252 |
x->get_widget("orderable_MPNs_add_button", w_orderable_MPNs_add_button); |
| 253 |
x->get_widget("flags_grid", w_flags_grid); |
| 254 |
x->get_widget("flags_label", w_flags_label); |
| 255 |
x->get_widget("override_prefix_inherit_button", w_override_prefix_inherit_button); |
| 256 |
x->get_widget("override_prefix_no_button", w_override_prefix_no_button); |
| 257 |
x->get_widget("override_prefix_yes_button", w_override_prefix_yes_button); |
| 258 |
x->get_widget("override_prefix_entry", w_override_prefix_entry); |
| 259 |
sg_parametric_label = decltype(sg_parametric_label)::cast_dynamic(x->get_object("sg_parametric_label")); |
| 260 |
|
| 261 |
override_prefix_radio_buttons = {{Part::OverridePrefix::NO, w_override_prefix_no_button}, |
| 262 |
{Part::OverridePrefix::INHERIT, w_override_prefix_inherit_button}, |
| 263 |
{Part::OverridePrefix::YES, w_override_prefix_yes_button}}; |
| 264 |
|
| 265 |
label_make_item_link(*w_entity_label, ObjectType::ENTITY); |
| 266 |
label_make_item_link(*w_base_label, ObjectType::PART); |
| 267 |
label_make_item_link(*w_package_label, ObjectType::PACKAGE); |
| 268 |
|
| 269 |
w_mpn = add_entry("part_mpn_box"); |
| 270 |
w_value = add_entry("part_value_box"); |
| 271 |
w_manufacturer = add_entry("part_manufacturer_box"); |
| 272 |
w_manufacturer->entry->set_completion(create_pool_manufacturer_completion(pool)); |
| 273 |
w_datasheet = add_entry("part_datasheet_box"); |
| 274 |
w_description = add_entry("part_description_box"); |
| 275 |
|
| 276 |
attr_editors.emplace(horizon::Part::Attribute::MPN, w_mpn); |
| 277 |
attr_editors.emplace(horizon::Part::Attribute::VALUE, w_value); |
| 278 |
attr_editors.emplace(horizon::Part::Attribute::MANUFACTURER, w_manufacturer); |
| 279 |
attr_editors.emplace(horizon::Part::Attribute::DESCRIPTION, w_description); |
| 280 |
attr_editors.emplace(horizon::Part::Attribute::DATASHEET, w_datasheet); |
| 281 |
|
| 282 |
|
| 283 |
for (auto &it : attr_editors) { |
| 284 |
it.second->entry->signal_changed().connect([this, it] { |
| 285 |
if (is_loading()) |
| 286 |
return; |
| 287 |
part.attributes[it.first] = it.second->get_as_pair(); |
| 288 |
set_needs_save(); |
| 289 |
}); |
| 290 |
it.second->button->signal_toggled().connect([this, it] { |
| 291 |
if (is_loading()) |
| 292 |
return; |
| 293 |
part.attributes[it.first] = it.second->get_as_pair(); |
| 294 |
set_needs_save(); |
| 295 |
}); |
| 296 |
} |
| 297 |
|
| 298 |
w_change_package_button->signal_clicked().connect(sigc::mem_fun(*this, &PartEditor::change_package)); |
| 299 |
w_set_base_menu_item->signal_activate().connect(sigc::mem_fun(*this, &PartEditor::change_base)); |
| 300 |
w_clear_base_menu_item->signal_activate().connect(sigc::mem_fun(*this, &PartEditor::clear_base)); |
| 301 |
w_create_base_menu_item->signal_activate().connect(sigc::mem_fun(*this, &PartEditor::create_base)); |
| 302 |
|
| 303 |
|
| 304 |
w_tags_inherit->signal_toggled().connect([this] { |
| 305 |
if (is_loading()) |
| 306 |
return; |
| 307 |
part.inherit_tags = w_tags_inherit->get_active(); |
| 308 |
set_needs_save(); |
| 309 |
}); |
| 310 |
|
| 311 |
w_tags->signal_changed().connect([this] { |
| 312 |
if (is_loading()) |
| 313 |
return; |
| 314 |
part.tags = w_tags->get_tags(); |
| 315 |
set_needs_save(); |
| 316 |
}); |
| 317 |
|
| 318 |
pin_store = Gtk::ListStore::create(pin_list_columns); |
| 319 |
pin_store->set_sort_func(pin_list_columns.pin_name, |
| 320 |
[this](const Gtk::TreeModel::iterator &ia, const Gtk::TreeModel::iterator &ib) { |
| 321 |
Gtk::TreeModel::Row ra = *ia; |
| 322 |
Gtk::TreeModel::Row rb = *ib; |
| 323 |
Glib::ustring a = ra[pin_list_columns.pin_name]; |
| 324 |
Glib::ustring b = rb[pin_list_columns.pin_name]; |
| 325 |
return strcmp_natural(a, b); |
| 326 |
}); |
| 327 |
w_tv_pins->set_model(pin_store); |
| 328 |
|
| 329 |
w_tv_pins->append_column("Gate", pin_list_columns.gate_name); |
| 330 |
w_tv_pins->append_column("Pin", pin_list_columns.pin_name); |
| 331 |
{ |
| 332 |
auto cr = Gtk::manage(new Gtk::CellRendererPixbuf()); |
| 333 |
cr->property_icon_name() = "object-select-symbolic"; |
| 334 |
cr->property_xalign() = 0; |
| 335 |
auto tvc = Gtk::manage(new Gtk::TreeViewColumn("Mapped", *cr)); |
| 336 |
tvc->add_attribute(cr->property_visible(), pin_list_columns.mapped); |
| 337 |
w_tv_pins->append_column(*tvc); |
| 338 |
} |
| 339 |
|
| 340 |
w_tv_pins->get_column(0)->set_sort_column(pin_list_columns.gate_name); |
| 341 |
w_tv_pins->get_column(1)->set_sort_column(pin_list_columns.pin_name); |
| 342 |
|
| 343 |
pin_store->set_sort_column(pin_list_columns.pin_name, Gtk::SORT_ASCENDING); |
| 344 |
Glib::signal_timeout().connect_once( |
| 345 |
sigc::track_obj([this] { pin_store->set_sort_column(pin_list_columns.gate_name, Gtk::SORT_ASCENDING); }, |
| 346 |
*this), |
| 347 |
10); |
| 348 |
|
| 349 |
pad_store = Gtk::ListStore::create(pad_list_columns); |
| 350 |
pad_store->set_sort_func(pad_list_columns.pad_name, |
| 351 |
[this](const Gtk::TreeModel::iterator &ia, const Gtk::TreeModel::iterator &ib) { |
| 352 |
Gtk::TreeModel::Row ra = *ia; |
| 353 |
Gtk::TreeModel::Row rb = *ib; |
| 354 |
Glib::ustring a = ra[pad_list_columns.pad_name]; |
| 355 |
Glib::ustring b = rb[pad_list_columns.pad_name]; |
| 356 |
return strcmp_natural(a, b); |
| 357 |
}); |
| 358 |
w_tv_pads->set_model(pad_store); |
| 359 |
w_tv_pads->append_column("Pad", pad_list_columns.pad_name); |
| 360 |
|
| 361 |
w_tv_pads->append_column("Gate", pad_list_columns.gate_name); |
| 362 |
w_tv_pads->append_column("Pin", pad_list_columns.pin_name); |
| 363 |
w_tv_pads->get_column(0)->set_sort_column(pad_list_columns.pad_name); |
| 364 |
|
| 365 |
pad_store->set_sort_column(pad_list_columns.pad_name, Gtk::SORT_ASCENDING); |
| 366 |
|
| 367 |
w_tv_pads->get_selection()->signal_changed().connect(sigc::mem_fun(*this, &PartEditor::update_map_buttons)); |
| 368 |
w_tv_pins->get_selection()->signal_changed().connect(sigc::mem_fun(*this, &PartEditor::update_map_buttons)); |
| 369 |
|
| 370 |
w_button_unmap->signal_clicked().connect([this] { |
| 371 |
auto sel = w_tv_pads->get_selection(); |
| 372 |
for (auto &path : sel->get_selected_rows()) { |
| 373 |
auto it = pad_store->get_iter(path); |
| 374 |
Gtk::TreeModel::Row row = *it; |
| 375 |
row[pad_list_columns.gate_name] = ""; |
| 376 |
row[pad_list_columns.pin_name] = ""; |
| 377 |
row[pad_list_columns.pin_uuid] = UUID(); |
| 378 |
row[pad_list_columns.gate_uuid] = UUID(); |
| 379 |
} |
| 380 |
update_mapped(); |
| 381 |
update_pad_map(); |
| 382 |
set_needs_save(); |
| 383 |
}); |
| 384 |
|
| 385 |
w_tv_pins->signal_row_activated().connect([this](const Gtk::TreeModel::Path &path, Gtk::TreeViewColumn *col) { |
| 386 |
auto it_pin = pin_store->get_iter(path); |
| 387 |
map_pin(it_pin); |
| 388 |
}); |
| 389 |
|
| 390 |
w_button_map->signal_clicked().connect([this] { |
| 391 |
auto pin_sel = w_tv_pins->get_selection(); |
| 392 |
auto it_pin = pin_sel->get_selected(); |
| 393 |
map_pin(it_pin); |
| 394 |
}); |
| 395 |
|
| 396 |
w_button_automap->signal_clicked().connect([this] { |
| 397 |
auto sel = w_tv_pads->get_selection(); |
| 398 |
for (auto &path : sel->get_selected_rows()) { |
| 399 |
auto it = pad_store->get_iter(path); |
| 400 |
Gtk::TreeModel::Row row = *it; |
| 401 |
Glib::ustring pad_name = row[pad_list_columns.pad_name]; |
| 402 |
for (const auto &it_pin : pin_store->children()) { |
| 403 |
Gtk::TreeModel::Row row_pin = *it_pin; |
| 404 |
Glib::ustring pin_name = row_pin[pin_list_columns.pin_name]; |
| 405 |
if (pin_name == pad_name) { |
| 406 |
row[pad_list_columns.gate_name] = row_pin.get_value(pin_list_columns.gate_name); |
| 407 |
row[pad_list_columns.pin_name] = row_pin.get_value(pin_list_columns.pin_name); |
| 408 |
row[pad_list_columns.pin_uuid] = row_pin.get_value(pin_list_columns.pin_uuid); |
| 409 |
row[pad_list_columns.gate_uuid] = row_pin.get_value(pin_list_columns.gate_uuid); |
| 410 |
break; |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
update_mapped(); |
| 415 |
update_pad_map(); |
| 416 |
set_needs_save(); |
| 417 |
}); |
| 418 |
|
| 419 |
w_button_select_pin->signal_clicked().connect([this] { |
| 420 |
auto sel = w_tv_pads->get_selection(); |
| 421 |
auto rows = sel->get_selected_rows(); |
| 422 |
if (rows.size() != 1) |
| 423 |
return; |
| 424 |
auto it_pad = pad_store->get_iter(rows.front()); |
| 425 |
Gtk::TreeModel::Row row_pad = *it_pad; |
| 426 |
auto gate_uuid = row_pad[pad_list_columns.gate_uuid]; |
| 427 |
auto pin_uuid = row_pad[pad_list_columns.pin_uuid]; |
| 428 |
for (auto &it : pin_store->children()) { |
| 429 |
Gtk::TreeModel::Row row = *it; |
| 430 |
if (row[pin_list_columns.gate_uuid] == gate_uuid && row[pin_list_columns.pin_uuid] == pin_uuid) { |
| 431 |
w_tv_pins->get_selection()->select(it); |
| 432 |
tree_view_scroll_to_selection(w_tv_pins); |
| 433 |
break; |
| 434 |
} |
| 435 |
} |
| 436 |
}); |
| 437 |
|
| 438 |
w_button_select_pads->signal_clicked().connect([this] { |
| 439 |
auto sel = w_tv_pins->get_selection(); |
| 440 |
auto rows = sel->get_selected_rows(); |
| 441 |
if (rows.size() != 1) |
| 442 |
return; |
| 443 |
auto it_pin = pin_store->get_iter(rows.front()); |
| 444 |
Gtk::TreeModel::Row row_pin = *it_pin; |
| 445 |
auto gate_uuid = row_pin[pin_list_columns.gate_uuid]; |
| 446 |
auto pin_uuid = row_pin[pin_list_columns.pin_uuid]; |
| 447 |
|
| 448 |
w_tv_pads->get_selection()->unselect_all(); |
| 449 |
for (auto &it : pad_store->children()) { |
| 450 |
Gtk::TreeModel::Row row = *it; |
| 451 |
if (row[pad_list_columns.gate_uuid] == gate_uuid && row[pad_list_columns.pin_uuid] == pin_uuid) { |
| 452 |
w_tv_pads->get_selection()->select(it); |
| 453 |
} |
| 454 |
} |
| 455 |
tree_view_scroll_to_selection(w_tv_pads); |
| 456 |
}); |
| 457 |
w_button_copy_from_other->signal_clicked().connect(sigc::mem_fun(*this, &PartEditor::copy_from_other_part)); |
| 458 |
w_model_inherit->signal_toggled().connect([this] { |
| 459 |
if (is_loading()) |
| 460 |
return; |
| 461 |
part.inherit_model = w_model_inherit->get_active(); |
| 462 |
set_needs_save(); |
| 463 |
update_model_inherit(); |
| 464 |
}); |
| 465 |
|
| 466 |
w_model_combo->signal_changed().connect([this] { |
| 467 |
if (is_loading()) |
| 468 |
return; |
| 469 |
part.model = w_model_combo->get_active_key(); |
| 470 |
set_needs_save(); |
| 471 |
}); |
| 472 |
|
| 473 |
{ |
| 474 |
namespace rv = ranges::views; |
| 475 |
const auto maxlen = ranges::max(ranges::views::concat( |
| 476 |
ranges::views::single(0), |
| 477 |
pool_parametric.get_tables() | rv::transform([](const auto &x) { return x.second.columns; }) | rv::join |
| 478 |
| rv::transform([](const auto &x) { return x.display_name.size(); }))); |
| 479 |
Gtk::Label *la; |
| 480 |
x->get_widget("label_table", la); |
| 481 |
la->set_width_chars(maxlen); |
| 482 |
} |
| 483 |
|
| 484 |
w_parametric_table_combo->append("", "None"); |
| 485 |
for (const auto &it : pool_parametric.get_tables()) { |
| 486 |
w_parametric_table_combo->append(it.first, it.second.display_name); |
| 487 |
} |
| 488 |
w_parametric_table_combo->signal_changed().connect([this] { |
| 489 |
if (is_loading()) |
| 490 |
return; |
| 491 |
w_parametric_table_combo->grab_focus(); |
| 492 |
update_parametric_editor(); |
| 493 |
set_needs_save(); |
| 494 |
}); |
| 495 |
|
| 496 |
w_orderable_MPNs_add_button->signal_clicked().connect([this] { |
| 497 |
auto uu = UUID::random(); |
| 498 |
part.orderable_MPNs.emplace(uu, ""); |
| 499 |
auto ed = create_orderable_MPN_editor(uu); |
| 500 |
ed->focus(); |
| 501 |
set_needs_save(); |
| 502 |
update_orderable_MPNs_label(); |
| 503 |
}); |
| 504 |
|
| 505 |
{ |
| 506 |
static const std::map<Part::Flag, std::string> flag_names = { |
| 507 |
{Part::Flag::BASE_PART, "Base part"}, |
| 508 |
{Part::Flag::EXCLUDE_BOM, "Exclude from BOM"}, |
| 509 |
{Part::Flag::EXCLUDE_PNP, "Exclude from Pick&Place"}, |
| 510 |
}; |
| 511 |
int top = 0; |
| 512 |
for (const auto &[fl, name] : flag_names) { |
| 513 |
auto ed = Gtk::manage(new FlagEditor()); |
| 514 |
flag_editors.emplace(fl, ed); |
| 515 |
const auto fl2 = fl; |
| 516 |
ed->signal_changed().connect([this, ed, fl2] { |
| 517 |
if (is_loading()) |
| 518 |
return; |
| 519 |
part.flags.at(fl2) = ed->get_state(); |
| 520 |
set_needs_save(); |
| 521 |
update_flags_label(); |
| 522 |
}); |
| 523 |
grid_attach_label_and_widget(w_flags_grid, name, ed, top); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
for (auto [ov, rb] : override_prefix_radio_buttons) { |
| 529 |
auto ov2 = ov; |
| 530 |
auto &rb2 = *rb; |
| 531 |
rb2.signal_toggled().connect([this, &rb2, ov2] { |
| 532 |
if (is_loading()) |
| 533 |
return; |
| 534 |
if (rb2.get_active()) { |
| 535 |
part.override_prefix = ov2; |
| 536 |
update_prefix_entry(); |
| 537 |
set_needs_save(); |
| 538 |
} |
| 539 |
}); |
| 540 |
} |
| 541 |
|
| 542 |
override_prefix_entry_connection = w_override_prefix_entry->signal_changed().connect([this] { |
| 543 |
if (part.override_prefix == Part::OverridePrefix::YES) { |
| 544 |
if (is_loading()) |
| 545 |
return; |
| 546 |
part.prefix = w_override_prefix_entry->get_text(); |
| 547 |
set_needs_save(); |
| 548 |
} |
| 549 |
}); |
| 550 |
|
| 551 |
load(); |
| 552 |
} |
| 553 |
|
| 554 |
void PartEditor::load() |
| 555 |
{ |
| 556 |
auto loading_setter = set_loading(); |
| 557 |
for (auto &[attr, entry] : attr_editors) { |
| 558 |
entry->property_can_inherit() = part.base.get(); |
| 559 |
entry->set_text_this(part.attributes.at(attr).second); |
| 560 |
if (part.base) { |
| 561 |
entry->set_text_inherit(part.base->attributes.at(attr).second); |
| 562 |
entry->property_inherit() = part.attributes.at(attr).first; |
| 563 |
} |
| 564 |
else { |
| 565 |
entry->property_inherit() = false; |
| 566 |
} |
| 567 |
} |
| 568 |
update_entries(); |
| 569 |
|
| 570 |
w_tags_inherit->set_active(part.inherit_tags); |
| 571 |
w_tags->set_tags(part.tags); |
| 572 |
w_change_package_button->set_sensitive(!part.base); |
| 573 |
w_clear_base_menu_item->set_sensitive(part.base.get()); |
| 574 |
|
| 575 |
|
| 576 |
if (part.base) { |
| 577 |
w_model_inherit->set_active(part.inherit_model); |
| 578 |
w_model_inherit->set_sensitive(true); |
| 579 |
} |
| 580 |
else { |
| 581 |
w_model_inherit->set_active(false); |
| 582 |
w_model_inherit->set_sensitive(false); |
| 583 |
} |
| 584 |
update_model_inherit(); |
| 585 |
|
| 586 |
|
| 587 |
update_treeview(); |
| 588 |
|
| 589 |
update_map_buttons(); |
| 590 |
|
| 591 |
update_mapped(); |
| 592 |
populate_models(); |
| 593 |
w_model_combo->set_active_key(part.model); |
| 594 |
|
| 595 |
|
| 596 |
if (part.parametric.count("table")) { |
| 597 |
std::string tab = part.parametric.at("table"); |
| 598 |
parametric_data[tab] = part.parametric; |
| 599 |
if (pool_parametric.get_tables().count(tab)) { |
| 600 |
w_parametric_table_combo->set_active_id(tab); |
| 601 |
} |
| 602 |
else { |
| 603 |
w_parametric_table_combo->set_active_id(""); |
| 604 |
} |
| 605 |
} |
| 606 |
else { |
| 607 |
w_parametric_table_combo->set_active_id(""); |
| 608 |
} |
| 609 |
update_parametric_editor(); |
| 610 |
|
| 611 |
for (const auto &it : part.orderable_MPNs) { |
| 612 |
create_orderable_MPN_editor(it.first); |
| 613 |
} |
| 614 |
update_orderable_MPNs_label(); |
| 615 |
|
| 616 |
override_prefix_radio_buttons.at(part.override_prefix)->set_active(true); |
| 617 |
|
| 618 |
w_override_prefix_inherit_button->set_sensitive(part.base.get()); |
| 619 |
update_prefix_entry(); |
| 620 |
|
| 621 |
for (auto [fl, ed] : flag_editors) { |
| 622 |
ed->set_can_inherit(part.base.get()); |
| 623 |
ed->set_state(part.flags.at(fl)); |
| 624 |
} |
| 625 |
update_flags_label(); |
| 626 |
} |
| 627 |
|
| 628 |
class OrderableMPNEditor *PartEditor::create_orderable_MPN_editor(const UUID &uu) |
| 629 |
{ |
| 630 |
auto ed = Gtk::manage(new OrderableMPNEditor(part, uu)); |
| 631 |
w_orderable_MPNs_box->pack_start(*ed, false, false, 0); |
| 632 |
ed->signal_changed().connect([this, ed] { |
| 633 |
if (part.orderable_MPNs.count(ed->get_uuid())) |
| 634 |
part.orderable_MPNs.at(ed->get_uuid()) = ed->get_MPN(); |
| 635 |
set_needs_save(); |
| 636 |
update_orderable_MPNs_label(); |
| 637 |
}); |
| 638 |
ed->show(); |
| 639 |
return ed; |
| 640 |
} |
| 641 |
|
| 642 |
void PartEditor::update_map_buttons() |
| 643 |
{ |
| 644 |
if (part.base) { |
| 645 |
w_button_map->set_sensitive(false); |
| 646 |
w_button_unmap->set_sensitive(false); |
| 647 |
w_button_automap->set_sensitive(false); |
| 648 |
w_button_copy_from_other->set_sensitive(false); |
| 649 |
} |
| 650 |
else { |
| 651 |
bool can_map = |
| 652 |
w_tv_pads->get_selection()->count_selected_rows() && w_tv_pins->get_selection()->count_selected_rows(); |
| 653 |
w_button_map->set_sensitive(can_map); |
| 654 |
w_button_unmap->set_sensitive(w_tv_pads->get_selection()->count_selected_rows()); |
| 655 |
w_button_automap->set_sensitive(w_tv_pads->get_selection()->count_selected_rows()); |
| 656 |
w_button_copy_from_other->set_sensitive(true); |
| 657 |
} |
| 658 |
w_button_select_pads->set_sensitive(w_tv_pins->get_selection()->count_selected_rows()); |
| 659 |
w_button_select_pin->set_sensitive(w_tv_pads->get_selection()->count_selected_rows() == 1); |
| 660 |
} |
| 661 |
|
| 662 |
void PartEditor::update_orderable_MPNs_label() |
| 663 |
{ |
| 664 |
std::string s; |
| 665 |
for (const auto &[uu, mpn] : part.orderable_MPNs) { |
| 666 |
s += mpn + ", "; |
| 667 |
} |
| 668 |
if (s.size()) { |
| 669 |
s.pop_back(); |
| 670 |
s.pop_back(); |
| 671 |
} |
| 672 |
else { |
| 673 |
s = "(None)"; |
| 674 |
} |
| 675 |
w_orderable_MPNs_label->set_text(s); |
| 676 |
} |
| 677 |
|
| 678 |
void PartEditor::update_flags_label() |
| 679 |
{ |
| 680 |
std::string s; |
| 681 |
static const std::map<Part::Flag, std::string> flag_names = { |
| 682 |
{Part::Flag::BASE_PART, "Base part"}, |
| 683 |
{Part::Flag::EXCLUDE_BOM, "No BOM"}, |
| 684 |
{Part::Flag::EXCLUDE_PNP, "No Pick&Place"}, |
| 685 |
}; |
| 686 |
for (const auto &[fl, name] : flag_names) { |
| 687 |
if (part.get_flag(fl)) { |
| 688 |
s += name; |
| 689 |
s += ", "; |
| 690 |
} |
| 691 |
} |
| 692 |
if (s.size()) { |
| 693 |
s.pop_back(); |
| 694 |
s.pop_back(); |
| 695 |
} |
| 696 |
else { |
| 697 |
s = "(None)"; |
| 698 |
} |
| 699 |
w_flags_label->set_text(s); |
| 700 |
} |
| 701 |
|
| 702 |
void PartEditor::update_prefix_entry() |
| 703 |
{ |
| 704 |
override_prefix_entry_connection.block(); |
| 705 |
switch (part.override_prefix) { |
| 706 |
case Part::OverridePrefix::NO: |
| 707 |
w_override_prefix_entry->set_sensitive(false); |
| 708 |
w_override_prefix_entry->set_text(part.entity->prefix); |
| 709 |
break; |
| 710 |
|
| 711 |
case Part::OverridePrefix::INHERIT: |
| 712 |
w_override_prefix_entry->set_sensitive(false); |
| 713 |
w_override_prefix_entry->set_text(part.base->get_prefix()); |
| 714 |
break; |
| 715 |
|
| 716 |
case Part::OverridePrefix::YES: |
| 717 |
w_override_prefix_entry->set_sensitive(true); |
| 718 |
w_override_prefix_entry->set_text(part.prefix); |
| 719 |
break; |
| 720 |
} |
| 721 |
override_prefix_entry_connection.unblock(); |
| 722 |
} |
| 723 |
|
| 724 |
void PartEditor::map_pin(Gtk::TreeModel::iterator it_pin) |
| 725 |
{ |
| 726 |
auto pin_sel = w_tv_pins->get_selection(); |
| 727 |
if (it_pin) { |
| 728 |
Gtk::TreeModel::Row row_pin = *it_pin; |
| 729 |
auto sel = w_tv_pads->get_selection(); |
| 730 |
for (auto &path : sel->get_selected_rows()) { |
| 731 |
auto it = pad_store->get_iter(path); |
| 732 |
Gtk::TreeModel::Row row = *it; |
| 733 |
row[pad_list_columns.gate_name] = row_pin.get_value(pin_list_columns.gate_name); |
| 734 |
row[pad_list_columns.pin_name] = row_pin.get_value(pin_list_columns.pin_name); |
| 735 |
row[pad_list_columns.pin_uuid] = row_pin.get_value(pin_list_columns.pin_uuid); |
| 736 |
row[pad_list_columns.gate_uuid] = row_pin.get_value(pin_list_columns.gate_uuid); |
| 737 |
} |
| 738 |
if (++it_pin) { |
| 739 |
pin_sel->select(it_pin); |
| 740 |
} |
| 741 |
if (sel->count_selected_rows() == 1) { |
| 742 |
auto it_pad = pad_store->get_iter(sel->get_selected_rows().at(0)); |
| 743 |
sel->unselect(it_pad); |
| 744 |
if (++it_pad) { |
| 745 |
sel->select(it_pad); |
| 746 |
} |
| 747 |
} |
| 748 |
update_mapped(); |
| 749 |
update_pad_map(); |
| 750 |
set_needs_save(); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
void PartEditor::update_model_inherit() |
| 755 |
{ |
| 756 |
if (!part.base) |
| 757 |
return; |
| 758 |
auto active = w_model_inherit->get_active(); |
| 759 |
if (active) { |
| 760 |
w_model_combo->set_active_key(part.base->model); |
| 761 |
} |
| 762 |
w_model_combo->set_sensitive(!active); |
| 763 |
} |
| 764 |
|
| 765 |
static std::string append_with_slash(const std::string &s) |
| 766 |
{ |
| 767 |
if (s.size()) |
| 768 |
return " / " + s; |
| 769 |
else |
| 770 |
return ""; |
| 771 |
} |
| 772 |
|
| 773 |
void PartEditor::update_entries() |
| 774 |
{ |
| 775 |
if (part.base) { |
| 776 |
w_base_label->set_markup( |
| 777 |
"<a href=\"" + (std::string)part.base->uuid + "\">" |
| 778 |
+ Glib::Markup::escape_text(part.base->get_MPN() + append_with_slash(part.base->get_manufacturer())) |
| 779 |
+ "</a>"); |
| 780 |
w_entity_label->set_markup("<a href=\"" + (std::string)part.base->entity->uuid + "\">" |
| 781 |
+ Glib::Markup::escape_text(part.base->entity->name |
| 782 |
+ append_with_slash(part.base->entity->manufacturer)) |
| 783 |
+ "</a>"); |
| 784 |
w_package_label->set_markup("<a href=\"" + (std::string)part.base->package->uuid + "\">" |
| 785 |
+ Glib::Markup::escape_text(part.base->package->name |
| 786 |
+ append_with_slash(part.base->package->manufacturer)) |
| 787 |
+ "</a>"); |
| 788 |
} |
| 789 |
else { |
| 790 |
w_base_label->set_text("none"); |
| 791 |
w_entity_label->set_markup( |
| 792 |
"<a href=\"" + (std::string)part.entity->uuid + "\">" |
| 793 |
+ Glib::Markup::escape_text(part.entity->name + append_with_slash(part.entity->manufacturer)) + "</a>"); |
| 794 |
w_package_label->set_markup( |
| 795 |
"<a href=\"" + (std::string)part.package->uuid + "\">" |
| 796 |
+ Glib::Markup::escape_text(part.package->name + append_with_slash(part.package->manufacturer)) |
| 797 |
+ "</a>"); |
| 798 |
} |
| 799 |
|
| 800 |
if (part.base) { |
| 801 |
std::stringstream s; |
| 802 |
auto tags_from_base = part.base->get_tags(); |
| 803 |
std::copy(tags_from_base.begin(), tags_from_base.end(), std::ostream_iterator<std::string>(s, " ")); |
| 804 |
w_tags_inherited->set_text(s.str()); |
| 805 |
} |
| 806 |
else { |
| 807 |
w_tags_inherited->set_text(""); |
| 808 |
} |
| 809 |
|
| 810 |
w_tags_inherit->set_sensitive(part.base.get()); |
| 811 |
} |
| 812 |
|
| 813 |
void PartEditor::set_package(std::shared_ptr<const Package> package) |
| 814 |
{ |
| 815 |
const auto old_package = part.package; |
| 816 |
part.package = package; |
| 817 |
part.model = part.package->default_model; |
| 818 |
const auto old_pad_map = part.pad_map; |
| 819 |
part.pad_map.clear(); |
| 820 |
for (const auto &[uu, pad] : part.package->pads) { |
| 821 |
const auto &pad_name = pad.name; |
| 822 |
if (auto it = find_if_ptr(old_pad_map, [&pad_name, &old_package](const auto &x) { |
| 823 |
return old_package->pads.at(x.first).name == pad_name; |
| 824 |
})) { |
| 825 |
part.pad_map.emplace(uu, it->second); |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
void PartEditor::change_package() |
| 831 |
{ |
| 832 |
auto top = dynamic_cast<Gtk::Window *>(get_ancestor(GTK_TYPE_WINDOW)); |
| 833 |
PoolBrowserDialog dia(top, ObjectType::PACKAGE, pool); |
| 834 |
if (dia.run() == Gtk::RESPONSE_OK) { |
| 835 |
set_package(pool.get_package(dia.get_browser().get_selected())); |
| 836 |
load(); |
| 837 |
set_needs_save(); |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
bool PartEditor::check_base(const UUID &new_base) |
| 842 |
{ |
| 843 |
bool r = true; |
| 844 |
auto &db = pool.get_db(); |
| 845 |
db.execute("DROP VIEW IF EXISTS part_deps"); |
| 846 |
{ |
| 847 |
db.execute( |
| 848 |
"CREATE TEMPORARY view part_deps AS SELECT uuid, dep_uuid FROM dependencies WHERE " |
| 849 |
"type = 'part' AND dep_type = 'part' AND uuid != '" + (std::string)part.uuid + "' " |
| 850 |
"UNION SELECT '" + (std::string)part.uuid + "', '" + (std::string) new_base+ "'"); |
| 851 |
} |
| 852 |
{ |
| 853 |
SQLite::Query q(db, |
| 854 |
"WITH FindRoot AS ( " |
| 855 |
"SELECT uuid, dep_uuid, uuid as path, 0 AS Distance " |
| 856 |
"FROM part_deps WHERE uuid = $part " |
| 857 |
"UNION ALL " |
| 858 |
"SELECT C.uuid, P.dep_uuid, C.path || ' > ' || P.uuid, C.Distance + 1 " |
| 859 |
"FROM part_deps AS P " |
| 860 |
" JOIN FindRoot AS C " |
| 861 |
" ON C.dep_uuid = P.uuid AND P.dep_uuid != P.uuid AND C.dep_uuid != C.uuid " |
| 862 |
") " |
| 863 |
"SELECT * " |
| 864 |
"FROM FindRoot AS R " |
| 865 |
"WHERE R.uuid = R.dep_uuid AND R.Distance > 0"); |
| 866 |
q.bind("$part", part.uuid); |
| 867 |
while (q.step()) { |
| 868 |
r = false; |
| 869 |
break; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
db.execute("DROP VIEW IF EXISTS part_deps"); |
| 874 |
return r; |
| 875 |
} |
| 876 |
|
| 877 |
void PartEditor::change_base() |
| 878 |
{ |
| 879 |
auto top = dynamic_cast<Gtk::Window *>(get_ancestor(GTK_TYPE_WINDOW)); |
| 880 |
PoolBrowserDialog dia(top, ObjectType::PART, pool); |
| 881 |
{ |
| 882 |
auto &br = dynamic_cast<PoolBrowserPart &>(dia.get_browser()); |
| 883 |
br.set_entity_uuid(part.entity->uuid); |
| 884 |
br.search(); |
| 885 |
} |
| 886 |
while (dia.run() == Gtk::RESPONSE_OK) { |
| 887 |
const auto selected_part_uuid = dia.get_browser().get_selected(); |
| 888 |
if (part.base && part.base->uuid == selected_part_uuid) |
| 889 |
return; |
| 890 |
if (selected_part_uuid == part.uuid || !check_base(selected_part_uuid)) { |
| 891 |
Gtk::MessageDialog md(dia, "Can't set as base part", false /* use_markup */, Gtk::MESSAGE_ERROR, |
| 892 |
Gtk::BUTTONS_OK); |
| 893 |
md.set_secondary_text("Cyclic dependency"); |
| 894 |
md.run(); |
| 895 |
continue; |
| 896 |
} |
| 897 |
auto new_base = pool.get_part(selected_part_uuid); |
| 898 |
set_base(new_base); |
| 899 |
load(); |
| 900 |
set_needs_save(); |
| 901 |
return; |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
void PartEditor::set_base(std::shared_ptr<const Part> new_base) |
| 906 |
{ |
| 907 |
auto old_base = part.base; |
| 908 |
part.base = new_base; |
| 909 |
if (new_base) { |
| 910 |
set_package(part.base->package); |
| 911 |
part.pad_map = part.base->pad_map; |
| 912 |
} |
| 913 |
else { |
| 914 |
for (auto &[attr, v] : part.attributes) { |
| 915 |
v.first = false; |
| 916 |
} |
| 917 |
for (auto &[flag, st] : part.flags) { |
| 918 |
if (st == Part::FlagState::INHERIT && old_base) |
| 919 |
st = old_base->get_flag(flag) ? Part::FlagState::SET : Part::FlagState::CLEAR; |
| 920 |
} |
| 921 |
if (part.inherit_tags) { |
| 922 |
auto inherited_tags = old_base->get_tags(); |
| 923 |
part.tags.insert(inherited_tags.begin(), inherited_tags.end()); |
| 924 |
} |
| 925 |
if (part.override_prefix == Part::OverridePrefix::INHERIT) |
| 926 |
part.override_prefix = old_base->get_override_prefix(); |
| 927 |
if (part.override_prefix == Part::OverridePrefix::YES) |
| 928 |
part.prefix = old_base->get_prefix(); |
| 929 |
part.inherit_model = false; |
| 930 |
part.inherit_tags = false; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
void PartEditor::clear_base() |
| 935 |
{ |
| 936 |
set_base(nullptr); |
| 937 |
load(); |
| 938 |
set_needs_save(); |
| 939 |
} |
| 940 |
|
| 941 |
void PartEditor::create_base() |
| 942 |
{ |
| 943 |
auto top = dynamic_cast<Gtk::Window *>(get_ancestor(GTK_TYPE_WINDOW)); |
| 944 |
|
| 945 |
GtkFileChooserNative *native = |
| 946 |
gtk_file_chooser_native_new("Save", top->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, "_Save", "_Cancel"); |
| 947 |
auto chooser = Glib::wrap(GTK_FILE_CHOOSER(native)); |
| 948 |
chooser->set_do_overwrite_confirmation(true); |
| 949 |
|
| 950 |
|
| 951 |
chooser->set_current_folder(Glib::path_get_dirname(filename)); |
| 952 |
|
| 953 |
run_native_filechooser_with_retry(chooser, "Error saving Part", [this, chooser] { |
| 954 |
std::string fn = append_dot_json(chooser->get_filename()); |
| 955 |
pool.check_filename_throw(ObjectType::PART, fn); |
| 956 |
Part new_part = part; |
| 957 |
new_part.flags.at(Part::Flag::BASE_PART) = Part::FlagState::SET; |
| 958 |
auto &mpn = new_part.attributes.at(Part::Attribute::MPN); |
| 959 |
if (!mpn.first) |
| 960 |
mpn.second = mpn.second + " (Base part)"; |
| 961 |
new_part.uuid = UUID::random(); |
| 962 |
save_json_to_file(fn, new_part.serialize()); |
| 963 |
s_signal_extra_file_saved.emit(fn); |
| 964 |
pending_base_part = new_part.uuid; |
| 965 |
}); |
| 966 |
} |
| 967 |
|
| 968 |
static void part_update_refs_or_0(Part &part, IPool &pool) |
| 969 |
{ |
| 970 |
part.entity = pool.get_entity(part.entity->uuid); |
| 971 |
part.package = pool.get_package(part.package->uuid); |
| 972 |
if (part.base) |
| 973 |
part.base = pool.get_part(part.base->uuid); |
| 974 |
for (auto &[pad, it] : part.pad_map) { |
| 975 |
it.gate.update(part.entity->gates); |
| 976 |
if (it.gate) |
| 977 |
it.pin.update(it.gate->unit->pins); |
| 978 |
else |
| 979 |
it.pin.ptr = nullptr; |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
void PartEditor::reload() |
| 984 |
{ |
| 985 |
part_update_refs_or_0(part, pool); |
| 986 |
update_entries(); |
| 987 |
update_treeview(); |
| 988 |
update_mapped(); |
| 989 |
if (pending_base_part) { |
| 990 |
std::shared_ptr<const Part> new_base; |
| 991 |
try { |
| 992 |
new_base = pool.get_part(pending_base_part); |
| 993 |
} |
| 994 |
catch (...) { |
| 995 |
// doesn't seem to exist yet... |
| 996 |
} |
| 997 |
if (new_base) { |
| 998 |
set_base(new_base); |
| 999 |
s_signal_open_item.emit(ObjectType::PART, new_base->uuid); |
| 1000 |
for (auto &[attr, value] : part.attributes) { |
| 1001 |
value.first = true; |
| 1002 |
} |
| 1003 |
part.tags.clear(); |
| 1004 |
part.inherit_tags = true; |
| 1005 |
part.inherit_model = true; |
| 1006 |
if (new_base->override_prefix != Part::OverridePrefix::NO) |
| 1007 |
part.override_prefix = Part::OverridePrefix::INHERIT; |
| 1008 |
pending_base_part = UUID(); |
| 1009 |
load(); |
| 1010 |
set_needs_save(); |
| 1011 |
} |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
void PartEditor::update_treeview() |
| 1016 |
{ |
| 1017 |
pin_store->freeze_notify(); |
| 1018 |
pad_store->freeze_notify(); |
| 1019 |
|
| 1020 |
pin_store->clear(); |
| 1021 |
pad_store->clear(); |
| 1022 |
|
| 1023 |
for (const auto &it_gate : part.entity->gates) { |
| 1024 |
for (const auto &it_pin : it_gate.second.unit->pins) { |
| 1025 |
Gtk::TreeModel::Row row = *(pin_store->append()); |
| 1026 |
row[pin_list_columns.gate_uuid] = it_gate.first; |
| 1027 |
row[pin_list_columns.gate_name] = it_gate.second.name; |
| 1028 |
row[pin_list_columns.pin_uuid] = it_pin.first; |
| 1029 |
row[pin_list_columns.pin_name] = it_pin.second.primary_name; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
for (const auto &it : part.package->pads) { |
| 1034 |
if (it.second.pool_padstack->type != Padstack::Type::MECHANICAL) { |
| 1035 |
Gtk::TreeModel::Row row = *(pad_store->append()); |
| 1036 |
row[pad_list_columns.pad_uuid] = it.first; |
| 1037 |
row[pad_list_columns.pad_name] = it.second.name; |
| 1038 |
if (part.pad_map.count(it.first)) { |
| 1039 |
const auto &x = part.pad_map.at(it.first); |
| 1040 |
if (x.gate && x.pin) { |
| 1041 |
row[pad_list_columns.gate_uuid] = x.gate->uuid; |
| 1042 |
row[pad_list_columns.gate_name] = x.gate->name; |
| 1043 |
row[pad_list_columns.pin_uuid] = x.pin->uuid; |
| 1044 |
row[pad_list_columns.pin_name] = x.pin->primary_name; |
| 1045 |
} |
| 1046 |
} |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
pad_store->thaw_notify(); |
| 1051 |
pin_store->thaw_notify(); |
| 1052 |
} |
| 1053 |
|
| 1054 |
void PartEditor::update_mapped() |
| 1055 |
{ |
| 1056 |
std::set<std::pair<UUID, UUID>> pins_mapped; |
| 1057 |
int n_pads_not_mapped = 0; |
| 1058 |
for (const auto &it : pad_store->children()) { |
| 1059 |
if (it[pad_list_columns.gate_uuid] != UUID()) { |
| 1060 |
pins_mapped.emplace(it[pad_list_columns.gate_uuid], it[pad_list_columns.pin_uuid]); |
| 1061 |
} |
| 1062 |
else { |
| 1063 |
n_pads_not_mapped++; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
for (auto &it : pin_store->children()) { |
| 1067 |
if (pins_mapped.count({it[pin_list_columns.gate_uuid], it[pin_list_columns.pin_uuid]})) { |
| 1068 |
it[pin_list_columns.mapped] = true; |
| 1069 |
} |
| 1070 |
else { |
| 1071 |
it[pin_list_columns.mapped] = false; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
w_pin_stat->set_text(std::to_string(pin_store->children().size() - pins_mapped.size()) + " pins not mapped"); |
| 1075 |
w_pad_stat->set_text(std::to_string(n_pads_not_mapped) + " pads not mapped"); |
| 1076 |
} |
| 1077 |
|
| 1078 |
void PartEditor::update_pad_map() |
| 1079 |
{ |
| 1080 |
part.pad_map.clear(); |
| 1081 |
for (const auto &it : pad_store->children()) { |
| 1082 |
if (it[pad_list_columns.gate_uuid] != UUID() && part.package->pads.count(it[pad_list_columns.pad_uuid])) { |
| 1083 |
const horizon::Gate *gate = &part.entity->gates.at(it[pad_list_columns.gate_uuid]); |
| 1084 |
const horizon::Pin *pin = &gate->unit->pins.at(it[pad_list_columns.pin_uuid]); |
| 1085 |
part.pad_map.emplace(it[pad_list_columns.pad_uuid], Part::PadMapItem(gate, pin)); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
void PartEditor::populate_models() |
| 1091 |
{ |
| 1092 |
w_model_combo->remove_all(); |
| 1093 |
for (const auto &it : part.package->models) { |
| 1094 |
w_model_combo->append((std::string)it.first, Glib::path_get_basename(it.second.filename)); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
void PartEditor::update_parametric_editor() |
| 1099 |
{ |
| 1100 |
if (parametric_editor) { |
| 1101 |
delete parametric_editor; |
| 1102 |
parametric_editor = nullptr; |
| 1103 |
} |
| 1104 |
auto tab = w_parametric_table_combo->get_active_id(); |
| 1105 |
if (pool_parametric.get_tables().count(tab)) { |
| 1106 |
auto ed = Gtk::manage(new ParametricEditor(pool_parametric, tab, sg_parametric_label)); |
| 1107 |
ed->show(); |
| 1108 |
w_parametric_box->pack_start(*ed, true, true, 0); |
| 1109 |
if (parametric_data.count(tab)) { |
| 1110 |
const auto &values = parametric_data.at(tab); |
| 1111 |
ed->update(values); |
| 1112 |
part.parametric = values; |
| 1113 |
} |
| 1114 |
else { |
| 1115 |
part.parametric.clear(); |
| 1116 |
part.parametric.emplace("table", tab); |
| 1117 |
} |
| 1118 |
parametric_editor = ed; |
| 1119 |
parametric_editor->signal_changed().connect([this] { |
| 1120 |
auto values = parametric_editor->get_values(); |
| 1121 |
parametric_data[values.at("table")] = values; |
| 1122 |
part.parametric = values; |
| 1123 |
set_needs_save(); |
| 1124 |
}); |
| 1125 |
} |
| 1126 |
else { |
| 1127 |
part.parametric.clear(); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
void PartEditor::copy_from_other_part() |
| 1132 |
{ |
| 1133 |
auto top = dynamic_cast<Gtk::Window *>(get_ancestor(GTK_TYPE_WINDOW)); |
| 1134 |
PoolBrowserDialog dia(top, ObjectType::PART, pool); |
| 1135 |
auto &br = dynamic_cast<PoolBrowserPart &>(dia.get_browser()); |
| 1136 |
br.set_entity_uuid(part.entity->uuid); |
| 1137 |
br.search(); |
| 1138 |
if (dia.run() == Gtk::RESPONSE_OK) { |
| 1139 |
auto uu = br.get_selected(); |
| 1140 |
auto other_part = pool.get_part(uu); |
| 1141 |
for (auto &it_pad : pad_store->children()) { |
| 1142 |
Gtk::TreeModel::Row row_pad = *it_pad; |
| 1143 |
if (row_pad.get_value(pad_list_columns.gate_uuid) == UUID()) { // only update unmapped pads |
| 1144 |
std::string pad_name = row_pad.get_value(pad_list_columns.pad_name); |
| 1145 |
// find other part mapping |
| 1146 |
for (const auto &it_map_other : other_part->pad_map) { |
| 1147 |
auto pad_uu_other = it_map_other.first; |
| 1148 |
const auto &pad_name_other = other_part->package->pads.at(pad_uu_other).name; |
| 1149 |
if (pad_name_other == pad_name) { // found it |
| 1150 |
row_pad[pad_list_columns.gate_name] = it_map_other.second.gate->name; |
| 1151 |
row_pad[pad_list_columns.pin_name] = it_map_other.second.pin->primary_name; |
| 1152 |
row_pad[pad_list_columns.pin_uuid] = it_map_other.second.pin->uuid; |
| 1153 |
row_pad[pad_list_columns.gate_uuid] = it_map_other.second.gate->uuid; |
| 1154 |
break; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} |
| 1158 |
} |
| 1159 |
update_mapped(); |
| 1160 |
set_needs_save(); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
void PartEditor::save_as(const std::string &fn) |
| 1165 |
{ |
| 1166 |
unset_needs_save(); |
| 1167 |
filename = fn; |
| 1168 |
save_json_to_file(fn, part.serialize()); |
| 1169 |
} |
| 1170 |
|
| 1171 |
std::string PartEditor::get_name() const |
| 1172 |
{ |
| 1173 |
return part.get_MPN(); |
| 1174 |
} |
| 1175 |
|
| 1176 |
const UUID &PartEditor::get_uuid() const |
| 1177 |
{ |
| 1178 |
return part.uuid; |
| 1179 |
} |
| 1180 |
|
| 1181 |
RulesCheckResult PartEditor::run_checks() const |
| 1182 |
{ |
| 1183 |
return check_part(part); |
| 1184 |
} |
| 1185 |
|
| 1186 |
const FileVersion &PartEditor::get_version() const |
| 1187 |
{ |
| 1188 |
return part.version; |
| 1189 |
} |
| 1190 |
|
| 1191 |
unsigned int PartEditor::get_required_version() const |
| 1192 |
{ |
| 1193 |
return part.get_required_version(); |
| 1194 |
} |
| 1195 |
|
| 1196 |
ObjectType PartEditor::get_type() const |
| 1197 |
{ |
| 1198 |
return ObjectType::PART; |
| 1199 |
} |
| 1200 |
|
| 1201 |
class HistoryItemPart : public HistoryManager::HistoryItem { |
| 1202 |
public: |
| 1203 |
HistoryItemPart(const Part &p, const std::string &cm) : HistoryManager::HistoryItem(cm), part(p) |
| 1204 |
{ |
| 1205 |
} |
| 1206 |
Part part; |
| 1207 |
}; |
| 1208 |
|
| 1209 |
std::unique_ptr<HistoryManager::HistoryItem> PartEditor::make_history_item(const std::string &comment) |
| 1210 |
{ |
| 1211 |
return std::make_unique<HistoryItemPart>(part, comment); |
| 1212 |
} |
| 1213 |
|
| 1214 |
void PartEditor::history_load(const HistoryManager::HistoryItem &it) |
| 1215 |
{ |
| 1216 |
const auto &x = dynamic_cast<const HistoryItemPart &>(it); |
| 1217 |
part = x.part; |
| 1218 |
part_update_refs_or_0(part, pool); |
| 1219 |
load(); |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
PartEditor *PartEditor::create(const std::string &fn, IPool &po, PoolParametric &pp) |
| 1224 |
{ |
| 1225 |
PartEditor *w; |
| 1226 |
Glib::RefPtr<Gtk::Builder> x = Gtk::Builder::create(); |
| 1227 |
x->add_from_resource("/org/horizon-eda/horizon/pool-prj-mgr/pool-mgr/editors/part_editor.ui"); |
| 1228 |
x->get_widget_derived("part_editor", w, fn, po, pp); |
| 1229 |
w->reference(); |
| 1230 |
return w; |
| 1231 |
} |
| 1232 |
} // namespace horizon |