OpenCPN Partial API docs
Loading...
Searching...
No Matches
filter_sentence_dlg.cpp
Go to the documentation of this file.
1
2/**************************************************************************
3 * Copyright (C) 2022 by David S. Register *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
24
25#include "filter_sentence_dlg.h"
26
27#include <cstddef>
28
29#include <wx/arrstr.h>
30#include <wx/string.h>
31
32#ifdef __ANDROID__
33#include "androidUTIL.h"
34#endif
35
36static wxString StringArrayToString(const wxArrayString& arr) {
37 wxString ret = wxEmptyString;
38 for (size_t i = 0; i < arr.Count(); i++) {
39 if (i > 0) ret.Append(",");
40 ret.Append(arr[i]);
41 }
42 return ret;
43}
44
45FilterSentenceDlg::FilterSentenceDlg(wxWindow* parent, FilterDirection dir,
46 ListType type, const wxArrayString& list)
47 : wxDialog(parent, wxID_ANY, _("Sentence Filter"), wxDefaultPosition,
48 wxSize(280, 420)),
49 m_type(type),
50 m_dir(dir),
51 m_sentences(NMEA0183().GetRecognizedArray()) {
52 m_sentences.Sort();
53 auto* mainSizer = new wxBoxSizer(wxVERTICAL);
54 auto* secondSizer = new wxBoxSizer(wxHORIZONTAL);
55 auto* pclbBox = new wxStaticBox(this, wxID_ANY, GetBoxLabel());
56 auto* stcSizer = new wxStaticBoxSizer(pclbBox, wxVERTICAL);
57 m_sentences_clb = new wxCheckListBox(this, wxID_ANY, wxDefaultPosition,
58 wxDefaultSize, m_sentences);
59 auto* btnEntrySizer = new wxBoxSizer(wxVERTICAL);
60 auto* btnCheckAll = new wxButton(this, wxID_ANY, _("Select All"));
61 auto* btnClearAll = new wxButton(this, wxID_ANY, _("Clear All"));
62 auto* btnAdd = new wxButton(this, wxID_ANY, _("Add"));
63 m_del_btn = new wxButton(this, wxID_ANY, _("Delete"));
64 m_del_btn->Disable();
65 auto* btnSizer = new wxStdDialogButtonSizer();
66 auto* btnOK = new wxButton(this, wxID_OK);
67 auto* btnCancel = new wxButton(this, wxID_CANCEL, _("Cancel"));
68
69 secondSizer->Add(stcSizer, 1, wxALL | wxEXPAND, 5);
70 stcSizer->Add(m_sentences_clb, 1, wxALL | wxEXPAND, 5);
71 btnEntrySizer->Add(btnCheckAll, 0, wxALL, 5);
72 btnEntrySizer->Add(btnClearAll, 0, wxALL, 5);
73 btnEntrySizer->AddSpacer(1);
74 btnEntrySizer->Add(btnAdd, 0, wxALL, 5);
75 btnEntrySizer->Add(m_del_btn, 0, wxALL, 5);
76 secondSizer->Add(btnEntrySizer, 0, wxALL | wxEXPAND, 5);
77 mainSizer->Add(secondSizer, 1, wxEXPAND, 5);
78 btnSizer->AddButton(btnOK);
79 btnSizer->AddButton(btnCancel);
80 btnSizer->Realize();
81 mainSizer->Add(btnSizer, 0, wxALL | wxEXPAND, 5);
82
83 SetSizer(mainSizer);
84 mainSizer->SetSizeHints(this);
85 Centre();
86
87 btnAdd->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
88 wxCommandEventHandler(FilterSentenceDlg::OnAddClick), nullptr,
89 this);
90 m_del_btn->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
91 wxCommandEventHandler(FilterSentenceDlg::OnDeleteClick),
92 nullptr, this);
93 m_sentences_clb->Connect(
94 wxEVT_COMMAND_LISTBOX_SELECTED,
95 wxCommandEventHandler(FilterSentenceDlg::OnCLBSelect), nullptr, this);
96 btnCheckAll->Connect(
97 wxEVT_COMMAND_BUTTON_CLICKED,
98 wxCommandEventHandler(FilterSentenceDlg::OnCheckAllClick), nullptr, this);
99 btnClearAll->Connect(
100 wxEVT_COMMAND_BUTTON_CLICKED,
101 wxCommandEventHandler(FilterSentenceDlg::OnClearAllClick), nullptr, this);
102 Populate(list);
103}
104
105wxString FilterSentenceDlg::GetBoxLabel() const {
106 if (m_dir == FILTER_OUTPUT)
107 return m_type == WHITELIST ? _("Transmit sentences") : _("Drop sentences");
108 else
109 return m_type == WHITELIST ? _("Accept only sentences")
110 : _("Ignore sentences");
111}
112
113void FilterSentenceDlg::Populate(const wxArrayString& list) {
114 if (m_dir == FILTER_OUTPUT) {
115 wxString s;
116 m_sentences.Add(g_TalkerIdText + wxString("RMB"));
117 m_sentences.Add(g_TalkerIdText + wxString("RMC"));
118 m_sentences.Add(g_TalkerIdText + wxString("APB"));
119 m_sentences.Add(g_TalkerIdText + wxString("XTE"));
120 }
121 m_sentences.Add("AIVDM");
122 m_sentences.Add("AIVDO");
123 m_sentences.Add("FRPOS");
124 m_sentences.Add(g_TalkerIdText);
125 m_sentences.Add("CD");
126 m_sentences.Sort();
127 m_sentences_clb->Clear();
128 m_sentences_clb->InsertItems(m_sentences, 0);
129
130 wxArrayString new_strings;
131 if (list.Count() == 0) {
132 for (size_t i = 0; i < m_sentences_clb->GetCount(); ++i)
133 m_sentences_clb->Check(i, m_type == WHITELIST);
134 } else {
135 for (size_t i = 0; i < list.Count(); ++i) {
136 int item = m_sentences_clb->FindString(list[i]);
137 if (item != wxNOT_FOUND)
138 m_sentences_clb->Check(item);
139 else
140 new_strings.Add(list[i]);
141 }
142 if (new_strings.GetCount()) {
143 m_sentences_clb->InsertItems(new_strings, m_sentences_clb->GetCount());
144 for (size_t i = 0; i < new_strings.GetCount(); ++i) {
145 int item = m_sentences_clb->FindString(new_strings[i]);
146 if (item != wxNOT_FOUND) m_sentences_clb->Check(item);
147 }
148 }
149 }
150}
151
152wxString FilterSentenceDlg::GetSentences() {
153 wxArrayString retString;
154 for (size_t i = 0; i < m_sentences_clb->GetCount(); i++) {
155 if (m_sentences_clb->IsChecked(i))
156 retString.Add(m_sentences_clb->GetString(i));
157 }
158 return StringArrayToString(retString);
159}
160
161void FilterSentenceDlg::OnCLBSelect(wxCommandEvent& e) {
162 // Only activate the "Delete" button if the selection is not in the standard
163 // list
164 m_del_btn->Enable(m_sentences.Index(e.GetString()) == wxNOT_FOUND);
165}
166
167void FilterSentenceDlg::OnAddClick(wxCommandEvent& event) {
168#ifdef __ANDROID__
169 androidDisableRotation();
170#endif
171
172 wxTextEntryDialog textdlg(
173 this,
174 _("Enter the NMEA sentence (2, 3 or 5 characters)\n or a valid REGEX "
175 "expression (6 characters or longer)"),
176 _("Enter the NMEA sentence"));
177
178 textdlg.SetTextValidator(wxFILTER_ASCII);
179 int result = textdlg.ShowModal();
180
181#ifdef __ANDROID__
182 androidEnableRotation();
183#endif
184
185 if (result == wxID_CANCEL) return;
186 wxString stc = textdlg.GetValue();
187
188 if (stc.Length() == 2 || stc.Length() == 3 || stc.Length() == 5) {
189 m_sentences_clb->Append(stc);
190 m_sentences_clb->Check(m_sentences_clb->FindString(stc));
191 return;
192 } else if (stc.Length() < 2) {
193 OCPNMessageBox(
194 this,
195 _("An NMEA sentence is generally 3 characters long (like RMC, GGA etc.)\n \
196 It can also have a two letter prefix identifying the source, or TALKER, of the message.\n \
197 The whole sentences then looks like GPGGA or AITXT.\n \
198 You may filter out all the sentences with certain TALKER prefix (like GP, AI etc.).\n \
199 The filter also accepts Regular Expressions (REGEX) with 6 or more characters. \n\n"),
200 _("OpenCPN Info"));
201 return;
202 }
203
204 else {
205 // Verify that a longer text entry is a valid RegEx
206 wxRegEx r(stc);
207 if (r.IsValid()) {
208 m_sentences_clb->Append(stc);
209 m_sentences_clb->Check(m_sentences_clb->FindString(stc));
210 return;
211 } else {
212 OCPNMessageBox(this, _("REGEX syntax error: \n") + stc,
213 _("OpenCPN Info"));
214 return;
215 }
216 }
217}
218
219void FilterSentenceDlg::OnDeleteClick(wxCommandEvent& event) {
220 m_sentences_clb->Delete(m_sentences_clb->GetSelection());
221}
222
223void FilterSentenceDlg::OnClearAllClick(wxCommandEvent& event) {
224 for (size_t i = 0; i < m_sentences_clb->GetCount(); i++)
225 m_sentences_clb->Check(i, false);
226}
227
228void FilterSentenceDlg::OnCheckAllClick(wxCommandEvent& event) {
229 for (size_t i = 0; i < m_sentences_clb->GetCount(); i++)
230 m_sentences_clb->Check(i, true);
231}