OpenCPN Partial API docs
Loading...
Searching...
No Matches
priority_gui.cpp
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose:
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2022 by David S. Register *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 ***************************************************************************
25 *
26 *
27 */
28
29#include <wx/wxprec.h>
30
31#ifndef WX_PRECOMP
32#include <wx/wx.h>
33#endif // precompiled headers
34
35#include <wx/app.h>
36#include <wx/tokenzr.h>
37
38#ifdef __OCPN__ANDROID__
39#include "androidUTIL.h"
40#include "qdebug.h"
41#include <QtWidgets/QScroller>
42#endif
43
44#include "priority_gui.h"
45#include "ocpn_app.h"
46#include "model/comm_bridge.h"
47#include "ocpn_frame.h"
48#include "ocpn_plugin.h"
49
50class PriorityEntry : public wxTreeItemData {
51public:
52 PriorityEntry(int category, int index) {
53 m_category = category, m_index = index;
54 }
55 ~PriorityEntry() {};
56
57 int m_category, m_index;
58};
59
60PriorityDlg::PriorityDlg(wxWindow* parent)
61 : wxDialog(parent, wxID_ANY, _("Adjust Comm Priorities"), wxDefaultPosition,
62 wxSize(480, 420), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
63 m_selIndex = 0;
64 m_selmap_index = 0;
65
66 wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
67 SetSizer(mainSizer);
68
69 wxBoxSizer* secondSizer = new wxBoxSizer(wxHORIZONTAL);
70 mainSizer->Add(secondSizer, 1, wxEXPAND, 5);
71
72 wxStaticBox* pclbBox = new wxStaticBox(this, wxID_ANY, _("Priority List"));
73 wxStaticBoxSizer* stcSizer = new wxStaticBoxSizer(pclbBox, wxVERTICAL);
74 secondSizer->Add(stcSizer, 1, wxALL | wxEXPAND, 5);
75
76 m_prioTree = new wxTreeCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
77 stcSizer->Add(m_prioTree, 1, wxALL | wxEXPAND, 5);
78 wxFont* pF = OCPNGetFont(_("Dialog"));
79 m_pF = pF;
80 m_prioTree->SetFont(*pF);
81#ifdef __ANDROID__
82 m_prioTree->GetHandle()->setStyleSheet(
83 getWideScrollBarsStyleSheet() /*getScrollBarsStyleSheet()*/);
84 QScroller::ungrabGesture(m_prioTree->GetHandle());
85#endif
86
87 wxBoxSizer* btnEntrySizer = new wxBoxSizer(wxVERTICAL);
88 secondSizer->Add(btnEntrySizer, 0, wxALL | wxEXPAND, 5);
89 btnMoveUp = new wxButton(this, wxID_ANY, _("Move Up"));
90 btnMoveDown = new wxButton(this, wxID_ANY, _("Move Down"));
91 btnMoveUp->Disable();
92 btnMoveDown->Disable();
93
94 btnEntrySizer->Add(btnMoveUp, 0, wxALL, 5);
95 btnEntrySizer->Add(btnMoveDown, 0, wxALL, 5);
96
97 btnEntrySizer->AddSpacer(15);
98
99 btnRefresh = new wxButton(this, wxID_ANY, _("Refresh"));
100 btnClear = new wxButton(this, wxID_ANY, _("Clear All"));
101
102 btnEntrySizer->Add(btnRefresh, 0, wxALL, 5);
103 btnEntrySizer->Add(btnClear, 0, wxALL, 5);
104
105 wxStdDialogButtonSizer* btnSizer = new wxStdDialogButtonSizer();
106 wxButton* btnOK = new wxButton(this, wxID_OK);
107 wxButton* btnCancel = new wxButton(this, wxID_CANCEL, _("Cancel"));
108 btnSizer->AddButton(btnOK);
109 btnSizer->AddButton(btnCancel);
110 btnSizer->Realize();
111 mainSizer->Add(btnSizer, 0, wxALL | wxEXPAND, 5);
112
113 // Connect Events
114 btnMoveUp->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
115 wxCommandEventHandler(PriorityDlg::OnMoveUpClick), NULL,
116 this);
117 btnMoveDown->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
118 wxCommandEventHandler(PriorityDlg::OnMoveDownClick),
119 NULL, this);
120
121 btnRefresh->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
122 wxCommandEventHandler(PriorityDlg::OnRefreshClick), NULL,
123 this);
124
125 btnClear->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
126 wxCommandEventHandler(PriorityDlg::OnClearClick), NULL,
127 this);
128
129 m_prioTree->Connect(wxEVT_TREE_SEL_CHANGED,
130 wxCommandEventHandler(PriorityDlg::OnItemSelected), NULL,
131 this);
132
133 // Get the current status
134 MyApp& app = wxGetApp();
135 m_map = app.m_comm_bridge.GetPriorityMaps();
136
137 Populate();
138
139 int n_lines = wxMax(m_prioTree->GetCount(), 15);
140
141 wxScreenDC dc;
142 int char_width, char_height;
143 dc.GetTextExtent("W", &char_width, &char_height, NULL, NULL, m_pF);
144
145 int stcw = wxMax(m_maxStringLength * 15 / 10, 15 * char_width);
146 wxWindow* top_frame = wxTheApp->GetTopWindow();
147 wxSize min_size = wxSize(
148 stcw, wxMin(top_frame->GetSize().y * 2 / 4, n_lines * GetCharHeight()));
149
150 stcSizer->SetMinSize(min_size);
151
152 Layout();
153#ifdef __ANDROID__
154 wxSize max_size =
155 wxSize(top_frame->GetSize().x, top_frame->GetSize().y * 7 / 10);
156 SetSize(max_size);
157 Centre();
158#else
159 Fit();
160 Centre();
161#endif
162
163#ifdef __ANDROID__
164 androidDisableRotation();
165#endif
166}
167
168PriorityDlg::~PriorityDlg() {
169#ifdef __ANDROID__
170 androidEnableRotation();
171#endif
172}
173
174void PriorityDlg::AddLeaves(const std::vector<std::string>& map_list,
175 size_t map_index, std::string map_name,
176 wxTreeItemId leaf_parent) {
177 if (map_list.size() < (size_t)map_index) return;
178
179 // Get the current Priority container for this branch
180 MyApp& app = wxGetApp();
181 PriorityContainer pc = app.m_comm_bridge.GetPriorityContainer(map_name);
182
183 wxString priority_string(map_list[map_index].c_str());
184 wxStringTokenizer tk(priority_string, "|");
185 size_t index = 0;
186 while (tk.HasMoreTokens()) {
187 wxString item_string = tk.GetNextToken();
188
189 wxScreenDC dc;
190 int char_width, char_height;
191 dc.GetTextExtent(item_string, &char_width, &char_height, NULL, NULL, m_pF);
192
193 // Record the maximum display string length, for use in dialog sizing.
194 if (char_width > m_maxStringLength) {
195 m_maxStringLength = char_width;
196 m_max_string = item_string;
197 }
198
199 PriorityEntry* pe = new PriorityEntry(map_index, index);
200 wxTreeItemId id_tk =
201 m_prioTree->AppendItem(leaf_parent, item_string, -1, -1, pe);
202
203 // Set bold text on item currently active (usually 0)
204 if ((size_t)(pc.active_priority) == index) m_prioTree->SetItemBold(id_tk);
205
206 index++;
207 }
208}
209
210void PriorityDlg::Populate() {
211 m_prioTree->Unselect();
212 m_prioTree->DeleteAllItems();
213 m_maxStringLength = 15; // default width calculation
214
215 // wxTreeItemId* rootData = new wxDirItemData(_T("Dummy"), _T("Dummy"),
216 // TRUE);
217 wxTreeItemId m_rootId = m_prioTree->AddRoot(_("Priorities"), -1, -1, NULL);
218 m_prioTree->SetItemHasChildren(m_rootId);
219
220 wxTreeItemId id_position =
221 m_prioTree->AppendItem(m_rootId, _("Position"), -1, -1, NULL);
222 m_prioTree->SetItemHasChildren(id_position);
223 AddLeaves(m_map, 0, "position", id_position);
224
225 wxTreeItemId id_velocity =
226 m_prioTree->AppendItem(m_rootId, _("Speed/Course"), -1, -1, NULL);
227 m_prioTree->SetItemHasChildren(id_velocity);
228 AddLeaves(m_map, 1, "velocity", id_velocity);
229
230 wxTreeItemId id_heading =
231 m_prioTree->AppendItem(m_rootId, _("Heading"), -1, -1, NULL);
232 m_prioTree->SetItemHasChildren(id_heading);
233 AddLeaves(m_map, 2, "heading", id_heading);
234
235 wxTreeItemId id_magvar =
236 m_prioTree->AppendItem(m_rootId, _("Mag Variation"), -1, -1, NULL);
237 m_prioTree->SetItemHasChildren(id_magvar);
238 AddLeaves(m_map, 3, "variation", id_magvar);
239
240 wxTreeItemId id_sats =
241 m_prioTree->AppendItem(m_rootId, _("Satellites"), -1, -1, NULL);
242 m_prioTree->SetItemHasChildren(id_sats);
243 AddLeaves(m_map, 4, "satellites", id_sats);
244
245 m_prioTree->ExpandAll();
246
247 // Restore selection
248 wxTreeItemId rootID = m_prioTree->GetRootItem();
249 wxTreeItemIdValue cookie;
250 int i = m_selmap_index;
251 wxTreeItemId cid = m_prioTree->GetFirstChild(rootID, cookie);
252
253 while ((i > 0) && cid.IsOk()) {
254 cid = m_prioTree->GetNextChild(rootID, cookie);
255 i--;
256 }
257
258 wxTreeItemId ccid = m_prioTree->GetFirstChild(cid, cookie);
259
260 int j = m_selIndex;
261 while ((j > 0) && ccid.IsOk()) {
262 ccid = m_prioTree->GetNextChild(cid, cookie);
263 j--;
264 }
265
266 if (ccid.IsOk()) m_prioTree->SelectItem(ccid);
267}
268
269void PriorityDlg::OnItemSelected(wxCommandEvent& event) {
270 btnMoveUp->Disable();
271 btnMoveDown->Disable();
272
273 wxTreeItemId id = m_prioTree->GetSelection();
274 PriorityEntry* pe = (PriorityEntry*)m_prioTree->GetItemData(id);
275 if (!pe) return;
276
277 m_selIndex = pe->m_index;
278 m_selmap_index = pe->m_category;
279
280 if (pe->m_index > 0) {
281 btnMoveUp->Enable();
282 }
283
284 wxTreeItemId id_parent = m_prioTree->GetItemParent(id);
285
286 // count siblings
287 int n_sibs = 0;
288 wxTreeItemIdValue cookie;
289 wxTreeItemId ch = m_prioTree->GetFirstChild(id_parent, cookie);
290 while (ch.IsOk()) {
291 n_sibs++;
292 ch = m_prioTree->GetNextChild(id_parent, cookie);
293 }
294
295 if (pe->m_index < n_sibs - 1) btnMoveDown->Enable();
296}
297
298void PriorityDlg::OnMoveUpClick(wxCommandEvent& event) {
299 ProcessMove(m_prioTree->GetSelection(), -1);
300}
301
302void PriorityDlg::OnMoveDownClick(wxCommandEvent& event) {
303 ProcessMove(m_prioTree->GetSelection(), 1);
304}
305
306void PriorityDlg::ProcessMove(wxTreeItemId id, int dir) {
307 // Get the extra data
308 PriorityEntry* pe = (PriorityEntry*)m_prioTree->GetItemData(id);
309 if (!pe) return;
310 if (pe->m_category > 4) return;
311
312 // Get the selected category string from the map
313 wxString priority_string = wxString(m_map[pe->m_category].c_str());
314
315 // Build an array
316 wxString prio_array[16]; // enough, plus
317
318 wxStringTokenizer tk(priority_string, "|");
319 int index = 0;
320 while (tk.HasMoreTokens() && index < 16) {
321 prio_array[index] = tk.GetNextToken();
322 index++;
323 }
324 int max_index = index;
325
326 // perform the action
327 if (dir == -1) { // Move UP
328 if (pe->m_index > 0) {
329 // swap entries in array
330 wxString s_above = prio_array[pe->m_index - 1];
331 wxString s_move = prio_array[pe->m_index];
332 prio_array[pe->m_index - 1] = s_move;
333 prio_array[pe->m_index] = s_above;
334 m_selIndex--;
335 }
336 } else { // Move DOWN
337 if (pe->m_index < max_index) {
338 // swap entries in array
339 wxString s_below = prio_array[pe->m_index + 1];
340 wxString s_move = prio_array[pe->m_index];
341 prio_array[pe->m_index + 1] = s_move;
342 prio_array[pe->m_index] = s_below;
343 m_selIndex++;
344 }
345 }
346
347 // create the new string
348 wxString prio_mod;
349 for (int i = 0; i < 16; i++) {
350 if (prio_array[i].Length()) {
351 prio_mod += prio_array[i];
352 prio_mod += wxString("|");
353 }
354 }
355
356 // update the string in the map
357 std::string s_upd(prio_mod.c_str());
358 m_map[pe->m_category] = s_upd;
359
360 // Auto-adjust Sat and COG/SOG priorities if POS has been moved up/down
361 if (pe->m_category == 0) {
362 AdjustSatPriority();
363 AdjustCOGSOGPriority();
364 }
365
366 // Update the priority mechanism
367 MyApp& app = wxGetApp();
368 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
369
370 // And reload the tree GUI
371 m_map = app.m_comm_bridge.GetPriorityMaps();
372 Populate();
373}
374
375void PriorityDlg::OnRefreshClick(wxCommandEvent& event) {
376 // Reload the tree GUI
377 MyApp& app = wxGetApp();
378 m_map = app.m_comm_bridge.GetPriorityMaps();
379 Populate();
380}
381
382void PriorityDlg::OnClearClick(wxCommandEvent& event) {
383 m_map[0].clear();
384 m_map[1].clear();
385 m_map[2].clear();
386 m_map[3].clear();
387 m_map[4].clear();
388
389 m_selmap_index = m_selIndex = 0;
390
391 // Update the priority mechanism
392 MyApp& app = wxGetApp();
393 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
394
395 // And reload the tree GUI
396 m_map = app.m_comm_bridge.GetPriorityMaps();
397 Populate();
398}
399
400void PriorityDlg::AdjustSatPriority() {
401 // Get an array of available sat sources
402 std::string sat_prio = m_map[4];
403 wxArrayString sat_sources;
404 wxString sat_priority_string(sat_prio.c_str());
405 wxStringTokenizer tks(sat_priority_string, "|");
406 while (tks.HasMoreTokens()) {
407 wxString item_string = tks.GetNextToken();
408 sat_sources.Add(item_string);
409 }
410
411 // Step thru the POS priority map
412 std::string pos_prio = m_map[0];
413 wxString pos_priority_string(pos_prio.c_str());
414 wxStringTokenizer tk(pos_priority_string, "|");
415 wxArrayString new_sat_prio;
416 while (tk.HasMoreTokens()) {
417 wxString item_string = tk.GetNextToken();
418 wxString pos_channel = item_string.BeforeFirst(';');
419
420 // search the sat sources array for a match
421 // if found, add to proposed new priority array
422 for (size_t i = 0; i < sat_sources.GetCount(); i++) {
423 if (pos_channel.IsSameAs(sat_sources[i].BeforeFirst(';'))) {
424 new_sat_prio.Add(sat_sources[i]);
425 // Mark this source as "used"
426 sat_sources[i] = "USED";
427 break;
428 } else { // no match, what to do? //FIXME (dave)
429 int yyp = 4;
430 }
431 }
432 }
433 // Create a new sat priority string from new_sat_prio array
434 wxString proposed_sat_prio;
435 for (size_t i = 0; i < new_sat_prio.GetCount(); i++) {
436 proposed_sat_prio += new_sat_prio[i];
437 proposed_sat_prio += wxString("|");
438 }
439
440 // Update the maps with the new sat priority string
441 m_map[4] = proposed_sat_prio.ToStdString();
442}
443
444void PriorityDlg::AdjustCOGSOGPriority() {
445 // Get an array of available COG/SOG sources
446 std::string cogsog_prio = m_map[1];
447 wxArrayString cogsog_sources;
448 wxString cogsog_priority_string(cogsog_prio.c_str());
449 wxStringTokenizer tks(cogsog_priority_string, "|");
450 while (tks.HasMoreTokens()) {
451 wxString item_string = tks.GetNextToken();
452 cogsog_sources.Add(item_string);
453 }
454
455 // Step thru the POS priority map
456 std::string pos_prio = m_map[0];
457 wxString pos_priority_string(pos_prio.c_str());
458 wxStringTokenizer tk(pos_priority_string, "|");
459 wxArrayString new_cogsog_prio;
460 while (tk.HasMoreTokens()) {
461 wxString item_string = tk.GetNextToken();
462 wxString pos_channel = item_string.BeforeFirst(';');
463
464 // search the cogsog sources array for a match
465 // if found, add to proposed new priority array
466 for (size_t i = 0; i < cogsog_sources.GetCount(); i++) {
467 if (pos_channel.IsSameAs(cogsog_sources[i].BeforeFirst(';'))) {
468 new_cogsog_prio.Add(cogsog_sources[i]);
469 // Mark this source as "used"
470 cogsog_sources[i] = "USED";
471 break;
472 } else { // no match, what to do? //FIXME (dave)
473 int yyp = 4;
474 }
475 }
476 }
477 // Create a new cog/sog priority string from new_cogsog_prio array
478 wxString proposed_cogsog_prio;
479 for (size_t i = 0; i < new_cogsog_prio.GetCount(); i++) {
480 proposed_cogsog_prio += new_cogsog_prio[i];
481 proposed_cogsog_prio += wxString("|");
482 }
483
484 // Update the maps with the new cog/sog priority string
485 m_map[1] = proposed_cogsog_prio.ToStdString();
486}
PlugIn Object Definition/API.
wxFont * OCPNGetFont(wxString TextElement, int default_size)
Gets a font for UI elements.