154 std::function<
void()> on_update)
155 : wxPanel(parent, wxID_ANY),
157 m_on_update(std::move(on_update)),
158 kIncludeBtnId(wxWindow::NewControlId()),
159 kExcludeBtnId(wxWindow::NewControlId()),
160 kEditBtnId(wxWindow::NewControlId()),
161 kListboxId(wxWindow::NewControlId()),
162 kSummaryId(wxWindow::NewControlId()),
163 kSetBtnId(wxWindow::NewControlId()),
164 kClearBtnId(wxWindow::NewControlId()) {
165 auto flags = wxSizerFlags(0).Border();
166 auto vbox =
new wxStaticBoxSizer(wxVERTICAL,
this, _(
"Message Types"));
167 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
169 auto radiobox =
new wxBoxSizer(wxVERTICAL);
170 radiobox->Add(
new wxRadioButton(
this, kIncludeBtnId, _(
"Include messages"),
171 wxDefaultPosition, wxDefaultSize,
174 new wxRadioButton(
this, kExcludeBtnId, _(
"Exclude messages")));
175 radiobox->Add(1, 1, 1);
176 radiobox->Add(
new wxButton(
this, kSetBtnId, _(
"Check all")), flags);
177 radiobox->Add(
new wxButton(
this, kClearBtnId, _(
"Clear all")), flags);
178 hbox->Add(radiobox, flags.Expand());
181 wxArrayString choices;
182 auto msg_types = GetActiveMessages();
183 for (
const auto& msg_type : msg_types) choices.Add(msg_type);
184 auto listbox =
new wxCheckListBox(
this, kListboxId, wxDefaultPosition,
185 wxDefaultSize, choices, 1);
186 hbox->Add(
new wxStaticText(
this, kSummaryId,
""), flags);
187 hbox->Add(listbox, flags);
189 auto edit_box =
new wxBoxSizer(wxVERTICAL);
190 auto edit_btn =
new EditButton(
this, kEditBtnId, [&] { OnEditClick(); });
191 edit_box->Add(edit_btn, wxSizerFlags());
192 hbox->Add(edit_box, flags.Expand());
194 wxWindow* btn = GetWindowById<wxWindow>(kSetBtnId);
196 btn->Bind(wxEVT_BUTTON, [&](wxCommandEvent&) { SetAllItems(
true); });
197 btn = GetWindowById<wxWindow>(kClearBtnId);
199 btn->Bind(wxEVT_BUTTON, [&](wxCommandEvent&) { SetAllItems(
false); });
201 vbox->Add(hbox, flags.Expand());
205 GetWindowById<wxStaticText>(kSummaryId)->SetLabel(GetSummary());
208 listbox->Bind(wxEVT_CHECKLISTBOX,
209 [&](wxCommandEvent& ev) { OnItemCheck(ev.GetInt()); });
213 std::string GetListboxItem(wxCheckListBox* listbox,
unsigned i)
const {
214 return listbox->GetString(i).ToStdString();
217 void SetAllItems(
bool value) {
218 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
219 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
220 listbox->Check(i, value);
225 std::string GetSummary() {
226 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
228 if (m_filter.exclude_msg.empty() && m_filter.include_msg.empty())
230 bool excluded = !m_filter.exclude_msg.empty();
232 excluded ? m_filter.exclude_msg.size() : m_filter.include_msg.size();
233 size_t all = listbox->GetCount();
234 if (all == checked)
return _(
"All");
236 std::stringstream ss;
237 ss <<
"[" << checked << _(
" of ") << all <<
"]";
246 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
247 auto summary = GetWindowById<wxStaticText>(kSummaryId);
248 auto edit_button = GetWindowById<EditButton>(kEditBtnId);
249 bool is_editing = listbox->IsShown();
250 listbox->Show(!is_editing);
251 summary->Show(is_editing);
252 summary->SetLabel(GetSummary());
253 edit_button->SetIcon(!is_editing);
254 GetWindowById<wxWindow>(kSetBtnId)->Show(!is_editing);
255 GetWindowById<wxWindow>(kClearBtnId)->Show(!is_editing);
264 void OnItemCheck(
int ix) {
265 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
267 for (
unsigned i = 0; i < listbox->GetCount(); i += 1)
268 if (listbox->IsChecked(i)) checked += 1;
278 void ImportFromFilter() {
279 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
280 auto exclude_btn = GetWindowById<wxRadioButton>(kExcludeBtnId);
281 auto include_btn = GetWindowById<wxRadioButton>(kIncludeBtnId);
283 exclude_btn->SetValue(m_filter.exclude_msg.size() > 0);
284 if (include_btn->GetValue()) {
285 if (m_filter.include_msg.empty()) {
286 for (
unsigned i = 0; i < listbox->GetCount(); i++) listbox->Check(i);
288 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
289 std::string item = GetListboxItem(listbox, i);
290 if (m_filter.include_msg.count(item)) listbox->Check(i);
294 if (m_filter.exclude_msg.empty()) {
295 for (
unsigned i = 0; i < listbox->GetCount(); i++) listbox->Check(i);
297 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
298 std::string item = GetListboxItem(listbox, i);
299 if (m_filter.exclude_msg.count(item)) listbox->Check(i);
306 void ExportToFilter() {
307 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
308 auto include_btn = GetWindowById<wxRadioButton>(kIncludeBtnId);
309 m_filter.include_msg.clear();
310 m_filter.exclude_msg.clear();
312 include_btn->GetValue() ? m_filter.include_msg : m_filter.exclude_msg;
313 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
314 if (!listbox->IsChecked(i))
continue;
315 std::string item = GetListboxItem(listbox, i);
316 the_set.insert(item);
318 if (the_set.size() == listbox->GetCount()) {
319 m_filter.include_msg.clear();
320 m_filter.exclude_msg.clear();
326 std::function<void()> m_on_update;
327 const int kIncludeBtnId;
328 const int kExcludeBtnId;
329 const int kEditBtnId;
330 const int kListboxId;
331 const int kSummaryId;
333 const int kClearBtnId;
347 SetPanel(wxWindow* parent, std::set<T>& set, std::function<
void()> on_update,
348 const std::string& label, std::function<wxArrayString()> get_choices)
351 m_on_update(std::move(on_update)),
352 kEditBtnId(wxWindow::NewControlId()),
353 kListboxId(wxWindow::NewControlId()),
354 kListLabelId(wxWindow::NewControlId()) {
355 wxPanel::Create(parent, wxID_ANY);
356 auto flags = wxSizerFlags(0).Border();
357 auto vbox =
new wxBoxSizer(wxVERTICAL);
358 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
359 hbox->Add(
new wxStaticText(
this, wxID_ANY, label), flags);
363 wxArrayString array = get_choices();
365 for (
auto string : array) max_char = wxMax(max_char,
string.Length());
366 int hsize = (max_char + 8) * wxWindow::GetCharWidth();
369 auto listbox =
new wxCheckListBox(
this, kListboxId, wxDefaultPosition,
370 wxSize(hsize, -1), get_choices());
371 hbox->Add(listbox, flags);
372 auto list_label =
new wxStaticText(
this, kListLabelId,
"");
373 hbox->Add(list_label, flags);
374 auto edit_btn =
new EditButton(
this, kEditBtnId, [&] { OnEditClick(); });
375 hbox->Add(edit_btn, flags);
376 vbox->Add(hbox, flags.Expand());
380 list_label->SetLabel(GetListLabel());
391 listbox->Bind(wxEVT_CHECKLISTBOX,
392 [&](wxCommandEvent& ev) { OnItemCheck(ev.GetInt()); });
397 T GetListboxItem(wxCheckListBox* listbox,
unsigned i)
const {
398 if constexpr (std::is_same<T, std::string>::value)
399 return listbox->GetString(i).ToStdString();
400 else if constexpr (std::is_same<T, NavAddr::Bus>::value)
401 return NavAddr::StringToBus(listbox->GetString(i).ToStdString());
402 else if constexpr (std::is_same<T, NavmsgStatus::Direction>::value)
403 return StringToDirection(listbox->GetString(i).ToStdString());
404 else if constexpr (std::is_same<T, NavmsgStatus::Accepted>::value)
406 listbox->GetString(i).ToStdString());
408 assert(
false &&
"bad type...");
412 std::string GetListLabel() {
413 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
414 if (m_set.empty() || m_set.size() == listbox->GetCount())
return _(
"All");
415 std::stringstream ss;
416 ss <<
"[" << m_set.size() << _(
" of ") << listbox->GetCount() <<
"]";
425 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
427 auto list_label = GetWindowById<wxStaticText>(kListLabelId);
428 auto edit_button = GetWindowById<EditButton>(kEditBtnId);
429 bool is_editing = listbox->IsShown();
430 listbox->Show(!is_editing);
431 list_label->Show(is_editing);
432 list_label->SetLabel(GetListLabel());
433 edit_button->SetIcon(!is_editing);
441 void OnItemCheck(
int ix) {
442 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
443 auto list_label = GetWindowById<wxStaticText>(kListLabelId);
445 for (
unsigned i = 0; i < listbox->GetCount(); i += 1)
446 if (listbox->IsChecked(i)) checked += 1;
451 list_label->SetLabel(GetListLabel());
456 void ImportFromFilter() {
457 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
459 for (
unsigned i = 0; i < listbox->GetCount(); i++) listbox->Check(i);
461 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
462 T item = GetListboxItem(listbox, i);
463 if (m_set.count(item) > 0) listbox->Check(i);
469 void ExportToFilter() {
470 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
472 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
473 if (!listbox->IsChecked(i))
continue;
474 T item = GetListboxItem(listbox, i);
477 if (m_set.size() == listbox->GetCount()) m_set.clear();
482 std::function<void()> m_on_update;
483 const int kEditBtnId;
484 const int kListboxId;
485 const int kListLabelId;
565 class Buttons :
public wxPanel {
567 Buttons(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
568 auto vbox =
new wxBoxSizer(wxVERTICAL);
569 auto buttons =
new wxStdDialogButtonSizer();
570 buttons->AddButton(
new wxButton(
this, wxID_CLOSE));
571 buttons->AddButton(
new wxButton(
this, wxID_APPLY));
572 vbox->Add(buttons, wxSizerFlags().Expand());
581 std::function<
void(
const std::string&)> on_update,
582 std::function<
void(
const std::string&)> on_apply)
583 : wxFrame(parent, wxID_ANY,
584 [name] {
return _(
"Edit filter: ") + name; }()),
585 m_on_update(std::move(on_update)),
586 m_on_apply(std::move(on_apply)),
588 SetName(kEditFilterFrameName + name);
589 m_filter = filters_on_disk::Read(m_name);
591 auto flags = wxSizerFlags().Border().Expand();
592 auto vbox =
new wxBoxSizer(wxVERTICAL);
594 vbox->Add(
new IfacePanel(
this, m_filter, [&] { Update(); }), flags);
595 vbox->Add(
new wxStaticLine(
this), flags.Expand());
596 auto buspanel =
new BusPanel(
this, m_filter, [&] { Update(); });
597 vbox->Add(buspanel, flags);
598 vbox->Add(
new wxStaticLine(
this), flags.Expand());
599 vbox->Add(
new DirectionPanel(
this, m_filter, [&] { Update(); }), flags);
600 vbox->Add(
new wxStaticLine(
this), flags.Expand());
601 vbox->Add(
new AcceptedPanel(
this, m_filter, [&] { Update(); }), flags);
602 vbox->Add(
new MsgTypePanel(
this, m_filter, [&] { Update(); }), flags);
603 vbox->Add(
new Buttons(
this), flags);
607 Bind(wxEVT_CLOSE_WINDOW, [&](wxCloseEvent&) { Destroy(); });
608 Bind(wxEVT_BUTTON, [&](wxCommandEvent& evt) { OnButtonEvent(evt); });
613 filters_on_disk::Write(m_filter, m_name);
617 void OnButtonEvent(wxCommandEvent& evt) {
618 if (evt.GetId() == wxID_CLOSE) {
621 }
else if (evt.GetId() == wxID_APPLY) {
627 std::function<void(
const std::string&)> m_on_update;
628 std::function<void(
const std::string&)> m_on_apply;