Debian Codegraph @

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

raw
1 #include "parametric.hpp"
2 #include <cmath>
3 #include <iomanip>
4 #include "util/gtk_util.hpp"
5 #include "util/util.hpp"
6
7 namespace horizon {
8
9
10 class ParametricParamEditor {
11 public:
12 ParametricParamEditor(const PoolParametric::Column &c) : column(c)
13 {
14 }
15 virtual std::string get_value() = 0;
16 virtual void set_value(const std::string &value) = 0;
17 virtual ~ParametricParamEditor()
18 {
19 }
20
21 protected:
22 const PoolParametric::Column &column;
23 };
24
25 class ParamQuantityEditor : public Gtk::Entry, public ParametricParamEditor {
26 public:
27 ParamQuantityEditor(const PoolParametric::Column &c) : Gtk::Entry(), ParametricParamEditor(c)
28 {
29 entry_set_tnum(*this);
30
31 signal_activate().connect(sigc::mem_fun(*this, &ParamQuantityEditor::parse));
32 signal_focus_out_event().connect([this](GdkEventFocus *ev) {
33 parse();
34 return false;
35 });
36 }
37 std::string get_value() override
38 {
39 std::ostringstream ss;
40 ss << value;
41 return ss.str();
42 }
43 void set_value(const std::string &v) override
44 {
45 std::istringstream istr(v);
46 istr.imbue(std::locale("C"));
47 istr >> value;
48 set_text(format_value(value));
49 }
50
51 private:
52 double value = 0;
53
54 void parse()
55 {
56 auto v = parse_si(get_text());
57 if (!std::isnan(v)) {
58 value = v;
59 }
60 set_text(format_value(value));
61 }
62
63 std::string format_value(double v)
64 {
65 return column.format(v);
66 }
67 };
68
69
70 class ParamEnumEditor : public Gtk::ComboBoxText, public ParametricParamEditor {
71 public:
72 ParamEnumEditor(const PoolParametric::Column &c) : Gtk::ComboBoxText(), ParametricParamEditor(c)
73 {
74 for (const auto &it : column.enum_items)
75 append(it);
76 }
77 std::string get_value() override
78 {
79 return get_active_text();
80 }
81 void set_value(const std::string &v) override
82 {
83 set_active_text(v);
84 }
85 };
86
87 class NullableParamEditor : public Gtk::Box, public ParametricParamEditor, public Changeable {
88 public:
89 NullableParamEditor(Gtk::Widget *w, ParametricParamEditor *e, const PoolParametric::Column &c)
90 : Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0), ParametricParamEditor(c), widget(w), editor(e)
91 {
92 get_style_context()->add_class("linked");
93 pack_start(*widget, true, true, 0);
94 widget->show();
95
96 nullbutton = Gtk::manage(new Gtk::ToggleButton("N/A"));
97 pack_start(*nullbutton, false, false, 0);
98 nullbutton->show();
99 nullbutton->set_sensitive(!column.required);
100
101 if (!column.required)
102 nullbutton->signal_toggled().connect([this] {
103 widget->set_sensitive(!nullbutton->get_active());
104 s_signal_changed.emit();
105 });
106 }
107
108 std::string get_value() override
109 {
110 if (!column.required && nullbutton->get_active())
111 return "";
112 else
113 return editor->get_value();
114 }
115 void set_value(const std::string &value) override
116 {
117 if (value.size() || column.required)
118 editor->set_value(value);
119
120 nullbutton->set_active(!column.required && (value.size() == 0));
121 }
122
123 private:
124 Gtk::Widget *widget;
125 Gtk::ToggleButton *nullbutton = nullptr;
126 ParametricParamEditor *editor;
127 };
128
129
130 ParametricEditor::ParametricEditor(PoolParametric &p, const std::string &t, Glib::RefPtr<Gtk::SizeGroup> sg)
131 : Gtk::Grid(), pool(p), table(pool.get_tables().at(t))
132 {
133 set_row_spacing(10);
134 set_column_spacing(10);
135 int top = 0;
136 for (auto &col : table.columns) {
137 Gtk::Widget *w = nullptr;
138 ParametricParamEditor *e = nullptr;
139 switch (col.type) {
140 case PoolParametric::Column::Type::QUANTITY: {
141 auto x = Gtk::manage(new ParamQuantityEditor(col));
142 x->signal_changed().connect([this] { s_signal_changed.emit(); });
143 w = x;
144 e = x;
145 } break;
146 case PoolParametric::Column::Type::ENUM: {
147 auto x = Gtk::manage(new ParamEnumEditor(col));
148 x->signal_changed().connect([this] { s_signal_changed.emit(); });
149 w = x;
150 e = x;
151 } break;
152 case PoolParametric::Column::Type::STRING:;
153 }
154 if (w && e) {
155 auto ne = Gtk::manage(new NullableParamEditor(w, e, col));
156 ne->show();
157 editors[col.name] = ne;
158 ne->signal_changed().connect([this] { s_signal_changed.emit(); });
159 auto la = grid_attach_label_and_widget(this, col.display_name, ne, top);
160 sg->add_widget(*la);
161 }
162 }
163 }
164
165 void ParametricEditor::update(const std::map<std::string, std::string> &params)
166 {
167 s_signal_changed.block();
168 for (auto &it : params) {
169 if (editors.count(it.first))
170 editors.at(it.first)->set_value(it.second);
171 }
172 s_signal_changed.unblock();
173 }
174
175 std::map<std::string, std::string> ParametricEditor::get_values()
176 {
177 std::map<std::string, std::string> r;
178 r.emplace("table", table.name);
179 for (auto &it : editors) {
180 r.emplace(it.first, it.second->get_value());
181 }
182 return r;
183 }
184
185 }; // namespace horizon