OpenCPN Partial API docs
Loading...
Searching...
No Matches
cat_settings.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2019 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17
24#include <map>
25#include <sstream>
26#include <string>
27
28#include <wx/button.h>
29#include <wx/choice.h>
30#include <wx/log.h>
31#include <wx/sizer.h>
32#include <wx/stattext.h>
33#include <wx/textctrl.h>
34
35#include "cat_settings.h"
36#include "observable_globvar.h"
37#include "model/config_vars.h"
38#include "model/ocpn_utils.h"
39#include "model/plugin_cache.h"
41
43class CustomCatalogCtrl : public wxTextCtrl {
44public:
45 CustomCatalogCtrl(wxWindow* parent) : wxTextCtrl(parent, wxID_ANY, "") {
46 SetValue(g_catalog_custom_url);
47 Bind(wxEVT_TEXT,
48 [&](wxCommandEvent&) { g_catalog_custom_url = GetValue(); });
49 }
50};
51
53class PlatformChoice : public wxChoice {
54public:
55 PlatformChoice(wxWindow* parent, wxStaticText* selected)
56 : wxChoice(), m_selected(selected) {
57 Bind(wxEVT_CHOICE, &PlatformChoice::OnChoice, this);
58 Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, getLabels());
59 SetSelection(0);
60 Layout();
61 }
62
63private:
64 wxStaticText* m_selected;
65
66 void OnChoice(wxCommandEvent&) {
67 GlobalVar<wxString> compat_os(&g_compatOS);
68 if (GetSelection() == 0) {
69 // "Select new flavour"
70 return;
71 }
72 if (GetSelection() == 1) {
73 // "Default Setting"
74 g_compatOsVersion = "";
75 compat_os.Set("");
76 auto newOS = CompatOs::GetInstance();
77 m_selected->SetLabel(newOS->name() + ":" + newOS->version());
78 } else {
79 auto current = GetString(GetSelection());
80 auto os = ocpn::split(current, " ")[0];
81 m_selected->SetLabel(os);
82 compat_os.Set(ocpn::split(os.c_str(), ":")[0]);
83 g_compatOsVersion = ocpn::split(os.c_str(), ":")[1];
84 }
85 }
86
87 wxArrayString getLabels() {
88 auto plug_handler = PluginHandler::GetInstance();
89 wxArrayString labels;
90 labels.Add(_("Select new flavour"));
91 labels.Add(_("Default setting"));
92 for (const auto& c : plug_handler->GetCountByTarget()) {
93 std::stringstream ss;
94 ss << c.first << " (" << c.second << ")";
95 labels.Add(ss.str());
96 }
97 return labels;
98 }
99};
100
102class CatalogChoice : public wxChoice {
103public:
104 CatalogChoice(wxWindow* parent, wxTextCtrl* custom_ctrl)
105 : wxChoice(), m_custom_ctrl(custom_ctrl) {
106 static const std::vector<std::string> labels(
107 {"master", "Beta", "Alpha", "custom"});
108 wxArrayString wxLabels;
109 for (const auto& l : labels) wxLabels.Add(l);
110 Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLabels);
111
112#ifdef __ANDROID__
113 SetMinSize(wxSize(12 * GetCharWidth(), -1));
114#endif
115
116 m_custom_ctrl->Enable(false);
117 for (auto l = labels.begin(); l != labels.end(); l++) {
118 if (g_catalog_channel == *l) {
119 SetSelection(l - labels.begin());
120 }
121 }
122 wxCommandEvent ev;
123 OnChoice(ev);
124 Layout();
125 Bind(wxEVT_CHOICE, &CatalogChoice::OnChoice, this);
126 }
127
128private:
129 wxTextCtrl* m_custom_ctrl;
130
131 void OnChoice(wxCommandEvent&) {
132 auto selected = GetString(GetSelection());
133 m_custom_ctrl->Enable(selected == "custom");
134 if (selected == "custom") {
135 m_custom_ctrl->Show();
136 GetParent()->Layout();
137 m_custom_ctrl->SetFocus();
138 g_catalog_custom_url = m_custom_ctrl->GetValue();
139 } else {
140 m_custom_ctrl->Hide();
141 }
142 GlobalVar<wxString> catalog(&g_catalog_channel);
143 catalog.Set(selected);
144 Layout();
145 }
146};
147
149class CompatText : public wxStaticText {
150public:
151 CompatText(wxWindow* parent) : wxStaticText(parent, wxID_ANY, "") {
152 auto compatOs = CompatOs::GetInstance();
153 SetLabel(compatOs->name() + ":" + compatOs->version());
154 }
155};
156
158class CatalogSizer : public wxStaticBoxSizer {
159public:
160 CatalogSizer(wxWindow* parent)
161 : wxStaticBoxSizer(wxHORIZONTAL, parent, _("Active catalog")) {
162 auto flags = wxSizerFlags().Border(wxALL, parent->GetCharWidth());
163 Add(new wxStaticText(parent, wxID_ANY, _("Select plugin catalog")), flags);
164 auto custom_ctrl = new CustomCatalogCtrl(parent);
165 Add(new CatalogChoice(parent, custom_ctrl), flags);
166 Add(custom_ctrl, flags.Expand().Proportion(1));
167 Layout();
168 }
169};
170
172class CompatSizer : public wxStaticBoxSizer {
173public:
174 CompatSizer(wxWindow* parent)
175 : wxStaticBoxSizer(wxHORIZONTAL, parent, _("Compatibility")) {
176 auto flags = wxSizerFlags().Border();
177 Add(new wxStaticText(parent, wxID_ANY, _("Active setting:")),
178 flags.Center());
179 auto status_text = new CompatText(parent);
180 Add(status_text, flags.Center().Proportion(1));
181 Add(new PlatformChoice(parent, status_text), flags);
182 }
183};
184
185/* Cache panel, size feedback and clear button. */
186class CacheSizer : public wxStaticBoxSizer {
187public:
188 CacheSizer(wxWindow* parent)
189 : wxStaticBoxSizer(wxHORIZONTAL, parent, _("Cache")) {
190 using CmdEvt = wxCommandEvent;
191
192 auto flags = wxSizerFlags().Border();
193 m_label = new wxStaticText(parent, wxID_ANY, "");
194 update_label();
195 Add(m_label, flags.Center().Proportion(1));
196
197 Add(1, 1, 1, wxEXPAND); // Expanding spacer
198 m_clear_button = new wxButton(parent, wxID_ANY, _("Clear cache"));
199 Add(m_clear_button, flags);
200 m_clear_button->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
201 [=](CmdEvt& e) { on_clear_btn_clicked(); });
202 }
203
204private:
205 wxButton* m_clear_button;
206 wxStaticText* m_label;
207
208 void on_clear_btn_clicked() {
210 update_label();
211 }
212
213 void update_label() {
214 char buf[128];
215 snprintf(buf, sizeof(buf), _("Size: %d MB in %d files"), ocpn::cache_size(),
217 m_label->SetLabel(buf);
218 }
219};
220
222class ButtonsSizer : public wxStdDialogButtonSizer {
223public:
224 ButtonsSizer(wxWindow* parent) : wxStdDialogButtonSizer() {
225 auto button = new wxButton(parent, wxID_OK, "LongLabel", wxDefaultPosition,
226 wxSize(10 * parent->GetCharWidth(), -1));
227 button->SetLabel(_("Done"));
228 SetAffirmativeButton(button);
229 Realize();
230 }
231};
232
235 : wxDialog(parent, wxID_ANY, _("Plugin Catalog Settings"),
236 wxDefaultPosition, wxDefaultSize,
237 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
238#ifdef __ANDROID__
239 SetBackgroundColour(wxColour(0x7c, 0xb0, 0xe9)); // light blue
240#endif
241 auto vbox = new wxBoxSizer(wxVERTICAL);
242
243 vbox->Add(new CatalogSizer(this), wxSizerFlags().Expand().DoubleBorder());
244#ifndef __ANDROID__
245 vbox->Add(new CompatSizer(this), wxSizerFlags().Expand().DoubleBorder());
246#endif
247 vbox->Add(new CacheSizer(this), wxSizerFlags().Expand().DoubleBorder());
248 vbox->Add(new ButtonsSizer(this), wxSizerFlags().Expand().DoubleBorder());
249
250 SetSizer(vbox);
251 Fit();
252 Layout();
253 SetMinSize(GetSize());
254}
Plugin catalog settings dialog.
The Done button.
Select master, beta, alpha, custom drop-down.
CatalogSettingsDialog(wxWindow *parent)
Top-level plugin settings dialog.
Catalog channel selection panel.
Plugin compatibility panel.
Current selected compatibility.
The custom URL text entry.
Wrapper for global variable, supports notification events when value changes.
Select compatibility drop-down.
static PluginHandler * GetInstance()
Singleton factory.
Global variables stored in configuration file.
std::vector< std::string > split(const char *token_string, const std::string &delimiter)
Return vector of items in s separated by delimiter.
void cache_clear()
Remove all files in cache:
unsigned cache_file_count()
Return number of files in cache.
unsigned long cache_size()
Return total size of files in cache in kbytes.
Global variables Listen()/Notify() wrapper.
Miscellaneous utilities, many of which string related.
PLugin remote repositories installation and Uninstall/list operations.