28#include <QtWidgets/QScroller>
29#include "androidUTIL.h"
40#include <wx/dcscreen.h>
42#include <wx/tokenzr.h>
52 m_category = category, m_index = index;
56 int m_category, m_index;
59PriorityDlg::PriorityDlg(wxWindow* parent)
60 : wxDialog(parent, wxID_ANY, _(
"Adjust Comm Priorities"), wxDefaultPosition,
61 wxSize(480, 420), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
64 SetMinSize(wxSize(480, 420));
65 auto top_frame_size = wxTheApp->GetTopWindow()->GetSize();
67 wxSize(wxMax(top_frame_size.x, 480), wxMax(top_frame_size.y, 420)));
69 wxBoxSizer* mainSizer =
new wxBoxSizer(wxVERTICAL);
72 wxBoxSizer* secondSizer =
new wxBoxSizer(wxHORIZONTAL);
73 mainSizer->Add(secondSizer, 1, wxEXPAND, 5);
75 wxStaticBox* pclbBox =
new wxStaticBox(
this, wxID_ANY, _(
"Priority List"));
76 wxStaticBoxSizer* stcSizer =
new wxStaticBoxSizer(pclbBox, wxVERTICAL);
77 secondSizer->Add(stcSizer, 1, wxALL | wxEXPAND, 5);
79 m_prioTree =
new wxTreeCtrl(
this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
80 stcSizer->Add(m_prioTree, 1, wxALL | wxEXPAND, 5);
83 m_prioTree->SetFont(*pF);
85 m_prioTree->GetHandle()->setStyleSheet(
86 getWideScrollBarsStyleSheet() );
87 QScroller::ungrabGesture(m_prioTree->GetHandle());
90 wxBoxSizer* btnEntrySizer =
new wxBoxSizer(wxVERTICAL);
91 secondSizer->Add(btnEntrySizer, 0, wxALL | wxEXPAND, 5);
92 btnMoveUp =
new wxButton(
this, wxID_ANY, _(
"Move Up"));
93 btnMoveDown =
new wxButton(
this, wxID_ANY, _(
"Move Down"));
95 btnMoveDown->Disable();
97 btnEntrySizer->Add(btnMoveUp, 0, wxALL, 5);
98 btnEntrySizer->Add(btnMoveDown, 0, wxALL, 5);
100 btnEntrySizer->AddSpacer(15);
102 btnRefresh =
new wxButton(
this, wxID_ANY, _(
"Refresh"));
103 btnClear =
new wxButton(
this, wxID_ANY, _(
"Clear All"));
105 btnEntrySizer->Add(btnRefresh, 0, wxALL, 5);
106 btnEntrySizer->Add(btnClear, 0, wxALL, 5);
108 wxStdDialogButtonSizer* btnSizer =
new wxStdDialogButtonSizer();
109 wxButton* btnOK =
new wxButton(
this, wxID_OK);
110 wxButton* btnCancel =
new wxButton(
this, wxID_CANCEL, _(
"Cancel"));
111 btnSizer->AddButton(btnOK);
112 btnSizer->AddButton(btnCancel);
114 mainSizer->Add(btnSizer, 0, wxALL | wxEXPAND, 5);
117 btnMoveUp->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
118 wxCommandEventHandler(PriorityDlg::OnMoveUpClick), NULL,
120 btnMoveDown->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
121 wxCommandEventHandler(PriorityDlg::OnMoveDownClick),
124 btnRefresh->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
125 wxCommandEventHandler(PriorityDlg::OnRefreshClick), NULL,
128 btnClear->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
129 wxCommandEventHandler(PriorityDlg::OnClearClick), NULL,
132 m_prioTree->Connect(wxEVT_TREE_SEL_CHANGED,
133 wxCommandEventHandler(PriorityDlg::OnItemSelected), NULL,
137 m_map = CommBridge::GetInstance().GetPriorityMaps();
141 int n_lines = wxMax(m_prioTree->GetCount(), 15);
144 int char_width, char_height;
145 dc.GetTextExtent(
"W", &char_width, &char_height, NULL, NULL, m_pF);
147 int stcw = wxMax(m_maxStringLength * 15 / 10, 15 * char_width);
148 wxWindow* top_frame = wxTheApp->GetTopWindow();
149 wxSize min_size = wxSize(
150 stcw, wxMin(top_frame->GetSize().y * 2 / 4, n_lines * GetCharHeight()));
152 stcSizer->SetMinSize(min_size);
157 wxSize(top_frame->GetSize().x, top_frame->GetSize().y * 7 / 10);
166 androidDisableRotation();
170PriorityDlg::~PriorityDlg() {
172 androidEnableRotation();
176void PriorityDlg::AddLeaves(
const std::vector<std::string>& map_list,
177 size_t map_index, std::string map_name,
178 wxTreeItemId leaf_parent) {
179 if (map_list.size() < (
size_t)map_index)
return;
183 CommBridge::GetInstance().GetPriorityContainer(map_name);
185 wxString priority_string(map_list[map_index].c_str());
186 wxStringTokenizer tk(priority_string,
"|");
188 while (tk.HasMoreTokens()) {
189 wxString item_string = tk.GetNextToken();
192 int char_width, char_height;
193 dc.GetTextExtent(item_string, &char_width, &char_height, NULL, NULL, m_pF);
196 if (char_width > m_maxStringLength) {
197 m_maxStringLength = char_width;
198 m_max_string = item_string;
203 m_prioTree->AppendItem(leaf_parent, item_string, -1, -1, pe);
206 if ((
size_t)(pc.active_priority) == index) m_prioTree->SetItemBold(id_tk);
212void PriorityDlg::Populate() {
213 m_prioTree->Unselect();
214 m_prioTree->DeleteAllItems();
215 m_maxStringLength = 15;
219 wxTreeItemId m_rootId = m_prioTree->AddRoot(_(
"Priorities"), -1, -1, NULL);
220 m_prioTree->SetItemHasChildren(m_rootId);
222 wxTreeItemId id_position =
223 m_prioTree->AppendItem(m_rootId, _(
"Position"), -1, -1, NULL);
224 m_prioTree->SetItemHasChildren(id_position);
225 AddLeaves(m_map, 0,
"position", id_position);
227 wxTreeItemId id_velocity =
228 m_prioTree->AppendItem(m_rootId, _(
"Speed/Course"), -1, -1, NULL);
229 m_prioTree->SetItemHasChildren(id_velocity);
230 AddLeaves(m_map, 1,
"velocity", id_velocity);
232 wxTreeItemId id_heading =
233 m_prioTree->AppendItem(m_rootId, _(
"Heading"), -1, -1, NULL);
234 m_prioTree->SetItemHasChildren(id_heading);
235 AddLeaves(m_map, 2,
"heading", id_heading);
237 wxTreeItemId id_magvar =
238 m_prioTree->AppendItem(m_rootId, _(
"Mag Variation"), -1, -1, NULL);
239 m_prioTree->SetItemHasChildren(id_magvar);
240 AddLeaves(m_map, 3,
"variation", id_magvar);
242 wxTreeItemId id_sats =
243 m_prioTree->AppendItem(m_rootId, _(
"Satellites"), -1, -1, NULL);
244 m_prioTree->SetItemHasChildren(id_sats);
245 AddLeaves(m_map, 4,
"satellites", id_sats);
247 m_prioTree->ExpandAll();
250 wxTreeItemId rootID = m_prioTree->GetRootItem();
251 wxTreeItemIdValue cookie;
252 int i = m_selmap_index;
253 wxTreeItemId cid = m_prioTree->GetFirstChild(rootID, cookie);
255 while ((i > 0) && cid.IsOk()) {
256 cid = m_prioTree->GetNextChild(rootID, cookie);
260 wxTreeItemId ccid = m_prioTree->GetFirstChild(cid, cookie);
263 while ((j > 0) && ccid.IsOk()) {
264 ccid = m_prioTree->GetNextChild(cid, cookie);
268 if (ccid.IsOk()) m_prioTree->SelectItem(ccid);
271void PriorityDlg::OnItemSelected(wxCommandEvent& event) {
272 btnMoveUp->Disable();
273 btnMoveDown->Disable();
275 wxTreeItemId
id = m_prioTree->GetSelection();
279 m_selIndex = pe->m_index;
280 m_selmap_index = pe->m_category;
282 if (pe->m_index > 0) {
286 wxTreeItemId id_parent = m_prioTree->GetItemParent(
id);
290 wxTreeItemIdValue cookie;
291 wxTreeItemId ch = m_prioTree->GetFirstChild(id_parent, cookie);
294 ch = m_prioTree->GetNextChild(id_parent, cookie);
297 if (pe->m_index < n_sibs - 1) btnMoveDown->Enable();
300void PriorityDlg::OnMoveUpClick(wxCommandEvent& event) {
301 ProcessMove(m_prioTree->GetSelection(), -1);
304void PriorityDlg::OnMoveDownClick(wxCommandEvent& event) {
305 ProcessMove(m_prioTree->GetSelection(), 1);
308void PriorityDlg::ProcessMove(wxTreeItemId
id,
int dir) {
312 if (pe->m_category > 4)
return;
315 wxString priority_string = wxString(m_map[pe->m_category].c_str());
318 wxString prio_array[16];
320 wxStringTokenizer tk(priority_string,
"|");
322 while (tk.HasMoreTokens() && index < 16) {
323 prio_array[index] = tk.GetNextToken();
326 int max_index = index;
330 if (pe->m_index > 0) {
332 wxString s_above = prio_array[pe->m_index - 1];
333 wxString s_move = prio_array[pe->m_index];
334 prio_array[pe->m_index - 1] = s_move;
335 prio_array[pe->m_index] = s_above;
339 if (pe->m_index < max_index) {
341 wxString s_below = prio_array[pe->m_index + 1];
342 wxString s_move = prio_array[pe->m_index];
343 prio_array[pe->m_index + 1] = s_move;
344 prio_array[pe->m_index] = s_below;
351 for (
int i = 0; i < 16; i++) {
352 if (prio_array[i].Length()) {
353 prio_mod += prio_array[i];
354 prio_mod += wxString(
"|");
359 std::string s_upd(prio_mod.c_str());
360 m_map[pe->m_category] = s_upd;
363 if (pe->m_category == 0) {
365 AdjustCOGSOGPriority();
369 CommBridge::GetInstance().UpdateAndApplyMaps(m_map);
372 m_map = CommBridge::GetInstance().GetPriorityMaps();
376void PriorityDlg::OnRefreshClick(wxCommandEvent& event) {
378 m_map = CommBridge::GetInstance().GetPriorityMaps();
382void PriorityDlg::OnClearClick(wxCommandEvent& event) {
389 m_selmap_index = m_selIndex = 0;
392 CommBridge::GetInstance().UpdateAndApplyMaps(m_map);
395 m_map = CommBridge::GetInstance().GetPriorityMaps();
399void PriorityDlg::AdjustSatPriority() {
401 std::string sat_prio = m_map[4];
402 wxArrayString sat_sources;
403 wxString sat_priority_string(sat_prio.c_str());
404 wxStringTokenizer tks(sat_priority_string,
"|");
405 while (tks.HasMoreTokens()) {
406 wxString item_string = tks.GetNextToken();
407 sat_sources.Add(item_string);
411 std::string pos_prio = m_map[0];
412 wxString pos_priority_string(pos_prio.c_str());
413 wxStringTokenizer tk(pos_priority_string,
"|");
414 wxArrayString new_sat_prio;
415 while (tk.HasMoreTokens()) {
416 wxString item_string = tk.GetNextToken();
417 wxString pos_channel = item_string.BeforeFirst(
';');
421 for (
size_t i = 0; i < sat_sources.GetCount(); i++) {
422 if (pos_channel.IsSameAs(sat_sources[i].BeforeFirst(
';'))) {
423 new_sat_prio.Add(sat_sources[i]);
425 sat_sources[i] =
"USED";
433 wxString proposed_sat_prio;
434 for (
size_t i = 0; i < new_sat_prio.GetCount(); i++) {
435 proposed_sat_prio += new_sat_prio[i];
436 proposed_sat_prio += wxString(
"|");
440 m_map[4] = proposed_sat_prio.ToStdString();
443void PriorityDlg::AdjustCOGSOGPriority() {
445 std::string cogsog_prio = m_map[1];
446 wxArrayString cogsog_sources;
447 wxString cogsog_priority_string(cogsog_prio.c_str());
448 wxStringTokenizer tks(cogsog_priority_string,
"|");
449 while (tks.HasMoreTokens()) {
450 wxString item_string = tks.GetNextToken();
451 cogsog_sources.Add(item_string);
455 std::string pos_prio = m_map[0];
456 wxString pos_priority_string(pos_prio.c_str());
457 wxStringTokenizer tk(pos_priority_string,
"|");
458 wxArrayString new_cogsog_prio;
459 while (tk.HasMoreTokens()) {
460 wxString item_string = tk.GetNextToken();
461 wxString pos_channel = item_string.BeforeFirst(
';');
465 for (
size_t i = 0; i < cogsog_sources.GetCount(); i++) {
466 if (pos_channel.IsSameAs(cogsog_sources[i].BeforeFirst(
';'))) {
467 new_cogsog_prio.Add(cogsog_sources[i]);
469 cogsog_sources[i] =
"USED";
477 wxString proposed_cogsog_prio;
478 for (
size_t i = 0; i < new_cogsog_prio.GetCount(); i++) {
479 proposed_cogsog_prio += new_cogsog_prio[i];
480 proposed_cogsog_prio += wxString(
"|");
484 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.