28#include <QtWidgets/QScroller>
29#include "androidUTIL.h"
40#include <wx/dcscreen.h>
42#include <wx/tokenzr.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) {
65 SetMinSize(wxSize(480, 420));
66 auto top_frame_size = wxTheApp->GetTopWindow()->GetSize();
68 wxSize(wxMax(top_frame_size.x, 480), wxMax(top_frame_size.y, 420)));
70 wxBoxSizer* mainSizer =
new wxBoxSizer(wxVERTICAL);
73 wxBoxSizer* secondSizer =
new wxBoxSizer(wxHORIZONTAL);
74 mainSizer->Add(secondSizer, 1, wxEXPAND, 5);
76 wxStaticBox* pclbBox =
new wxStaticBox(
this, wxID_ANY, _(
"Priority List"));
77 wxStaticBoxSizer* stcSizer =
new wxStaticBoxSizer(pclbBox, wxVERTICAL);
78 secondSizer->Add(stcSizer, 1, wxALL | wxEXPAND, 5);
80 m_prioTree =
new wxTreeCtrl(
this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
81 stcSizer->Add(m_prioTree, 1, wxALL | wxEXPAND, 5);
84 m_prioTree->SetFont(*pF);
86 m_prioTree->GetHandle()->setStyleSheet(
87 getWideScrollBarsStyleSheet() );
88 QScroller::ungrabGesture(m_prioTree->GetHandle());
91 wxBoxSizer* btnEntrySizer =
new wxBoxSizer(wxVERTICAL);
92 secondSizer->Add(btnEntrySizer, 0, wxALL | wxEXPAND, 5);
93 btnMoveUp =
new wxButton(
this, wxID_ANY, _(
"Move Up"));
94 btnMoveDown =
new wxButton(
this, wxID_ANY, _(
"Move Down"));
96 btnMoveDown->Disable();
98 btnEntrySizer->Add(btnMoveUp, 0, wxALL, 5);
99 btnEntrySizer->Add(btnMoveDown, 0, wxALL, 5);
101 btnEntrySizer->AddSpacer(15);
103 btnRefresh =
new wxButton(
this, wxID_ANY, _(
"Refresh"));
104 btnClear =
new wxButton(
this, wxID_ANY, _(
"Clear All"));
106 btnEntrySizer->Add(btnRefresh, 0, wxALL, 5);
107 btnEntrySizer->Add(btnClear, 0, wxALL, 5);
109 wxStdDialogButtonSizer* btnSizer =
new wxStdDialogButtonSizer();
110 wxButton* btnOK =
new wxButton(
this, wxID_OK);
111 wxButton* btnCancel =
new wxButton(
this, wxID_CANCEL, _(
"Cancel"));
112 btnSizer->AddButton(btnOK);
113 btnSizer->AddButton(btnCancel);
115 mainSizer->Add(btnSizer, 0, wxALL | wxEXPAND, 5);
118 btnMoveUp->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
119 wxCommandEventHandler(PriorityDlg::OnMoveUpClick), NULL,
121 btnMoveDown->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
122 wxCommandEventHandler(PriorityDlg::OnMoveDownClick),
125 btnRefresh->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
126 wxCommandEventHandler(PriorityDlg::OnRefreshClick), NULL,
129 btnClear->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
130 wxCommandEventHandler(PriorityDlg::OnClearClick), NULL,
133 m_prioTree->Connect(wxEVT_TREE_SEL_CHANGED,
134 wxCommandEventHandler(PriorityDlg::OnItemSelected), NULL,
138 m_map = CommBridge::GetInstance().GetPriorityMaps();
142 int n_lines = wxMax(m_prioTree->GetCount(), 15);
145 int char_width, char_height;
146 dc.GetTextExtent(
"W", &char_width, &char_height, NULL, NULL, m_pF);
148 int stcw = wxMax(m_maxStringLength * 15 / 10, 15 * char_width);
149 wxWindow* top_frame = wxTheApp->GetTopWindow();
150 wxSize min_size = wxSize(
151 stcw, wxMin(top_frame->GetSize().y * 2 / 4, n_lines * GetCharHeight()));
153 stcSizer->SetMinSize(min_size);
158 wxSize(top_frame->GetSize().x, top_frame->GetSize().y * 7 / 10);
167 androidDisableRotation();
171PriorityDlg::~PriorityDlg() {
173 androidEnableRotation();
177void PriorityDlg::AddLeaves(
const std::vector<std::string>& map_list,
178 size_t map_index, std::string map_name,
179 wxTreeItemId leaf_parent) {
180 if (map_list.size() < (
size_t)map_index)
return;
184 CommBridge::GetInstance().GetPriorityContainer(map_name);
186 wxString priority_string(map_list[map_index].c_str());
187 wxStringTokenizer tk(priority_string,
"|");
189 while (tk.HasMoreTokens()) {
190 wxString item_string = tk.GetNextToken();
193 int char_width, char_height;
194 dc.GetTextExtent(item_string, &char_width, &char_height, NULL, NULL, m_pF);
197 if (char_width > m_maxStringLength) {
198 m_maxStringLength = char_width;
199 m_max_string = item_string;
204 m_prioTree->AppendItem(leaf_parent, item_string, -1, -1, pe);
207 if ((
size_t)(pc.active_priority) == index) m_prioTree->SetItemBold(id_tk);
213void PriorityDlg::Populate() {
214 m_prioTree->Unselect();
215 m_prioTree->DeleteAllItems();
216 m_maxStringLength = 15;
220 wxTreeItemId m_rootId = m_prioTree->AddRoot(_(
"Priorities"), -1, -1, NULL);
221 m_prioTree->SetItemHasChildren(m_rootId);
223 wxTreeItemId id_position =
224 m_prioTree->AppendItem(m_rootId, _(
"Position"), -1, -1, NULL);
225 m_prioTree->SetItemHasChildren(id_position);
226 AddLeaves(m_map, 0,
"position", id_position);
228 wxTreeItemId id_velocity =
229 m_prioTree->AppendItem(m_rootId, _(
"Speed/Course"), -1, -1, NULL);
230 m_prioTree->SetItemHasChildren(id_velocity);
231 AddLeaves(m_map, 1,
"velocity", id_velocity);
233 wxTreeItemId id_heading =
234 m_prioTree->AppendItem(m_rootId, _(
"Heading"), -1, -1, NULL);
235 m_prioTree->SetItemHasChildren(id_heading);
236 AddLeaves(m_map, 2,
"heading", id_heading);
238 wxTreeItemId id_magvar =
239 m_prioTree->AppendItem(m_rootId, _(
"Mag Variation"), -1, -1, NULL);
240 m_prioTree->SetItemHasChildren(id_magvar);
241 AddLeaves(m_map, 3,
"variation", id_magvar);
243 wxTreeItemId id_sats =
244 m_prioTree->AppendItem(m_rootId, _(
"Satellites"), -1, -1, NULL);
245 m_prioTree->SetItemHasChildren(id_sats);
246 AddLeaves(m_map, 4,
"satellites", id_sats);
248 m_prioTree->ExpandAll();
251 wxTreeItemId rootID = m_prioTree->GetRootItem();
252 wxTreeItemIdValue cookie;
253 int i = m_selmap_index;
254 wxTreeItemId cid = m_prioTree->GetFirstChild(rootID, cookie);
256 while ((i > 0) && cid.IsOk()) {
257 cid = m_prioTree->GetNextChild(rootID, cookie);
261 wxTreeItemId ccid = m_prioTree->GetFirstChild(cid, cookie);
264 while ((j > 0) && ccid.IsOk()) {
265 ccid = m_prioTree->GetNextChild(cid, cookie);
269 if (ccid.IsOk()) m_prioTree->SelectItem(ccid);
272void PriorityDlg::OnItemSelected(wxCommandEvent& event) {
273 btnMoveUp->Disable();
274 btnMoveDown->Disable();
276 wxTreeItemId
id = m_prioTree->GetSelection();
280 m_selIndex = pe->m_index;
281 m_selmap_index = pe->m_category;
283 if (pe->m_index > 0) {
287 wxTreeItemId id_parent = m_prioTree->GetItemParent(
id);
291 wxTreeItemIdValue cookie;
292 wxTreeItemId ch = m_prioTree->GetFirstChild(id_parent, cookie);
295 ch = m_prioTree->GetNextChild(id_parent, cookie);
298 if (pe->m_index < n_sibs - 1) btnMoveDown->Enable();
301void PriorityDlg::OnMoveUpClick(wxCommandEvent& event) {
302 ProcessMove(m_prioTree->GetSelection(), -1);
305void PriorityDlg::OnMoveDownClick(wxCommandEvent& event) {
306 ProcessMove(m_prioTree->GetSelection(), 1);
309void PriorityDlg::ProcessMove(wxTreeItemId
id,
int dir) {
313 if (pe->m_category > 4)
return;
316 wxString priority_string = wxString(m_map[pe->m_category].c_str());
319 wxString prio_array[16];
321 wxStringTokenizer tk(priority_string,
"|");
323 while (tk.HasMoreTokens() && index < 16) {
324 prio_array[index] = tk.GetNextToken();
327 int max_index = index;
331 if (pe->m_index > 0) {
333 wxString s_above = prio_array[pe->m_index - 1];
334 wxString s_move = prio_array[pe->m_index];
335 prio_array[pe->m_index - 1] = s_move;
336 prio_array[pe->m_index] = s_above;
340 if (pe->m_index < max_index) {
342 wxString s_below = prio_array[pe->m_index + 1];
343 wxString s_move = prio_array[pe->m_index];
344 prio_array[pe->m_index + 1] = s_move;
345 prio_array[pe->m_index] = s_below;
352 for (
int i = 0; i < 16; i++) {
353 if (prio_array[i].Length()) {
354 prio_mod += prio_array[i];
355 prio_mod += wxString(
"|");
360 std::string s_upd(prio_mod.c_str());
361 m_map[pe->m_category] = s_upd;
364 if (pe->m_category == 0) {
366 AdjustCOGSOGPriority();
370 CommBridge::GetInstance().UpdateAndApplyMaps(m_map);
373 m_map = CommBridge::GetInstance().GetPriorityMaps();
377void PriorityDlg::OnRefreshClick(wxCommandEvent& event) {
379 m_map = CommBridge::GetInstance().GetPriorityMaps();
383void PriorityDlg::OnClearClick(wxCommandEvent& event) {
390 m_selmap_index = m_selIndex = 0;
393 CommBridge::GetInstance().UpdateAndApplyMaps(m_map);
396 m_map = CommBridge::GetInstance().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();
CommBridge class and helpers.
PlugIn Object Definition/API.
wxFont * OCPNGetFont(wxString TextElement, int default_size)
Gets a font for UI elements.
Input priorities management dialog.