Debian Codegraph @

fontforgeexe/gtk/find_problems.cpp

raw symbols SCIP
1 /* Copyright (C) 2025 by Maxim Iorsh <iorsh@[email protected]>
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "find_problems.hpp"
29
30 #include "application.hpp"
31 #include "utils.hpp"
32 #include "intl.h"
33
34 namespace ff::dlg {
35
36 FindProblemsDlg::FindProblemsDlg(GWindow parent,
37 const std::vector<ProblemTab>& pr_tabs,
38 double near)
39 : DialogBase(parent) {
40 set_title(_("Find Problems"));
41 set_resizable(false);
42 set_help_context("ui/dialogs/problems.html");
43
44 auto tabs = build_notebook(pr_tabs);
45 tabs->set_scrollable();
46 get_content_area()->pack_start(*tabs);
47
48 auto button_box = Gtk::make_managed<Gtk::HBox>();
49 auto clear_all_button = Gtk::make_managed<Gtk::Button>(_("Clear All"));
50 auto set_all_button = Gtk::make_managed<Gtk::Button>(_("Set All"));
51
52 clear_all_button->signal_clicked().connect(sigc::bind(
53 sigc::mem_fun(*this, &FindProblemsDlg::set_all_checkboxes), false));
54 set_all_button->signal_clicked().connect(sigc::bind(
55 sigc::mem_fun(*this, &FindProblemsDlg::set_all_checkboxes), true));
56
57 button_box->pack_start(*clear_all_button, Gtk::PACK_SHRINK);
58 button_box->pack_start(*set_all_button, Gtk::PACK_SHRINK);
59 get_content_area()->pack_start(*button_box);
60
61 get_content_area()->pack_start(*Gtk::make_managed<Gtk::HSeparator>(),
62 Gtk::PACK_SHRINK, 5);
63
64 auto near_value_box = Gtk::make_managed<Gtk::HBox>();
65 near_value_entry_.set_width_chars(6);
66 near_value_entry_.set_value(near);
67 near_value_box->pack_start(
68 *Gtk::make_managed<Gtk::Label>(U_("ยน \"Near\" means within")),
69 Gtk::PACK_SHRINK);
70 near_value_box->pack_start(near_value_entry_, Gtk::PACK_SHRINK);
71 near_value_box->pack_start(*Gtk::make_managed<Gtk::Label>(_("em-units")),
72 Gtk::PACK_SHRINK);
73 get_content_area()->pack_start(*near_value_box);
74
75 show_all();
76 }
77
78 Gtk::HBox* FindProblemsDlg::build_record_box(
79 const ProblemRecord& record, Glib::RefPtr<Gtk::SizeGroup> size_group) {
80 auto record_box = Gtk::make_managed<Gtk::HBox>();
81 bool disabled = record.disabled;
82
83 Gtk::CheckButton record_check(record.label, true);
84 record_check.set_tooltip_text(record.tooltip);
85 record_check.set_active(record.active);
86 if (size_group) {
87 size_group->add_widget(record_check);
88 }
89
90 // This notation of parent is unrelated to parent/child widgets. It
91 // denotes problem records which logically depend on other records,
92 // and cannot be selected without their parent.
93 if (record.parent_cid != 0) {
94 Gtk::CheckButton& parent_check = widget_map_[record.parent_cid].first;
95 record_check.set_margin_start(2 * ff::ui_utils::ui_font_em_size());
96 disabled |= !parent_check.get_active();
97 parent_check.signal_toggled().connect([&parent_check, record_box]() {
98 record_box->set_sensitive(parent_check.get_active());
99 });
100 }
101
102 record_box->pack_start(record_check, Gtk::PACK_SHRINK);
103 record_box->set_sensitive(!disabled);
104
105 widgets::NumericalEntry* record_entry = nullptr;
106 if (!std::holds_alternative<std::monostate>(record.value)) {
107 if (std::holds_alternative<int>(record.value)) {
108 auto int_entry = Gtk::make_managed<widgets::IntegerEntry>();
109 int_entry->set_value(std::get<int>(record.value));
110 record_entry = int_entry;
111 } else {
112 auto double_entry = Gtk::make_managed<widgets::DoubleEntry>();
113 double_entry->set_value(std::get<double>(record.value));
114 record_entry = double_entry;
115 }
116 record_entry->set_width_chars(6);
117 record_entry->set_valign(Gtk::ALIGN_END);
118 record_entry->set_vexpand(false);
119 record_box->pack_start(*record_entry, Gtk::PACK_SHRINK);
120 }
121
122 widget_map_.emplace(record.cid,
123 std::make_pair(std::move(record_check), record_entry));
124
125 return record_box;
126 }
127
128 Gtk::Notebook* FindProblemsDlg::build_notebook(
129 const std::vector<ProblemTab>& pr_tabs) {
130 auto tabs = Gtk::make_managed<Gtk::Notebook>();
131
132 for (const ProblemTab& tab : pr_tabs) {
133 auto record_page = Gtk::make_managed<Gtk::VBox>();
134
135 // If ALL the records on this page have numerical entry, we want them
136 // neatly aligned.
137 bool align_entries = !std::any_of(
138 tab.records.cbegin(), tab.records.cend(), [](const auto& rec) {
139 return std::holds_alternative<std::monostate>(rec.value);
140 });
141 auto width_group =
142 align_entries ? Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL)
143 : (Glib::RefPtr<Gtk::SizeGroup>)nullptr;
144
145 auto height_group = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_VERTICAL);
146 for (const ProblemRecord& record : tab.records) {
147 Gtk::HBox* record_box = build_record_box(record, width_group);
148 height_group->add_widget(*record_box);
149
150 record_page->pack_start(*record_box, Gtk::PACK_SHRINK);
151 }
152 tabs->append_page(*record_page, tab.label);
153 }
154 return tabs;
155 }
156
157 void FindProblemsDlg::set_all_checkboxes(bool status) {
158 for (auto& [cid, widgets] : widget_map_) {
159 widgets.first.set_active(status);
160 }
161 }
162
163 ProblemRecordsOut FindProblemsDlg::show(GWindow parent,
164 const std::vector<ProblemTab>& pr_tabs,
165 double& near) {
166 FindProblemsDlg dialog(parent, pr_tabs, near);
167
168 Gtk::ResponseType result = dialog.run();
169 ProblemRecordsOut records_out;
170
171 if (result != Gtk::RESPONSE_OK) {
172 // Return empty result whenever the user canceled the dialog.
173 return records_out;
174 }
175
176 for (const ProblemTab& tab : pr_tabs) {
177 for (const ProblemRecord& record : tab.records) {
178 const auto& [checkbox, entry] = dialog.widget_map_[record.cid];
179 if (!checkbox.get_active()) continue;
180
181 NumericalValue new_value;
182 if (entry) {
183 new_value = entry->get_num_value();
184 }
185 records_out.emplace(record.cid, new_value);
186 }
187 }
188 near = dialog.near_value_entry_.get_value();
189
190 return records_out;
191 }
192
193 bool find_problems_dialog(GWindow parent, std::vector<ProblemTab>& pr_tabs,
194 double& near) {
195 // To avoid instability, the GTK application is lazily initialized only when
196 // a GTK window is invoked.
197 ff::app::GtkApp();
198
199 ff::dlg::ProblemRecordsOut result =
200 ff::dlg::FindProblemsDlg::show(parent, pr_tabs, near);
201
202 for (ProblemTab& tab : pr_tabs) {
203 for (ProblemRecord& rec : tab.records) {
204 rec.active = result.count(rec.cid);
205 if (rec.active) {
206 rec.value = result[rec.cid];
207 }
208 }
209 }
210
211 return (!result.empty());
212 }
213
214 } // namespace ff::dlg