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);
357 wxArrayString array = get_choices();
359 for (
auto string : array) max_char = wxMax(max_char,
string.Length());
360 int hsize = (max_char + 8) * wxWindow::GetCharWidth();
363 auto listbox =
new wxCheckListBox(
this, kListboxId, wxDefaultPosition,
364 wxSize(hsize, -1), get_choices());
365 hbox->Add(listbox, flags);
366 auto list_label =
new wxStaticText(
this, kListLabelId,
"");
367 hbox->Add(list_label, flags);
368 auto edit_btn =
new EditButton(
this, kEditBtnId, [&] { OnEditClick(); });
369 hbox->Add(edit_btn, flags);
370 vbox->Add(hbox, flags.Expand());
374 list_label->SetLabel(GetListLabel());
385 listbox->Bind(wxEVT_CHECKLISTBOX,
386 [&](wxCommandEvent& ev) { OnItemCheck(ev.GetInt()); });
391 T GetListboxItem(wxCheckListBox* listbox,
unsigned i)
const {
392 if constexpr (std::is_same<T, std::string>::value)
393 return listbox->GetString(i).ToStdString();
394 else if constexpr (std::is_same<T, NavAddr::Bus>::value)
395 return NavAddr::StringToBus(listbox->GetString(i).ToStdString());
396 else if constexpr (std::is_same<T, NavmsgStatus::Direction>::value)
397 return StringToDirection(listbox->GetString(i).ToStdString());
398 else if constexpr (std::is_same<T, NavmsgStatus::Accepted>::value)
400 listbox->GetString(i).ToStdString());
402 assert(
false &&
"bad type...");
406 std::string GetListLabel() {
407 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
408 if (m_set.empty() || m_set.size() == listbox->GetCount())
return _(
"All");
409 std::stringstream ss;
410 ss <<
"[" << m_set.size() << _(
" of ") << listbox->GetCount() <<
"]";
419 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
421 auto list_label = GetWindowById<wxStaticText>(kListLabelId);
422 auto edit_button = GetWindowById<EditButton>(kEditBtnId);
423 bool is_editing = listbox->IsShown();
424 listbox->Show(!is_editing);
425 list_label->Show(is_editing);
426 list_label->SetLabel(GetListLabel());
427 edit_button->SetIcon(!is_editing);
435 void OnItemCheck(
int ix) {
436 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
437 auto list_label = GetWindowById<wxStaticText>(kListLabelId);
439 for (
unsigned i = 0; i < listbox->GetCount(); i += 1)
440 if (listbox->IsChecked(i)) checked += 1;
445 list_label->SetLabel(GetListLabel());
450 void ImportFromFilter() {
451 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
453 for (
unsigned i = 0; i < listbox->GetCount(); i++) listbox->Check(i);
455 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
456 T item = GetListboxItem(listbox, i);
457 if (m_set.count(item) > 0) listbox->Check(i);
463 void ExportToFilter() {
464 auto listbox = GetWindowById<wxCheckListBox>(kListboxId);
466 for (
unsigned i = 0; i < listbox->GetCount(); i += 1) {
467 if (!listbox->IsChecked(i))
continue;
468 T item = GetListboxItem(listbox, i);
471 if (m_set.size() == listbox->GetCount()) m_set.clear();
476 std::function<void()> m_on_update;
477 const int kEditBtnId;
478 const int kListboxId;
479 const int kListLabelId;
559 class Buttons :
public wxPanel {
561 Buttons(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
562 auto vbox =
new wxBoxSizer(wxVERTICAL);
563 auto buttons =
new wxStdDialogButtonSizer();
564 buttons->AddButton(
new wxButton(
this, wxID_CLOSE));
565 buttons->AddButton(
new wxButton(
this, wxID_APPLY));
566 vbox->Add(buttons, wxSizerFlags().Expand());
575 std::function<
void(
const std::string&)> on_update,
576 std::function<
void(
const std::string&)> on_apply)
577 : wxFrame(parent, wxID_ANY,
578 [name] {
return _(
"Edit filter: ") + name; }()),
579 m_on_update(std::move(on_update)),
580 m_on_apply(std::move(on_apply)),
582 SetName(kEditFilterFrameName + name);
583 m_filter = filters_on_disk::Read(m_name);
585 auto flags = wxSizerFlags().Border().Expand();
586 auto vbox =
new wxBoxSizer(wxVERTICAL);
588 vbox->Add(
new IfacePanel(
this, m_filter, [&] { Update(); }), flags);
589 vbox->Add(
new wxStaticLine(
this), flags.Expand());
590 auto buspanel =
new BusPanel(
this, m_filter, [&] { Update(); });
591 vbox->Add(buspanel, flags);
592 vbox->Add(
new wxStaticLine(
this), flags.Expand());
593 vbox->Add(
new DirectionPanel(
this, m_filter, [&] { Update(); }), flags);
594 vbox->Add(
new wxStaticLine(
this), flags.Expand());
595 vbox->Add(
new AcceptedPanel(
this, m_filter, [&] { Update(); }), flags);
596 vbox->Add(
new MsgTypePanel(
this, m_filter, [&] { Update(); }), flags);
597 vbox->Add(
new Buttons(
this), flags);
601 Bind(wxEVT_CLOSE_WINDOW, [&](wxCloseEvent&) { Destroy(); });
602 Bind(wxEVT_BUTTON, [&](wxCommandEvent& evt) { OnButtonEvent(evt); });
607 filters_on_disk::Write(m_filter, m_name);
611 void OnButtonEvent(wxCommandEvent& evt) {
612 if (evt.GetId() == wxID_CLOSE) {
615 }
else if (evt.GetId() == wxID_APPLY) {
621 std::function<void(
const std::string&)> m_on_update;
622 std::function<void(
const std::string&)> m_on_apply;