36#include <wx/tokenzr.h>
38#ifdef __OCPN__ANDROID__
39#include "androidUTIL.h"
43#include "priority_gui.h"
45#include "comm_bridge.h"
46#include "ocpn_frame.h"
54 PriorityEntry(
int category,
int index){ m_category = category, m_index=index; }
57 int m_category, m_index;
63PriorityDlg::PriorityDlg(wxWindow* parent)
64 : wxDialog(parent, wxID_ANY, _(
"Adjust Comm Priorities"), wxDefaultPosition,
65 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);
85 wxBoxSizer* btnEntrySizer =
new wxBoxSizer(wxVERTICAL);
86 secondSizer->Add(btnEntrySizer, 0, wxALL | wxEXPAND, 5);
87 btnMoveUp =
new wxButton(
this, wxID_ANY, _(
"Move Up"));
88 btnMoveDown =
new wxButton(
this, wxID_ANY, _(
"Move Down"));
90 btnMoveDown->Disable();
92 btnEntrySizer->Add(btnMoveUp, 0, wxALL, 5);
93 btnEntrySizer->Add(btnMoveDown, 0, wxALL, 5);
95 btnEntrySizer->AddSpacer(15);
97 btnRefresh =
new wxButton(
this, wxID_ANY, _(
"Refresh"));
98 btnClear =
new wxButton(
this, wxID_ANY, _(
"Clear All"));
100 btnEntrySizer->Add(btnRefresh, 0, wxALL, 5);
101 btnEntrySizer->Add(btnClear, 0, wxALL, 5);
103 wxStdDialogButtonSizer* btnSizer =
new wxStdDialogButtonSizer();
104 wxButton* btnOK =
new wxButton(
this, wxID_OK);
105 wxButton* btnCancel =
new wxButton(
this, wxID_CANCEL, _(
"Cancel"));
106 btnSizer->AddButton(btnOK);
107 btnSizer->AddButton(btnCancel);
109 mainSizer->Add(btnSizer, 0, wxALL | wxEXPAND, 5);
112 btnMoveUp->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
113 wxCommandEventHandler(PriorityDlg::OnMoveUpClick), NULL,
115 btnMoveDown->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
116 wxCommandEventHandler(PriorityDlg::OnMoveDownClick), NULL,
119 btnRefresh->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
120 wxCommandEventHandler(PriorityDlg::OnRefreshClick), NULL,
123 btnClear->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
124 wxCommandEventHandler(PriorityDlg::OnClearClick), NULL,
127 m_prioTree->Connect(wxEVT_TREE_SEL_CHANGED,
128 wxCommandEventHandler(PriorityDlg::OnItemSelected),
132 MyApp& app = wxGetApp();
133 m_map = app.m_comm_bridge.GetPriorityMaps();
137 int n_lines = wxMax(m_prioTree->GetCount(), 15);
139 stcSizer->SetMinSize(m_maxStringLength * GetCharWidth() * 15 / 10,
140 wxMin(gFrame->GetSize().y * 3 /4 , n_lines * GetCharHeight()));
144 mainSizer->Fit(
this);
149void PriorityDlg::AddLeaves(
const std::vector<std::string> &map_list,
150 size_t map_index, std::string map_name,
151 wxTreeItemId leaf_parent){
152 if(map_list.size() < (
size_t)map_index)
156 MyApp& app = wxGetApp();
159 wxString priority_string(map_list[map_index].c_str());
160 wxStringTokenizer tk(priority_string,
"|");
162 while (tk.HasMoreTokens()) {
163 wxString item_string = tk.GetNextToken();
166 m_maxStringLength = wxMax(m_maxStringLength, item_string.Length());
169 wxTreeItemId id_tk = m_prioTree->AppendItem(leaf_parent, item_string, -1, -1, pe);
172 if ( (
size_t)(pc.active_priority) == index)
173 m_prioTree->SetItemBold(id_tk);
180void PriorityDlg::Populate() {
182 m_prioTree->Unselect();
183 m_prioTree->DeleteAllItems();
184 m_maxStringLength = 15;
187 wxTreeItemId m_rootId = m_prioTree->AddRoot(_(
"Priorities"), -1, -1, NULL);
188 m_prioTree->SetItemHasChildren(m_rootId);
190 wxTreeItemId id_position = m_prioTree->AppendItem(m_rootId, _(
"Position"), -1, -1, NULL);
191 m_prioTree->SetItemHasChildren(id_position);
192 AddLeaves(m_map, 0,
"position", id_position);
194 wxTreeItemId id_velocity = m_prioTree->AppendItem(m_rootId, _(
"Speed/Course"), -1, -1, NULL);
195 m_prioTree->SetItemHasChildren(id_velocity);
196 AddLeaves(m_map, 1,
"velocity", id_velocity);
198 wxTreeItemId id_heading = m_prioTree->AppendItem(m_rootId, _(
"Heading"), -1, -1, NULL);
199 m_prioTree->SetItemHasChildren(id_heading);
200 AddLeaves(m_map, 2,
"heading", id_heading);
202 wxTreeItemId id_magvar = m_prioTree->AppendItem(m_rootId, _(
"Mag Variation"), -1, -1, NULL);
203 m_prioTree->SetItemHasChildren(id_magvar);
204 AddLeaves(m_map, 3,
"variation", id_magvar);
206 wxTreeItemId id_sats = m_prioTree->AppendItem(m_rootId, _(
"Satellites"), -1, -1, NULL);
207 m_prioTree->SetItemHasChildren(id_sats);
208 AddLeaves(m_map, 4,
"satellites", id_sats);
210 m_prioTree->ExpandAll();
213 wxTreeItemId rootID = m_prioTree->GetRootItem();
214 wxTreeItemIdValue cookie;
215 int i = m_selmap_index;
216 wxTreeItemId cid = m_prioTree->GetFirstChild(rootID, cookie);
218 while ((i > 0) && cid.IsOk()){
219 cid = m_prioTree->GetNextChild( rootID, cookie);
223 wxTreeItemId ccid = m_prioTree->GetFirstChild(cid, cookie);
226 while ((j > 0) && ccid.IsOk()){
227 ccid = m_prioTree->GetNextChild( cid, cookie);
232 m_prioTree->SelectItem(ccid);
237void PriorityDlg::OnItemSelected(wxCommandEvent& event){
238 btnMoveUp->Disable();
239 btnMoveDown->Disable();
241 wxTreeItemId
id = m_prioTree->GetSelection();
246 m_selIndex = pe->m_index;
247 m_selmap_index = pe->m_category;
249 if (pe->m_index > 0){
253 wxTreeItemId id_parent = m_prioTree->GetItemParent(
id);
257 wxTreeItemIdValue cookie;
258 wxTreeItemId ch = m_prioTree->GetFirstChild(id_parent, cookie);
261 ch = m_prioTree->GetNextChild(id_parent, cookie);
264 if (pe->m_index < n_sibs-1)
265 btnMoveDown->Enable();
268void PriorityDlg::OnMoveUpClick(wxCommandEvent& event) {
269 ProcessMove(m_prioTree->GetSelection(), -1);
272void PriorityDlg::OnMoveDownClick(wxCommandEvent& event) {
273 ProcessMove(m_prioTree->GetSelection(), 1);
276void PriorityDlg::ProcessMove(wxTreeItemId
id,
int dir){
282 if(pe->m_category >4)
286 wxString priority_string = wxString(m_map[pe->m_category].c_str());
289 wxString prio_array[16];
291 wxStringTokenizer tk(priority_string,
"|");
293 while (tk.HasMoreTokens() && index < 16) {
294 prio_array[index] = tk.GetNextToken();
297 int max_index = index;
301 if (pe->m_index > 0){
303 wxString s_above = prio_array[pe->m_index - 1];
304 wxString s_move = prio_array[pe->m_index];
305 prio_array[pe->m_index - 1] = s_move;
306 prio_array[pe->m_index] = s_above;
311 if (pe->m_index < max_index){
313 wxString s_below = prio_array[pe->m_index + 1];
314 wxString s_move = prio_array[pe->m_index];
315 prio_array[pe->m_index + 1] = s_move;
316 prio_array[pe->m_index] = s_below;
323 for (
int i=0 ; i < 16 ; i++){
324 if (prio_array[i].Length()){
325 prio_mod += prio_array[i];
326 prio_mod += wxString(
"|");
331 std::string s_upd(prio_mod.c_str());
332 m_map[pe->m_category] = s_upd;
335 if (pe->m_category == 0){
337 AdjustCOGSOGPriority();
341 MyApp& app = wxGetApp();
342 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
345 m_map = app.m_comm_bridge.GetPriorityMaps();
349void PriorityDlg::OnRefreshClick(wxCommandEvent& event) {
351 MyApp& app = wxGetApp();
352 m_map = app.m_comm_bridge.GetPriorityMaps();
356void PriorityDlg::OnClearClick(wxCommandEvent& event) {
363 m_selmap_index = m_selIndex = 0;
366 MyApp& app = wxGetApp();
367 app.m_comm_bridge.UpdateAndApplyMaps(m_map);
370 m_map = app.m_comm_bridge.GetPriorityMaps();
375void PriorityDlg::AdjustSatPriority() {
378 std::string sat_prio = m_map[4];
379 wxArrayString sat_sources;
380 wxString sat_priority_string(sat_prio.c_str());
381 wxStringTokenizer tks(sat_priority_string,
"|");
382 while (tks.HasMoreTokens()) {
383 wxString item_string = tks.GetNextToken();
384 sat_sources.Add(item_string);
388 std::string pos_prio = m_map[0];
389 wxString pos_priority_string(pos_prio.c_str());
390 wxStringTokenizer tk(pos_priority_string,
"|");
391 wxArrayString new_sat_prio;
392 while (tk.HasMoreTokens()) {
393 wxString item_string = tk.GetNextToken();
394 wxString pos_channel = item_string.BeforeFirst(
';');
398 for (
size_t i = 0 ; i < sat_sources.GetCount(); i++){
399 if (pos_channel.IsSameAs(sat_sources[i].BeforeFirst(
';'))){
400 new_sat_prio.Add(sat_sources[i]);
402 sat_sources[i] =
"USED";
411 wxString proposed_sat_prio;
412 for (
size_t i = 0 ; i < new_sat_prio.GetCount(); i++){
413 proposed_sat_prio += new_sat_prio[i];
414 proposed_sat_prio += wxString(
"|");
418 m_map[4] = proposed_sat_prio.ToStdString();
421void PriorityDlg::AdjustCOGSOGPriority() {
424 std::string cogsog_prio = m_map[1];
425 wxArrayString cogsog_sources;
426 wxString cogsog_priority_string(cogsog_prio.c_str());
427 wxStringTokenizer tks(cogsog_priority_string,
"|");
428 while (tks.HasMoreTokens()) {
429 wxString item_string = tks.GetNextToken();
430 cogsog_sources.Add(item_string);
434 std::string pos_prio = m_map[0];
435 wxString pos_priority_string(pos_prio.c_str());
436 wxStringTokenizer tk(pos_priority_string,
"|");
437 wxArrayString new_cogsog_prio;
438 while (tk.HasMoreTokens()) {
439 wxString item_string = tk.GetNextToken();
440 wxString pos_channel = item_string.BeforeFirst(
';');
444 for (
size_t i = 0 ; i < cogsog_sources.GetCount(); i++){
445 if (pos_channel.IsSameAs(cogsog_sources[i].BeforeFirst(
';'))){
446 new_cogsog_prio.Add(cogsog_sources[i]);
448 cogsog_sources[i] =
"USED";
457 wxString proposed_cogsog_prio;
458 for (
size_t i = 0 ; i < new_cogsog_prio.GetCount(); i++){
459 proposed_cogsog_prio += new_cogsog_prio[i];
460 proposed_cogsog_prio += wxString(
"|");
464 m_map[1] = proposed_cogsog_prio.ToStdString();
Data for selected source from multiple candidates.