OpenCPN Partial API docs
Loading...
Searching...
No Matches
ienc_toolbar.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2017 by David S. Register *
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 "config.h"
25
26#include <wx/wxprec.h>
27
28#ifndef WX_PRECOMP
29#include <wx/wx.h>
30#endif
31
32#include <wx/bitmap.h>
33#include <wx/brush.h>
34#include <wx/dcmemory.h>
35#include <wx/event.h>
36#include <wx/file.h>
37#include <wx/filename.h>
38#include <wx/log.h>
39
40#include "ienc_toolbar.h"
41
42#include "model/svg_utils.h"
43
44#include "chcanv.h"
45#include "ocpn_platform.h"
46#include "pluginmanager.h"
47#include "s52plib.h"
48#include "s52s57.h"
49#include "toolbar.h"
50#include "top_frame.h"
51#include "user_colors.h"
52
54
55//---------------------------------------------------------------------------------------
56// iENCToolbar Implementation
57//---------------------------------------------------------------------------------------
58
59iENCToolbar::iENCToolbar(wxWindow *parent, wxPoint pos, long orient,
60 float size_factor, ToolbarDlgCallbacks callbacks)
61 : ocpnFloatingToolbarDialog(parent, pos, orient, size_factor, callbacks) {
62 LoadToolBitmaps();
63
64 wxSize a = m_bmMinimum.GetSize();
65 m_ptoolbar->SetToolBitmapSize(a);
66
67 m_pbmScratch = new wxBitmap(a.x, a.y);
68
69 m_bmTemplate = m_bmRMinus;
70
71 m_toolDensity =
72 m_ptoolbar->AddTool(ID_DENSITY, "Density", m_bmMinimum, m_bmMinimum);
73 m_ptoolbar->AddTool(ID_RPLUS, "RangePlus", m_bmRPlus, m_bmRPlus);
74 m_ptoolbar->AddTool(ID_RMINUS, "RangeMinus", m_bmRMinus, m_bmRMinus);
75
76 SetCanToggleOrientation(false);
77 EnableRolloverBitmaps(false);
78 DisableTooltips();
79
80 m_nDensity = 0;
81 SetDensityToolBitmap(m_nDensity);
82 m_range = 0;
83
84 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
85
86 // Realize() the toolbar
87 Realize();
88 SetDefaultPosition();
89
90 m_state_timer.SetOwner(this, STATE_TIMER);
91 m_state_timer.Start(100, wxTIMER_CONTINUOUS);
92 this->Connect(wxEVT_TIMER, wxTimerEventHandler(iENCToolbar::StateTimerEvent),
93 NULL, this);
94}
95
96iENCToolbar::~iENCToolbar() {
97 m_state_timer.Stop();
98 delete m_pbmScratch;
99}
100
101void iENCToolbar::SetColorScheme(ColorScheme cs) {
102 m_nDensity = -1;
103 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
104 SetRangeToolBitmap();
105 ocpnFloatingToolbarDialog::SetColorScheme(cs);
106 m_ptoolbar->InvalidateBitmaps();
107 RefreshToolbar();
108 m_range = 0;
109}
110
111void iENCToolbar::LoadToolBitmaps() {
112 wxString svgDir = g_Platform->GetSharedDataDir() + "uidata" +
113 wxFileName::GetPathSeparator();
114
115 int w = 96;
116 int h = 32;
117
118 if (::wxFileExists(svgDir + "iENC_All.svg")) {
119 m_bmAll = LoadSVG(svgDir + "iENC_All.svg", w, h);
120 m_bmMinimum = LoadSVG(svgDir + "iENC_Minimum.svg", w, h);
121 m_bmStandard = LoadSVG(svgDir + "iENC_Standard.svg", w, h);
122 m_bmUStd = LoadSVG(svgDir + "iENC_UserStd.svg", w, h);
123 m_bmRPlus = LoadSVG(svgDir + "iENC_RPlus.svg", w, h);
124 m_bmRMinus = LoadSVG(svgDir + "iENC_RMinus.svg", w, h);
125 } else {
126 wxLogMessage("Cannot find iENC icons at: " + svgDir);
127
128 m_bmMinimum = wxBitmap(96, 32);
129 m_bmStandard = wxBitmap(96, 32);
130 m_bmAll = wxBitmap(96, 32);
131 m_bmUStd = wxBitmap(96, 32);
132 m_bmRPlus = wxBitmap(96, 32);
133 m_bmRMinus = wxBitmap(96, 32);
134 }
135}
136
137void iENCToolbar::OnToolLeftClick(wxCommandEvent &event) {
138 int itemId = event.GetId();
139
140 AbstractChartCanvas *acc = top_frame::Get()->GetAbstractPrimaryCanvas();
141
142 enum _DisCat nset = STANDARD;
143 double range;
144
145 switch (itemId) {
146 case ID_DENSITY:
147
148 if (++m_nDensity > 3) m_nDensity = 0;
149
150 SetDensityToolBitmap(m_nDensity);
151
152 switch (m_nDensity) {
153 case 0:
154 nset = DISPLAYBASE;
155 break;
156 case 1:
157 nset = STANDARD;
158 break;
159 case 2:
160 nset = OTHER;
161 break;
162 case 3:
163 nset = MARINERS_STANDARD;
164 break;
165 default:
166 nset = STANDARD;
167 break;
168 }
169
170 top_frame::Get()->SetENCDisplayCategory(acc, nset);
171
172 break;
173
174 case ID_RMINUS:
175 range = acc->GetCanvasRangeMeters();
176 range = wxRound(range * 10) / 10.;
177
178 if (range > 8000.) acc->SetCanvasRangeMeters(8000.);
179 if (range > 4000.)
180 acc->SetCanvasRangeMeters(4000.);
181 else if (range > 2000.)
182 acc->SetCanvasRangeMeters(2000.);
183 else if (range > 1600.)
184 acc->SetCanvasRangeMeters(1600.);
185 else if (range > 1200.)
186 acc->SetCanvasRangeMeters(1200.);
187 else if (range > 800.)
188 acc->SetCanvasRangeMeters(800.);
189 else if (range > 500.)
190 acc->SetCanvasRangeMeters(500.);
191 else if (range > 300.)
192 acc->SetCanvasRangeMeters(300.);
193
194 break;
195
196 case ID_RPLUS:
197 range = acc->GetCanvasRangeMeters();
198 range = wxRound(range * 10) / 10.;
199
200 if (range < 300.)
201 acc->SetCanvasRangeMeters(300.);
202 else if (range < 500.)
203 acc->SetCanvasRangeMeters(500.);
204 else if (range < 800.)
205 acc->SetCanvasRangeMeters(800.);
206 else if (range < 1200.)
207 acc->SetCanvasRangeMeters(1200.);
208 else if (range < 1600.)
209 acc->SetCanvasRangeMeters(1600.);
210 else if (range < 2000.)
211 acc->SetCanvasRangeMeters(2000.);
212 else if (range < 4000.)
213 acc->SetCanvasRangeMeters(4000.);
214 else if (range < 8000.)
215 acc->SetCanvasRangeMeters(8000.);
216
217 break;
218
219 default:
220 break;
221 }
222 m_ptoolbar->InvalidateBitmaps();
223 RefreshToolbar();
224}
225
226void iENCToolbar::SetDensityToolBitmap(int nDensity) {
227 int toolID = m_toolDensity->GetId();
228
229 if (nDensity == 0)
230 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmMinimum, &m_bmMinimum);
231 else if (nDensity == 1)
232 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmStandard, &m_bmStandard);
233 else if (nDensity == 2)
234 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmAll, &m_bmAll);
235 else if (nDensity == 3)
236 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmUStd, &m_bmUStd);
237}
238
239void iENCToolbar::SetRangeToolBitmap() {
240 wxMemoryDC dc;
241 dc.SelectObject(*m_pbmScratch);
242 dc.SetBackground(wxBrush(GetGlobalColor("DILG0")));
243 dc.Clear();
244 dc.DrawBitmap(m_bmRMinus, wxPoint(0, 0));
245
246 // Render the range as text onto the template
247 m_rangeFont = wxTheFontList->FindOrCreateFont(
248 12, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
249
250 wxString range_string;
251 range_string.Printf("%4.0fm", m_range);
252
253 dc.SetFont(*m_rangeFont);
254
255 // Select a font that will fit into the allow space.
256 bool good = false;
257 int target_points = 12;
258 while (!good && (target_points > 4)) {
259 int width, height;
260 dc.GetTextExtent(range_string, &width, &height);
261 if (width < 50) {
262 good = true;
263 break;
264 } else {
265 target_points--;
266 m_rangeFont = wxTheFontList->FindOrCreateFont(
267 target_points, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
268 wxFONTWEIGHT_NORMAL);
269 dc.SetFont(*m_rangeFont);
270 }
271 }
272
273 dc.SetTextForeground(*wxBLACK);
274 dc.SetBackgroundMode(wxTRANSPARENT);
275
276 dc.DrawText(range_string, 42, 8);
277
278 dc.SelectObject(wxNullBitmap);
279
280 m_ptoolbar->SetToolBitmaps(ID_RMINUS, m_pbmScratch, m_pbmScratch);
281}
282
283void iENCToolbar::StateTimerEvent(wxTimerEvent &event) {
284 AbstractChartCanvas *acc = top_frame::Get()->GetAbstractPrimaryCanvas();
285 if (!acc) return;
286
287 bool bRefresh = false;
288 // Keep the Density tool in sync
289 if (ps52plib) {
290 int nset = 1;
291
292 switch (acc->GetENCDisplayCategory()) {
293 case (DISPLAYBASE):
294 nset = 0;
295 break;
296 case (STANDARD):
297 nset = 1;
298 break;
299 case (OTHER):
300 nset = 2;
301 break;
302 case (MARINERS_STANDARD):
303 nset = 3;
304 break;
305 default:
306 nset = 1;
307 break;
308 }
309
310 if (nset != m_nDensity) {
311 if (nset < 3) {
312 m_nDensity = nset;
313 SetDensityToolBitmap(m_nDensity);
314
315 bRefresh = true;
316 }
317 }
318 }
319
320 // Keep the Range annunciator updated
321
322 if (acc) {
323 double range = acc->GetCanvasRangeMeters();
324
325 if (range != m_range) {
326 m_range = range;
327 SetRangeToolBitmap();
328 bRefresh = true;
329 }
330 }
331 if (bRefresh) {
332 RebuildToolbar();
333 m_ptoolbar->InvalidateBitmaps();
334 RefreshToolbar();
335 }
336
337 // Restore runtime timer frequency after startup
338 m_state_timer.Start(500, wxTIMER_CONTINUOUS);
339}
Generic Chart canvas base.
Minimal ChartCAnvas interface with very little dependencies.
Floating toolbar for iENC (International Electronic Navigational Chart) functionality.
Floating toolbar dialog for OpenCPN.
Definition toolbar.h:395
iENCToolbar * g_iENCToolbar
Global instance.
iENC specific chart operations floating toolbar extension
OpenCPN Platform specific support utilities.
PlugInManager and helper classes – Mostly gui parts (dialogs) and plugin API stuff.
wxBitmap LoadSVG(const wxString filename, const unsigned int width, const unsigned int height, wxBitmap *default_bitmap, bool use_cache)
Load SVG file and return it's bitmap representation of requested size In case file can't be loaded an...
Definition svg_utils.cpp:59
SVG utilities.
OpenCPN Toolbar.
Abstract gFrame/MyFrame interface.