1 #include "pad_parameter_set_window.hpp"
2 #include "widgets/parameter_set_editor.hpp"
3 #include "widgets/pool_browser_button.hpp"
4 #include "package/pad.hpp"
5 #include "widgets/pool_browser_padstack.hpp"
6 #include "pool/package.hpp"
7 #include "util/util.hpp"
8 #include "pool/ipool.hpp"
9 #include <iostream>
10 #include <deque>
11 #include <algorithm>
12
13 namespace horizon {
14
PadParameterSetWindow(Gtk::Window * parent,class ImpInterface * intf,std::set<class Pad * > & apads,IPool & p,class Package & apkg)15 PadParameterSetWindow::PadParameterSetWindow(Gtk::Window *parent, class ImpInterface *intf,
16 std::set<class Pad *> &apads, IPool &p, class Package &apkg)
17 : ToolWindow(parent, intf), pool(p), pkg(apkg), pads(apads)
18 {
19 set_default_size(300, 600);
20 set_title("Edit pad");
21
22 box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0));
23 combo = Gtk::manage(new Gtk::ComboBoxText());
24 combo->set_margin_start(8);
25 combo->set_margin_end(8);
26 combo->set_margin_top(8);
27 combo->set_margin_bottom(8);
28 box->pack_start(*combo, false, false, 0);
29
30 box2 = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 8));
31 box2->set_margin_start(8);
32 box2->set_margin_end(8);
33 {
34 auto la = Gtk::manage(new Gtk::Label("Padstack"));
35 la->get_style_context()->add_class("dim-label");
36 box2->pack_start(*la, false, false, 0);
37 }
38
39 auto padstack_apply_all_button = Gtk::manage(new Gtk::Button);
40 padstack_apply_all_button->set_image_from_icon_name("object-select-symbolic", Gtk::ICON_SIZE_BUTTON);
41 padstack_apply_all_button->set_tooltip_text("Apply to all pads");
42 padstack_apply_all_button->signal_clicked().connect([this] {
43 auto ps = pool.get_padstack(padstack_button->property_selected_uuid());
44 for (auto &it : pads) {
45 it->pool_padstack = ps;
46 it->padstack = *ps;
47 it->padstack.apply_parameter_set(it->parameter_set);
48 }
49 emit_event(ToolDataWindow::Event::UPDATE);
50 });
51 box2->pack_end(*padstack_apply_all_button, false, false, 0);
52
53 box->pack_start(*box2, false, false, 0);
54
55 std::vector<Pad *> pads_sorted(pads.begin(), pads.end());
56 std::sort(pads_sorted.begin(), pads_sorted.end(),
57 [](const auto a, const auto b) { return strcmp_natural(a->name, b->name) < 0; });
58
59 for (auto it : pads_sorted) {
60 combo->append(static_cast<std::string>(it->uuid), it->name);
61 }
62
63 combo->signal_changed().connect(sigc::mem_fun(*this, &PadParameterSetWindow::load_pad));
64
65 combo->set_active(0);
66 editor->focus_first();
67
68 add(*box);
69
70 show_all();
71 }
72
load_pad()73 void PadParameterSetWindow::load_pad()
74 {
75 UUID uu(combo->get_active_id());
76 pad_current = &pkg.pads.at(uu);
77 if (editor)
78 delete editor;
79 editor = Gtk::manage(new ParameterSetEditor(&pad_current->parameter_set, false));
80 editor->set_has_apply_all_toggle("Apply to all selected pads (Shift+Enter)");
81 editor->populate();
82 editor->set_apply_all(params_apply_all);
83 editor->signal_apply_all_toggled().connect([this](ParameterID id, bool enable) {
84 if (enable) {
85 params_apply_all.insert(id);
86 apply_all(id);
87 for (auto pad : pads) {
88 pad->padstack.apply_parameter_set(pad->parameter_set);
89 }
90 emit_event(ToolDataWindow::Event::UPDATE);
91 }
92 else {
93 params_apply_all.erase(id);
94 }
95 });
96 editor->signal_activate_last().connect([this] { emit_event(ToolDataWindow::Event::OK); });
97 editor->signal_changed().connect([this] {
98 for (auto id : params_apply_all) {
99 apply_all(id);
100 }
101 for (auto pad : pads) {
102 pad->padstack.apply_parameter_set(pad->parameter_set);
103 }
104 emit_event(ToolDataWindow::Event::UPDATE);
105 });
106 box->pack_start(*editor, true, true, 0);
107 editor->show();
108
109 if (padstack_button)
110 delete padstack_button;
111
112 padstack_button = Gtk::manage(new PoolBrowserButton(ObjectType::PADSTACK, pool));
113 auto &br = dynamic_cast<PoolBrowserPadstack &>(padstack_button->get_browser());
114 br.set_package_uuid(pkg.uuid);
115 padstack_button->property_selected_uuid() = pad_current->pool_padstack->uuid;
116 padstack_button->property_selected_uuid().signal_changed().connect([this] {
117 auto ps = pool.get_padstack(padstack_button->property_selected_uuid());
118 pad_current->pool_padstack = ps;
119 pad_current->padstack = *ps;
120 pad_current->padstack.apply_parameter_set(pad_current->parameter_set);
121 emit_event(ToolDataWindow::Event::UPDATE);
122 });
123 box2->pack_start(*padstack_button, true, true, 0);
124 padstack_button->show();
125 }
126
apply_all(ParameterID id)127 void PadParameterSetWindow::apply_all(ParameterID id)
128 {
129 for (auto it : pads) {
130 it->parameter_set[id] = pad_current->parameter_set.at(id);
131 }
132 }
133
go_to_pad(const UUID & uu)134 bool PadParameterSetWindow::go_to_pad(const UUID &uu)
135 {
136 const auto pad = &pkg.pads.at(uu);
137 if (!pads.count(pad))
138 return false;
139
140 combo->set_active_id((std::string)pad->uuid);
141 return true;
142 }
143
144 } // namespace horizon
145