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);
152 SetMaxSize(top_frame->GetSize());
159 androidDisableRotation();
163PriorityDlg::~PriorityDlg() {
165 androidEnableRotation();
169void PriorityDlg::AddLeaves(
const std::vector<std::string>& map_list,
170 size_t map_index, std::string map_name,
171 wxTreeItemId leaf_parent) {
172 if (map_list.size() < (
size_t)map_index)
return;
175 MyApp& app = wxGetApp();
178 wxString priority_string(map_list[map_index].c_str());
179 wxStringTokenizer tk(priority_string,
"|");
181 while (tk.HasMoreTokens()) {
182 wxString item_string = tk.GetNextToken();
185 int char_width, char_height;
186 dc.GetTextExtent(item_string, &char_width, &char_height, NULL, NULL, m_pF);
189 if (char_width > m_maxStringLength) {
190 m_maxStringLength = char_width;
191 m_max_string = item_string;
196 m_prioTree->AppendItem(leaf_parent, item_string, -1, -1, pe);
199 if ((
size_t)(pc.active_priority) == index) m_prioTree->SetItemBold(id_tk);
205void PriorityDlg::Populate() {
206 m_prioTree->Unselect();
207 m_prioTree->DeleteAllItems();
208 m_maxStringLength = 15;
212 wxTreeItemId m_rootId = m_prioTree->AddRoot(_(
"Priorities"), -1, -1, NULL);
213 m_prioTree->SetItemHasChildren(m_rootId);
215 wxTreeItemId id_position =
216 m_prioTree->AppendItem(m_rootId, _(
"Position"), -1, -1, NULL);
217 m_prioTree->SetItemHasChildren(id_position);
218 AddLeaves(m_map, 0,
"position", id_position);
220 wxTreeItemId id_velocity =
221 m_prioTree->AppendItem(m_rootId, _(
"Speed/Course"), -1, -1, NULL);
222 m_prioTree->SetItemHasChildren(id_velocity);
223 AddLeaves(m_map, 1,
"velocity", id_velocity);
225 wxTreeItemId id_heading =
226 m_prioTree->AppendItem(m_rootId, _(
"Heading"), -1, -1, NULL);
227 m_prioTree->SetItemHasChildren(id_heading);
228 AddLeaves(m_map, 2,
"heading", id_heading);
230 wxTreeItemId id_magvar =
231 m_prioTree->AppendItem(m_rootId, _(
"Mag Variation"), -1, -1, NULL);
232 m_prioTree->SetItemHasChildren(id_magvar);
233 AddLeaves(m_map, 3,
"variation", id_magvar);
235 wxTreeItemId id_sats =
236 m_prioTree->AppendItem(m_rootId, _(
"Satellites"), -1, -1, NULL);
237 m_prioTree->SetItemHasChildren(id_sats);
238 AddLeaves(m_map, 4,
"satellites", id_sats);
240 m_prioTree->ExpandAll();
243 wxTreeItemId rootID = m_prioTree->GetRootItem();
244 wxTreeItemIdValue cookie;
245 int i = m_selmap_index;
246 wxTreeItemId cid = m_prioTree->GetFirstChild(rootID, cookie);
248 while ((i > 0) && cid.IsOk()) {
249 cid = m_prioTree->GetNextChild(rootID, cookie);
253 wxTreeItemId ccid = m_prioTree->GetFirstChild(cid, cookie);
256 while ((j > 0) && ccid.IsOk()) {
257 ccid = m_prioTree->GetNextChild(cid, cookie);
261 if (ccid.IsOk()) m_prioTree->SelectItem(ccid);
264void PriorityDlg::OnItemSelected(wxCommandEvent& event) {
265 btnMoveUp->Disable();
266 btnMoveDown->Disable();
268 wxTreeItemId
id = m_prioTree->GetSelection();
272 m_selIndex = pe->m_index;
273 m_selmap_index = pe->m_category;
275 if (pe->m_index > 0) {
279 wxTreeItemId id_parent = m_prioTree->GetItemParent(
id);
283 wxTreeItemIdValue cookie;
284 wxTreeItemId ch = m_prioTree->GetFirstChild(id_parent, cookie);
287 ch = m_prioTree->GetNextChild(id_parent, cookie);
290 if (pe->m_index < n_sibs - 1) btnMoveDown->Enable();
293void PriorityDlg::OnMoveUpClick(wxCommandEvent& event) {
294 ProcessMove(m_prioTree->GetSelection(), -1);
297void PriorityDlg::OnMoveDownClick(wxCommandEvent& event) {
298 ProcessMove(m_prioTree->GetSelection(), 1);
301void PriorityDlg::ProcessMove(wxTreeItemId
id,
int dir) {
305 if (pe->m_category > 4)
return;
308 wxString priority_string = wxString(m_map[pe->m_category].c_str());
311 wxString prio_array[16];
313 wxStringTokenizer tk(priority_string,
"|");
315 while (tk.HasMoreTokens() && index < 16) {
316 prio_array[index] = tk.GetNextToken();
319 int max_index = index;
323 if (pe->m_index > 0) {
325 wxString s_above = prio_array[pe->m_index - 1];
326 wxString s_move = prio_array[pe->m_index];
327 prio_array[pe->m_index - 1] = s_move;
328 prio_array[pe->m_index] = s_above;
332 if (pe->m_index < max_index) {
334 wxString s_below = prio_array[pe->m_index + 1];
335 wxString s_move = prio_array[pe->m_index];
336 prio_array[pe->m_index + 1] = s_move;
337 prio_array[pe->m_index] = s_below;
344 for (
int i = 0; i < 16; i++) {
345 if (prio_array[i].Length()) {
346 prio_mod += prio_array[i];
347 prio_mod += wxString(
"|");
352 std::string s_upd(prio_mod.c_str());
353 m_map[pe->m_category] = s_upd;
356 if (pe->m_category == 0) {
358 AdjustCOGSOGPriority();
362 MyApp& app = wxGetApp();
363 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
366 m_map = app.m_comm_bridge.GetPriorityMaps();
370void PriorityDlg::OnRefreshClick(wxCommandEvent& event) {
372 MyApp& app = wxGetApp();
373 m_map = app.m_comm_bridge.GetPriorityMaps();
377void PriorityDlg::OnClearClick(wxCommandEvent& event) {
384 m_selmap_index = m_selIndex = 0;
387 MyApp& app = wxGetApp();
388 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
391 m_map = app.m_comm_bridge.GetPriorityMaps();
395void PriorityDlg::AdjustSatPriority() {
397 std::string sat_prio = m_map[4];
398 wxArrayString sat_sources;
399 wxString sat_priority_string(sat_prio.c_str());
400 wxStringTokenizer tks(sat_priority_string,
"|");
401 while (tks.HasMoreTokens()) {
402 wxString item_string = tks.GetNextToken();
403 sat_sources.Add(item_string);
407 std::string pos_prio = m_map[0];
408 wxString pos_priority_string(pos_prio.c_str());
409 wxStringTokenizer tk(pos_priority_string,
"|");
410 wxArrayString new_sat_prio;
411 while (tk.HasMoreTokens()) {
412 wxString item_string = tk.GetNextToken();
413 wxString pos_channel = item_string.BeforeFirst(
';');
417 for (
size_t i = 0; i < sat_sources.GetCount(); i++) {
418 if (pos_channel.IsSameAs(sat_sources[i].BeforeFirst(
';'))) {
419 new_sat_prio.Add(sat_sources[i]);
421 sat_sources[i] =
"USED";
429 wxString proposed_sat_prio;
430 for (
size_t i = 0; i < new_sat_prio.GetCount(); i++) {
431 proposed_sat_prio += new_sat_prio[i];
432 proposed_sat_prio += wxString(
"|");
436 m_map[4] = proposed_sat_prio.ToStdString();
439void PriorityDlg::AdjustCOGSOGPriority() {
441 std::string cogsog_prio = m_map[1];
442 wxArrayString cogsog_sources;
443 wxString cogsog_priority_string(cogsog_prio.c_str());
444 wxStringTokenizer tks(cogsog_priority_string,
"|");
445 while (tks.HasMoreTokens()) {
446 wxString item_string = tks.GetNextToken();
447 cogsog_sources.Add(item_string);
451 std::string pos_prio = m_map[0];
452 wxString pos_priority_string(pos_prio.c_str());
453 wxStringTokenizer tk(pos_priority_string,
"|");
454 wxArrayString new_cogsog_prio;
455 while (tk.HasMoreTokens()) {
456 wxString item_string = tk.GetNextToken();
457 wxString pos_channel = item_string.BeforeFirst(
';');
461 for (
size_t i = 0; i < cogsog_sources.GetCount(); i++) {
462 if (pos_channel.IsSameAs(cogsog_sources[i].BeforeFirst(
';'))) {
463 new_cogsog_prio.Add(cogsog_sources[i]);
465 cogsog_sources[i] =
"USED";
473 wxString proposed_cogsog_prio;
474 for (
size_t i = 0; i < new_cogsog_prio.GetCount(); i++) {
475 proposed_cogsog_prio += new_cogsog_prio[i];
476 proposed_cogsog_prio += wxString(
"|");
480 m_map[1] = proposed_cogsog_prio.ToStdString();
PlugIn Object Definition/API.
wxFont * OCPNGetFont(wxString TextElement, int default_size)
Gets a font for UI elements.