29 #include <wx/wxprec.h>
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"
55 PriorityEntry(
int category,
int index){ m_category = category, m_index=index; }
58 int m_category, m_index;
64 PriorityDlg::PriorityDlg(wxWindow* parent)
65 : wxDialog(parent, wxID_ANY, _(
"Adjust Comm Priorities"), wxDefaultPosition,
66 wxSize(480, 420), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
71 wxBoxSizer* mainSizer =
new wxBoxSizer(wxVERTICAL);
74 wxBoxSizer* secondSizer =
new wxBoxSizer(wxHORIZONTAL);
75 mainSizer->Add(secondSizer, 1, wxEXPAND, 5);
77 wxStaticBox* pclbBox =
new wxStaticBox(
this, wxID_ANY, _(
"Priority List"));
78 wxStaticBoxSizer* stcSizer =
new wxStaticBoxSizer(pclbBox, wxVERTICAL);
79 secondSizer->Add(stcSizer, 1, wxALL | wxEXPAND, 5);
81 m_prioTree =
new wxTreeCtrl(
this,wxID_ANY, wxDefaultPosition,
83 stcSizer->Add(m_prioTree, 1, wxALL | wxEXPAND, 5);
84 wxFont *pF = OCPNGetFont(_T(
"Dialog"), 0);
86 m_prioTree->SetFont(*pF);
88 m_prioTree->GetHandle()->setStyleSheet(getWideScrollBarsStyleSheet());
89 QScroller::ungrabGesture(m_prioTree->GetHandle());
92 wxBoxSizer* btnEntrySizer =
new wxBoxSizer(wxVERTICAL);
93 secondSizer->Add(btnEntrySizer, 0, wxALL | wxEXPAND, 5);
94 btnMoveUp =
new wxButton(
this, wxID_ANY, _(
"Move Up"));
95 btnMoveDown =
new wxButton(
this, wxID_ANY, _(
"Move Down"));
97 btnMoveDown->Disable();
99 btnEntrySizer->Add(btnMoveUp, 0, wxALL, 5);
100 btnEntrySizer->Add(btnMoveDown, 0, wxALL, 5);
102 btnEntrySizer->AddSpacer(15);
104 btnRefresh =
new wxButton(
this, wxID_ANY, _(
"Refresh"));
105 btnClear =
new wxButton(
this, wxID_ANY, _(
"Clear All"));
107 btnEntrySizer->Add(btnRefresh, 0, wxALL, 5);
108 btnEntrySizer->Add(btnClear, 0, wxALL, 5);
110 wxStdDialogButtonSizer* btnSizer =
new wxStdDialogButtonSizer();
111 wxButton* btnOK =
new wxButton(
this, wxID_OK);
112 wxButton* btnCancel =
new wxButton(
this, wxID_CANCEL, _(
"Cancel"));
113 btnSizer->AddButton(btnOK);
114 btnSizer->AddButton(btnCancel);
116 mainSizer->Add(btnSizer, 0, wxALL | wxEXPAND, 5);
119 btnMoveUp->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
120 wxCommandEventHandler(PriorityDlg::OnMoveUpClick), NULL,
122 btnMoveDown->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
123 wxCommandEventHandler(PriorityDlg::OnMoveDownClick), NULL,
126 btnRefresh->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
127 wxCommandEventHandler(PriorityDlg::OnRefreshClick), NULL,
130 btnClear->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
131 wxCommandEventHandler(PriorityDlg::OnClearClick), NULL,
134 m_prioTree->Connect(wxEVT_TREE_SEL_CHANGED,
135 wxCommandEventHandler(PriorityDlg::OnItemSelected),
139 MyApp& app = wxGetApp();
140 m_map = app.m_comm_bridge.GetPriorityMaps();
144 int n_lines = wxMax(m_prioTree->GetCount(), 15);
147 int char_width, char_height;
148 dc.GetTextExtent(
"W", &char_width, &char_height, NULL, NULL, m_pF);
150 int stcw = wxMax(m_maxStringLength * 15 / 10, 15 * char_width);
151 wxSize min_size = wxSize(stcw,
152 wxMin(gFrame->GetSize().y * 2 /4 , n_lines * GetCharHeight()));
154 stcSizer->SetMinSize(min_size);
156 SetMaxSize(gFrame->GetSize());
163 androidDisableRotation();
167 PriorityDlg::~PriorityDlg() {
169 androidEnableRotation();
173 void PriorityDlg::AddLeaves(
const std::vector<std::string> &map_list,
174 size_t map_index, std::string map_name,
175 wxTreeItemId leaf_parent){
176 if(map_list.size() < (
size_t)map_index)
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;
200 wxTreeItemId id_tk = m_prioTree->AppendItem(leaf_parent, item_string, -1, -1, pe);
203 if ( (
size_t)(pc.active_priority) == index)
204 m_prioTree->SetItemBold(id_tk);
211 void PriorityDlg::Populate() {
213 m_prioTree->Unselect();
214 m_prioTree->DeleteAllItems();
215 m_maxStringLength = 15;
218 wxTreeItemId m_rootId = m_prioTree->AddRoot(_(
"Priorities"), -1, -1, NULL);
219 m_prioTree->SetItemHasChildren(m_rootId);
221 wxTreeItemId id_position = 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 = m_prioTree->AppendItem(m_rootId, _(
"Speed/Course"), -1, -1, NULL);
226 m_prioTree->SetItemHasChildren(id_velocity);
227 AddLeaves(m_map, 1,
"velocity", id_velocity);
229 wxTreeItemId id_heading = m_prioTree->AppendItem(m_rootId, _(
"Heading"), -1, -1, NULL);
230 m_prioTree->SetItemHasChildren(id_heading);
231 AddLeaves(m_map, 2,
"heading", id_heading);
233 wxTreeItemId id_magvar = m_prioTree->AppendItem(m_rootId, _(
"Mag Variation"), -1, -1, NULL);
234 m_prioTree->SetItemHasChildren(id_magvar);
235 AddLeaves(m_map, 3,
"variation", id_magvar);
237 wxTreeItemId id_sats = m_prioTree->AppendItem(m_rootId, _(
"Satellites"), -1, -1, NULL);
238 m_prioTree->SetItemHasChildren(id_sats);
239 AddLeaves(m_map, 4,
"satellites", id_sats);
241 m_prioTree->ExpandAll();
244 wxTreeItemId rootID = m_prioTree->GetRootItem();
245 wxTreeItemIdValue cookie;
246 int i = m_selmap_index;
247 wxTreeItemId cid = m_prioTree->GetFirstChild(rootID, cookie);
249 while ((i > 0) && cid.IsOk()){
250 cid = m_prioTree->GetNextChild( rootID, cookie);
254 wxTreeItemId ccid = m_prioTree->GetFirstChild(cid, cookie);
257 while ((j > 0) && ccid.IsOk()){
258 ccid = m_prioTree->GetNextChild( cid, cookie);
263 m_prioTree->SelectItem(ccid);
268 void PriorityDlg::OnItemSelected(wxCommandEvent& event){
269 btnMoveUp->Disable();
270 btnMoveDown->Disable();
272 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)
296 btnMoveDown->Enable();
299 void PriorityDlg::OnMoveUpClick(wxCommandEvent& event) {
300 ProcessMove(m_prioTree->GetSelection(), -1);
303 void PriorityDlg::OnMoveDownClick(wxCommandEvent& event) {
304 ProcessMove(m_prioTree->GetSelection(), 1);
307 void PriorityDlg::ProcessMove(wxTreeItemId
id,
int dir){
313 if(pe->m_category >4)
317 wxString priority_string = wxString(m_map[pe->m_category].c_str());
320 wxString prio_array[16];
322 wxStringTokenizer tk(priority_string,
"|");
324 while (tk.HasMoreTokens() && index < 16) {
325 prio_array[index] = tk.GetNextToken();
328 int max_index = index;
332 if (pe->m_index > 0){
334 wxString s_above = 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_above;
342 if (pe->m_index < max_index){
344 wxString s_below = prio_array[pe->m_index + 1];
345 wxString s_move = prio_array[pe->m_index];
346 prio_array[pe->m_index + 1] = s_move;
347 prio_array[pe->m_index] = s_below;
354 for (
int i=0 ; i < 16 ; i++){
355 if (prio_array[i].Length()){
356 prio_mod += prio_array[i];
357 prio_mod += wxString(
"|");
362 std::string s_upd(prio_mod.c_str());
363 m_map[pe->m_category] = s_upd;
366 if (pe->m_category == 0){
368 AdjustCOGSOGPriority();
372 MyApp& app = wxGetApp();
373 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
376 m_map = app.m_comm_bridge.GetPriorityMaps();
380 void PriorityDlg::OnRefreshClick(wxCommandEvent& event) {
382 MyApp& app = wxGetApp();
383 m_map = app.m_comm_bridge.GetPriorityMaps();
387 void PriorityDlg::OnClearClick(wxCommandEvent& event) {
394 m_selmap_index = m_selIndex = 0;
397 MyApp& app = wxGetApp();
398 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
401 m_map = app.m_comm_bridge.GetPriorityMaps();
406 void PriorityDlg::AdjustSatPriority() {
409 std::string sat_prio = m_map[4];
410 wxArrayString sat_sources;
411 wxString sat_priority_string(sat_prio.c_str());
412 wxStringTokenizer tks(sat_priority_string,
"|");
413 while (tks.HasMoreTokens()) {
414 wxString item_string = tks.GetNextToken();
415 sat_sources.Add(item_string);
419 std::string pos_prio = m_map[0];
420 wxString pos_priority_string(pos_prio.c_str());
421 wxStringTokenizer tk(pos_priority_string,
"|");
422 wxArrayString new_sat_prio;
423 while (tk.HasMoreTokens()) {
424 wxString item_string = tk.GetNextToken();
425 wxString pos_channel = item_string.BeforeFirst(
';');
429 for (
size_t i = 0 ; i < sat_sources.GetCount(); i++){
430 if (pos_channel.IsSameAs(sat_sources[i].BeforeFirst(
';'))){
431 new_sat_prio.Add(sat_sources[i]);
433 sat_sources[i] =
"USED";
442 wxString proposed_sat_prio;
443 for (
size_t i = 0 ; i < new_sat_prio.GetCount(); i++){
444 proposed_sat_prio += new_sat_prio[i];
445 proposed_sat_prio += wxString(
"|");
449 m_map[4] = proposed_sat_prio.ToStdString();
452 void PriorityDlg::AdjustCOGSOGPriority() {
455 std::string cogsog_prio = m_map[1];
456 wxArrayString cogsog_sources;
457 wxString cogsog_priority_string(cogsog_prio.c_str());
458 wxStringTokenizer tks(cogsog_priority_string,
"|");
459 while (tks.HasMoreTokens()) {
460 wxString item_string = tks.GetNextToken();
461 cogsog_sources.Add(item_string);
465 std::string pos_prio = m_map[0];
466 wxString pos_priority_string(pos_prio.c_str());
467 wxStringTokenizer tk(pos_priority_string,
"|");
468 wxArrayString new_cogsog_prio;
469 while (tk.HasMoreTokens()) {
470 wxString item_string = tk.GetNextToken();
471 wxString pos_channel = item_string.BeforeFirst(
';');
475 for (
size_t i = 0 ; i < cogsog_sources.GetCount(); i++){
476 if (pos_channel.IsSameAs(cogsog_sources[i].BeforeFirst(
';'))){
477 new_cogsog_prio.Add(cogsog_sources[i]);
479 cogsog_sources[i] =
"USED";
488 wxString proposed_cogsog_prio;
489 for (
size_t i = 0 ; i < new_cogsog_prio.GetCount(); i++){
490 proposed_cogsog_prio += new_cogsog_prio[i];
491 proposed_cogsog_prio += wxString(
"|");
495 m_map[1] = proposed_cogsog_prio.ToStdString();