36#include <wx/tokenzr.h>
38#ifdef __OCPN__ANDROID__
39#include "androidUTIL.h"
41#include <QtWidgets/QScroller>
44#include "priority_gui.h"
46#include "model/comm_bridge.h"
47#include "ocpn_frame.h"
53 m_category = category, m_index = index;
57 int m_category, m_index;
60PriorityDlg::PriorityDlg(wxWindow* parent)
61 : wxDialog(parent, wxID_ANY, _(
"Adjust Comm Priorities"), wxDefaultPosition,
62 wxSize(480, 420), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
66 wxBoxSizer* mainSizer =
new wxBoxSizer(wxVERTICAL);
69 wxBoxSizer* secondSizer =
new wxBoxSizer(wxHORIZONTAL);
70 mainSizer->Add(secondSizer, 1, wxEXPAND, 5);
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);
76 m_prioTree =
new wxTreeCtrl(
this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
77 stcSizer->Add(m_prioTree, 1, wxALL | wxEXPAND, 5);
80 m_prioTree->SetFont(*pF);
82 m_prioTree->GetHandle()->setStyleSheet(
83 getWideScrollBarsStyleSheet() );
84 QScroller::ungrabGesture(m_prioTree->GetHandle());
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"));
92 btnMoveDown->Disable();
94 btnEntrySizer->Add(btnMoveUp, 0, wxALL, 5);
95 btnEntrySizer->Add(btnMoveDown, 0, wxALL, 5);
97 btnEntrySizer->AddSpacer(15);
99 btnRefresh =
new wxButton(
this, wxID_ANY, _(
"Refresh"));
100 btnClear =
new wxButton(
this, wxID_ANY, _(
"Clear All"));
102 btnEntrySizer->Add(btnRefresh, 0, wxALL, 5);
103 btnEntrySizer->Add(btnClear, 0, wxALL, 5);
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);
111 mainSizer->Add(btnSizer, 0, wxALL | wxEXPAND, 5);
114 btnMoveUp->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
115 wxCommandEventHandler(PriorityDlg::OnMoveUpClick), NULL,
117 btnMoveDown->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
118 wxCommandEventHandler(PriorityDlg::OnMoveDownClick),
121 btnRefresh->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
122 wxCommandEventHandler(PriorityDlg::OnRefreshClick), NULL,
125 btnClear->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
126 wxCommandEventHandler(PriorityDlg::OnClearClick), NULL,
129 m_prioTree->Connect(wxEVT_TREE_SEL_CHANGED,
130 wxCommandEventHandler(PriorityDlg::OnItemSelected), NULL,
134 MyApp& app = wxGetApp();
135 m_map = app.m_comm_bridge.GetPriorityMaps();
139 int n_lines = wxMax(m_prioTree->GetCount(), 15);
142 int char_width, char_height;
143 dc.GetTextExtent(
"W", &char_width, &char_height, NULL, NULL, m_pF);
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()));
150 stcSizer->SetMinSize(min_size);
155 wxSize(top_frame->GetSize().x, top_frame->GetSize().y * 7 / 10);
164 androidDisableRotation();
168PriorityDlg::~PriorityDlg() {
170 androidEnableRotation();
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;
180 MyApp& app = wxGetApp();
183 wxString priority_string(map_list[map_index].c_str());
184 wxStringTokenizer tk(priority_string,
"|");
186 while (tk.HasMoreTokens()) {
187 wxString item_string = tk.GetNextToken();
190 int char_width, char_height;
191 dc.GetTextExtent(item_string, &char_width, &char_height, NULL, NULL, m_pF);
194 if (char_width > m_maxStringLength) {
195 m_maxStringLength = char_width;
196 m_max_string = item_string;
201 m_prioTree->AppendItem(leaf_parent, item_string, -1, -1, pe);
204 if ((
size_t)(pc.active_priority) == index) m_prioTree->SetItemBold(id_tk);
210void PriorityDlg::Populate() {
211 m_prioTree->Unselect();
212 m_prioTree->DeleteAllItems();
213 m_maxStringLength = 15;
217 wxTreeItemId m_rootId = m_prioTree->AddRoot(_(
"Priorities"), -1, -1, NULL);
218 m_prioTree->SetItemHasChildren(m_rootId);
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);
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);
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);
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);
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);
245 m_prioTree->ExpandAll();
248 wxTreeItemId rootID = m_prioTree->GetRootItem();
249 wxTreeItemIdValue cookie;
250 int i = m_selmap_index;
251 wxTreeItemId cid = m_prioTree->GetFirstChild(rootID, cookie);
253 while ((i > 0) && cid.IsOk()) {
254 cid = m_prioTree->GetNextChild(rootID, cookie);
258 wxTreeItemId ccid = m_prioTree->GetFirstChild(cid, cookie);
261 while ((j > 0) && ccid.IsOk()) {
262 ccid = m_prioTree->GetNextChild(cid, cookie);
266 if (ccid.IsOk()) m_prioTree->SelectItem(ccid);
269void PriorityDlg::OnItemSelected(wxCommandEvent& event) {
270 btnMoveUp->Disable();
271 btnMoveDown->Disable();
273 wxTreeItemId
id = m_prioTree->GetSelection();
277 m_selIndex = pe->m_index;
278 m_selmap_index = pe->m_category;
280 if (pe->m_index > 0) {
284 wxTreeItemId id_parent = m_prioTree->GetItemParent(
id);
288 wxTreeItemIdValue cookie;
289 wxTreeItemId ch = m_prioTree->GetFirstChild(id_parent, cookie);
292 ch = m_prioTree->GetNextChild(id_parent, cookie);
295 if (pe->m_index < n_sibs - 1) btnMoveDown->Enable();
298void PriorityDlg::OnMoveUpClick(wxCommandEvent& event) {
299 ProcessMove(m_prioTree->GetSelection(), -1);
302void PriorityDlg::OnMoveDownClick(wxCommandEvent& event) {
303 ProcessMove(m_prioTree->GetSelection(), 1);
306void PriorityDlg::ProcessMove(wxTreeItemId
id,
int dir) {
310 if (pe->m_category > 4)
return;
313 wxString priority_string = wxString(m_map[pe->m_category].c_str());
316 wxString prio_array[16];
318 wxStringTokenizer tk(priority_string,
"|");
320 while (tk.HasMoreTokens() && index < 16) {
321 prio_array[index] = tk.GetNextToken();
324 int max_index = index;
328 if (pe->m_index > 0) {
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;
337 if (pe->m_index < max_index) {
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;
349 for (
int i = 0; i < 16; i++) {
350 if (prio_array[i].Length()) {
351 prio_mod += prio_array[i];
352 prio_mod += wxString(
"|");
357 std::string s_upd(prio_mod.c_str());
358 m_map[pe->m_category] = s_upd;
361 if (pe->m_category == 0) {
363 AdjustCOGSOGPriority();
367 MyApp& app = wxGetApp();
368 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
371 m_map = app.m_comm_bridge.GetPriorityMaps();
375void PriorityDlg::OnRefreshClick(wxCommandEvent& event) {
377 MyApp& app = wxGetApp();
378 m_map = app.m_comm_bridge.GetPriorityMaps();
382void PriorityDlg::OnClearClick(wxCommandEvent& event) {
389 m_selmap_index = m_selIndex = 0;
392 MyApp& app = wxGetApp();
393 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
396 m_map = app.m_comm_bridge.GetPriorityMaps();
400void PriorityDlg::AdjustSatPriority() {
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);
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(
';');
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]);
426 sat_sources[i] =
"USED";
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(
"|");
441 m_map[4] = proposed_sat_prio.ToStdString();
444void PriorityDlg::AdjustCOGSOGPriority() {
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);
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(
';');
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]);
470 cogsog_sources[i] =
"USED";
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(
"|");
485 m_map[1] = proposed_cogsog_prio.ToStdString();
PlugIn Object Definition/API.
wxFont * OCPNGetFont(wxString TextElement, int default_size)
Gets a font for UI elements.