148 std::function<
void()> on_update)
149 : wxPanel(parent, wxID_ANY),
151 m_on_update(std::move(on_update)),
152 kIncludeBtnId(wxWindow::NewControlId()),
153 kExcludeBtnId(wxWindow::NewControlId()),
154 kEditBtnId(wxWindow::NewControlId()),
155 kListboxId(wxWindow::NewControlId()),
156 kSummaryId(wxWindow::NewControlId()),
157 kSetBtnId(wxWindow::NewControlId()),
158 kClearBtnId(wxWindow::NewControlId()) {
159 auto flags = wxSizerFlags(0).Border();
160 auto vbox =
new wxStaticBoxSizer(wxVERTICAL,
this, _(
"Message Types"));
161 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
163 auto radiobox =
new wxBoxSizer(wxVERTICAL);
164 radiobox->Add(
new wxRadioButton(
this, kIncludeBtnId, _(
"Include messages"),
165 wxDefaultPosition, wxDefaultSize,
168 new wxRadioButton(
this, kExcludeBtnId, _(
"Exclude messages")));
169 radiobox->Add(1, 1, 1);
170 radiobox->Add(
new wxButton(
this, kSetBtnId, _(
"Check all")), flags);
171 radiobox->Add(
new wxButton(
this, kClearBtnId, _(
"Clear all")), flags);
172 hbox->Add(radiobox, flags.Expand());
175 wxArrayString choices;
176 auto msg_types = GetActiveMessages();
177 for (
const auto& msg_type : msg_types) choices.Add(msg_type);
178 auto listbox =
new wxCheckListBox(
this, kListboxId, wxDefaultPosition,
179 wxDefaultSize, choices, 1);
180 hbox->Add(
new wxStaticText(
this, kSummaryId,
""), flags);
181 hbox->Add(listbox, flags);
183 auto edit_box =
new wxBoxSizer(wxVERTICAL);
184 auto edit_btn =
new EditButton(
this, kEditBtnId, [&] { OnEditClick(); });
185 edit_box->Add(edit_btn, wxSizerFlags());
186 hbox->Add(edit_box, flags.Expand());
188 wxWindow* btn = GetWindowById<wxWindow>(kSetBtnId);
190 btn->Bind(wxEVT_BUTTON, [&](wxCommandEvent&) { SetAllItems(
true); });
191 btn = GetWindowById<wxWindow>(kClearBtnId);
193 btn->Bind(wxEVT_BUTTON, [&](wxCommandEvent&) { SetAllItems(
false); });
195 vbox->Add(hbox, flags.Expand());
199 GetWindowById<wxStaticText>(kSummaryId)->SetLabel(GetSummary());
202 listbox->Bind(wxEVT_CHECKLISTBOX,
203 [&](wxCommandEvent& ev) { OnItemCheck(ev.GetInt()); });
207 std::string GetListboxItem(wxCheckListBox* listbox,
unsigned i)
const {
208 return listbox->GetString(i).ToStdString();
211 void SetAllItems(
bool value) {
212 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
213 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
214 listbox->Check(i, value);
219 std::string GetSummary() {
220 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
222 if (m_filter.exclude_msg.empty() && m_filter.include_msg.empty())
224 bool excluded = !m_filter.exclude_msg.empty();
226 excluded ? m_filter.exclude_msg.size() : m_filter.include_msg.size();
227 size_t all = listbox->GetCount();
228 if (all == checked)
return _(
"All");
230 std::stringstream ss;
231 ss <<
"[" << checked << _(
" of ") << all <<
"]";
240 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
241 auto summary = GetWindowById<wxStaticText>(kSummaryId);
242 auto edit_button = GetWindowById<EditButton>(kEditBtnId);
243 bool is_editing = listbox->IsShown();
244 listbox->Show(!is_editing);
245 summary->Show(is_editing);
246 summary->SetLabel(GetSummary());
247 edit_button->SetIcon(!is_editing);
248 GetWindowById<wxWindow>(kSetBtnId)->Show(!is_editing);
249 GetWindowById<wxWindow>(kClearBtnId)->Show(!is_editing);
258 void OnItemCheck(
int ix) {
259 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
261 for (
unsigned i = 0; i < listbox->GetCount(); i += 1)
262 if (listbox->IsChecked(i)) checked += 1;
272 void ImportFromFilter() {
273 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
274 auto exclude_btn = GetWindowById<wxRadioButton>(kExcludeBtnId);
275 auto include_btn = GetWindowById<wxRadioButton>(kIncludeBtnId);
277 exclude_btn->SetValue(m_filter.exclude_msg.size() > 0);
278 if (include_btn->GetValue()) {
279 if (m_filter.include_msg.empty()) {
280 for (
unsigned i = 0; i < listbox->GetCount(); i++) listbox->Check(i);
282 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
283 std::string item = GetListboxItem(listbox, i);
284 if (m_filter.include_msg.count(item)) listbox->Check(i);
288 if (m_filter.exclude_msg.empty()) {
289 for (
unsigned i = 0; i < listbox->GetCount(); i++) listbox->Check(i);
291 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
292 std::string item = GetListboxItem(listbox, i);
293 if (m_filter.exclude_msg.count(item)) listbox->Check(i);
300 void ExportToFilter() {
301 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
302 auto include_btn = GetWindowById<wxRadioButton>(kIncludeBtnId);
303 m_filter.include_msg.clear();
304 m_filter.exclude_msg.clear();
306 include_btn->GetValue() ? m_filter.include_msg : m_filter.exclude_msg;
307 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
308 if (!listbox->IsChecked(i))
continue;
309 std::string item = GetListboxItem(listbox, i);
310 the_set.insert(item);
312 if (the_set.size() == listbox->GetCount()) {
313 m_filter.include_msg.clear();
314 m_filter.exclude_msg.clear();
320 std::function<void()> m_on_update;
321 const int kIncludeBtnId;
322 const int kExcludeBtnId;
323 const int kEditBtnId;
324 const int kListboxId;
325 const int kSummaryId;
327 const int kClearBtnId;
341 SetPanel(wxWindow* parent, std::set<T>& set, std::function<
void()> on_update,
342 const std::string& label, std::function<wxArrayString()> get_choices)
345 m_on_update(std::move(on_update)),
346 kEditBtnId(wxWindow::NewControlId()),
347 kListboxId(wxWindow::NewControlId()),
348 kListLabelId(wxWindow::NewControlId()) {
349 wxPanel::Create(parent, wxID_ANY);
350 auto flags = wxSizerFlags(0).Border();
351 auto vbox =
new wxBoxSizer(wxVERTICAL);
352 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
353 hbox->Add(
new wxStaticText(
this, wxID_ANY, label), flags);
356 auto listbox =
new wxCheckListBox(
this, kListboxId, wxDefaultPosition,
357 wxDefaultSize, get_choices());
358 hbox->Add(listbox, flags);
359 auto list_label =
new wxStaticText(
this, kListLabelId,
"");
360 hbox->Add(list_label, flags);
361 auto edit_btn =
new EditButton(
this, kEditBtnId, [&] { OnEditClick(); });
362 hbox->Add(edit_btn, flags);
363 vbox->Add(hbox, flags.Expand());
367 list_label->SetLabel(GetListLabel());
373 listbox->Bind(wxEVT_CHECKLISTBOX,
374 [&](wxCommandEvent& ev) { OnItemCheck(ev.GetInt()); });
379 T GetListboxItem(wxCheckListBox* listbox,
unsigned i)
const {
380 if constexpr (std::is_same<T, std::string>::value)
381 return listbox->GetString(i).ToStdString();
382 else if constexpr (std::is_same<T, NavAddr::Bus>::value)
383 return NavAddr::StringToBus(listbox->GetString(i).ToStdString());
384 else if constexpr (std::is_same<T, NavmsgStatus::Direction>::value)
385 return StringToDirection(listbox->GetString(i).ToStdString());
386 else if constexpr (std::is_same<T, NavmsgStatus::Accepted>::value)
388 listbox->GetString(i).ToStdString());
390 assert(
false &&
"bad type...");
394 std::string GetListLabel() {
395 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
396 if (m_set.empty() || m_set.size() == listbox->GetCount())
return _(
"All");
397 std::stringstream ss;
398 ss <<
"[" << m_set.size() << _(
" of ") << listbox->GetCount() <<
"]";
407 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
408 auto list_label = GetWindowById<wxStaticText>(kListLabelId);
409 auto edit_button = GetWindowById<EditButton>(kEditBtnId);
410 bool is_editing = listbox->IsShown();
411 listbox->Show(!is_editing);
412 list_label->Show(is_editing);
413 list_label->SetLabel(GetListLabel());
414 edit_button->SetIcon(!is_editing);
422 void OnItemCheck(
int ix) {
423 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
424 auto list_label = GetWindowById<wxStaticText>(kListLabelId);
426 for (
unsigned i = 0; i < listbox->GetCount(); i += 1)
427 if (listbox->IsChecked(i)) checked += 1;
432 list_label->SetLabel(GetListLabel());
437 void ImportFromFilter() {
438 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
440 for (
unsigned i = 0; i < listbox->GetCount(); i++) listbox->Check(i);
442 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
443 T item = GetListboxItem(listbox, i);
444 if (m_set.count(item) > 0) listbox->Check(i);
450 void ExportToFilter() {
451 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
453 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
454 if (!listbox->IsChecked(i))
continue;
455 T item = GetListboxItem(listbox, i);
458 if (m_set.size() == listbox->GetCount()) m_set.clear();
463 std::function<void()> m_on_update;
464 const int kEditBtnId;
465 const int kListboxId;
466 const int kListLabelId;
546 class Buttons :
public wxPanel {
548 Buttons(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
549 auto vbox =
new wxBoxSizer(wxVERTICAL);
550 auto buttons =
new wxStdDialogButtonSizer();
551 buttons->AddButton(
new wxButton(
this, wxID_CLOSE));
552 buttons->AddButton(
new wxButton(
this, wxID_APPLY));
553 vbox->Add(buttons, wxSizerFlags().Expand());
562 std::function<
void(
const std::string&)> on_update,
563 std::function<
void(
const std::string&)> on_apply)
564 : wxFrame(parent, wxID_ANY,
565 [name] {
return _(
"Edit filter: ") + name; }()),
566 m_on_update(std::move(on_update)),
567 m_on_apply(std::move(on_apply)),
569 SetName(kEditFilterFrameName + name);
570 m_filter = filters_on_disk::Read(m_name);
572 auto flags = wxSizerFlags().Border().Expand();
573 auto vbox =
new wxBoxSizer(wxVERTICAL);
575 vbox->Add(
new IfacePanel(
this, m_filter, [&] { Update(); }), flags);
576 vbox->Add(
new wxStaticLine(
this), flags.Expand());
577 vbox->Add(
new BusPanel(
this, m_filter, [&] { Update(); }), flags);
578 vbox->Add(
new wxStaticLine(
this), flags.Expand());
579 vbox->Add(
new DirectionPanel(
this, m_filter, [&] { Update(); }), flags);
580 vbox->Add(
new wxStaticLine(
this), flags.Expand());
581 vbox->Add(
new AcceptedPanel(
this, m_filter, [&] { Update(); }), flags);
582 vbox->Add(
new MsgTypePanel(
this, m_filter, [&] { Update(); }), flags);
583 vbox->Add(
new Buttons(
this), flags);
587 Bind(wxEVT_CLOSE_WINDOW, [&](wxCloseEvent&) { Destroy(); });
588 Bind(wxEVT_BUTTON, [&](wxCommandEvent& evt) { OnButtonEvent(evt); });
593 filters_on_disk::Write(m_filter, m_name);
597 void OnButtonEvent(wxCommandEvent& evt) {
598 if (evt.GetId() == wxID_CLOSE) {
601 }
else if (evt.GetId() == wxID_APPLY) {
607 std::function<void(
const std::string&)> m_on_update;
608 std::function<void(
const std::string&)> m_on_apply;