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
98void iENCToolbar::SetColorScheme(ColorScheme cs) {
99 m_nDensity = -1;
100 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
101 SetRangeToolBitmap();
102 ocpnFloatingToolbarDialog::SetColorScheme(cs);
103 m_ptoolbar->InvalidateBitmaps();
104 RefreshToolbar();
105 m_range = 0;
106}
107
108void iENCToolbar::LoadToolBitmaps() {
109 wxString svgDir = g_Platform->GetSharedDataDir() + "uidata" +
110 wxFileName::GetPathSeparator();
111
112 int w = 96;
113 int h = 32;
114
115 if (::wxFileExists(svgDir + "iENC_All.svg")) {
116 m_bmAll = LoadSVG(svgDir + "iENC_All.svg", w, h);
117 m_bmMinimum = LoadSVG(svgDir + "iENC_Minimum.svg", w, h);
118 m_bmStandard = LoadSVG(svgDir + "iENC_Standard.svg", w, h);
119 m_bmUStd = LoadSVG(svgDir + "iENC_UserStd.svg", w, h);
120 m_bmRPlus = LoadSVG(svgDir + "iENC_RPlus.svg", w, h);
121 m_bmRMinus = LoadSVG(svgDir + "iENC_RMinus.svg", w, h);
122 } else {
123 wxLogMessage("Cannot find iENC icons at: " + svgDir);
124
125 m_bmMinimum = wxBitmap(96, 32);
126 m_bmStandard = wxBitmap(96, 32);
127 m_bmAll = wxBitmap(96, 32);
128 m_bmUStd = wxBitmap(96, 32);
129 m_bmRPlus = wxBitmap(96, 32);
130 m_bmRMinus = wxBitmap(96, 32);
131 }
132}
133
134void iENCToolbar::OnToolLeftClick(wxCommandEvent &event) {
135 int itemId = event.GetId();
136
137 AbstractChartCanvas *acc = top_frame::Get()->GetAbstractPrimaryCanvas();
138
139 enum _DisCat nset = STANDARD;
140 double range;
141
142 switch (itemId) {
143 case ID_DENSITY:
144
145 if (++m_nDensity > 3) m_nDensity = 0;
146
147 SetDensityToolBitmap(m_nDensity);
148
149 switch (m_nDensity) {
150 case 0:
151 nset = DISPLAYBASE;
152 break;
153 case 1:
154 nset = STANDARD;
155 break;
156 case 2:
157 nset = OTHER;
158 break;
159 case 3:
160 nset = MARINERS_STANDARD;
161 break;
162 default:
163 nset = STANDARD;
164 break;
165 }
166
167 top_frame::Get()->SetENCDisplayCategory(acc, nset);
168
169 break;
170
171 case ID_RMINUS:
172 range = acc->GetCanvasRangeMeters();
173 range = wxRound(range * 10) / 10.;
174
175 if (range > 8000.) acc->SetCanvasRangeMeters(8000.);
176 if (range > 4000.)
177 acc->SetCanvasRangeMeters(4000.);
178 else if (range > 2000.)
179 acc->SetCanvasRangeMeters(2000.);
180 else if (range > 1600.)
181 acc->SetCanvasRangeMeters(1600.);
182 else if (range > 1200.)
183 acc->SetCanvasRangeMeters(1200.);
184 else if (range > 800.)
185 acc->SetCanvasRangeMeters(800.);
186 else if (range > 500.)
187 acc->SetCanvasRangeMeters(500.);
188 else if (range > 300.)
189 acc->SetCanvasRangeMeters(300.);
190
191 break;
192
193 case ID_RPLUS:
194 range = acc->GetCanvasRangeMeters();
195 range = wxRound(range * 10) / 10.;
196
197 if (range < 300.)
198 acc->SetCanvasRangeMeters(300.);
199 else if (range < 500.)
200 acc->SetCanvasRangeMeters(500.);
201 else if (range < 800.)
202 acc->SetCanvasRangeMeters(800.);
203 else if (range < 1200.)
204 acc->SetCanvasRangeMeters(1200.);
205 else if (range < 1600.)
206 acc->SetCanvasRangeMeters(1600.);
207 else if (range < 2000.)
208 acc->SetCanvasRangeMeters(2000.);
209 else if (range < 4000.)
210 acc->SetCanvasRangeMeters(4000.);
211 else if (range < 8000.)
212 acc->SetCanvasRangeMeters(8000.);
213
214 break;
215
216 default:
217 break;
218 }
219 m_ptoolbar->InvalidateBitmaps();
220 RefreshToolbar();
221}
222
223void iENCToolbar::SetDensityToolBitmap(int nDensity) {
224 int toolID = m_toolDensity->GetId();
225
226 if (nDensity == 0)
227 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmMinimum, &m_bmMinimum);
228 else if (nDensity == 1)
229 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmStandard, &m_bmStandard);
230 else if (nDensity == 2)
231 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmAll, &m_bmAll);
232 else if (nDensity == 3)
233 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmUStd, &m_bmUStd);
234}
235
236void iENCToolbar::SetRangeToolBitmap() {
237 wxMemoryDC dc;
238 dc.SelectObject(*m_pbmScratch);
239 dc.SetBackground(wxBrush(GetGlobalColor("DILG0")));
240 dc.Clear();
241 dc.DrawBitmap(m_bmRMinus, wxPoint(0, 0));
242
243 // Render the range as text onto the template
244 m_rangeFont = wxTheFontList->FindOrCreateFont(
245 12, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
246
247 wxString range_string;
248 range_string.Printf("%4.0fm", m_range);
249
250 dc.SetFont(*m_rangeFont);
251
252 // Select a font that will fit into the allow space.
253 bool good = false;
254 int target_points = 12;
255 while (!good && (target_points > 4)) {
256 int width, height;
257 dc.GetTextExtent(range_string, &width, &height);
258 if (width < 50) {
259 good = true;
260 break;
261 } else {
262 target_points--;
263 m_rangeFont = wxTheFontList->FindOrCreateFont(
264 target_points, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
265 wxFONTWEIGHT_NORMAL);
266 dc.SetFont(*m_rangeFont);
267 }
268 }
269
270 dc.SetTextForeground(*wxBLACK);
271 dc.SetBackgroundMode(wxTRANSPARENT);
272
273 dc.DrawText(range_string, 42, 8);
274
275 dc.SelectObject(wxNullBitmap);
276
277 m_ptoolbar->SetToolBitmaps(ID_RMINUS, m_pbmScratch, m_pbmScratch);
278}
279
280void iENCToolbar::StateTimerEvent(wxTimerEvent &event) {
281 AbstractChartCanvas *acc = top_frame::Get()->GetAbstractPrimaryCanvas();
282 if (!acc) return;
283
284 bool bRefresh = false;
285 // Keep the Density tool in sync
286 if (ps52plib) {
287 int nset = 1;
288
289 auto acc = top_frame::Get()->GetAbstractPrimaryCanvas();
290 switch (acc->GetENCDisplayCategory()) {
291 case (DISPLAYBASE):
292 nset = 0;
293 break;
294 case (STANDARD):
295 nset = 1;
296 break;
297 case (OTHER):
298 nset = 2;
299 break;
300 case (MARINERS_STANDARD):
301 nset = 3;
302 break;
303 default:
304 nset = 1;
305 break;
306 }
307
308 if (nset != m_nDensity) {
309 if (nset < 3) {
310 m_nDensity = nset;
311 SetDensityToolBitmap(m_nDensity);
312
313 bRefresh = true;
314 }
315 }
316 }
317
318 // Keep the Range annunciator updated
319
320 if (acc) {
321 double range = acc->GetCanvasRangeMeters();
322
323 if (range != m_range) {
324 m_range = range;
325 SetRangeToolBitmap();
326 bRefresh = true;
327 }
328 }
329 if (bRefresh) {
330 RebuildToolbar();
331 m_ptoolbar->InvalidateBitmaps();
332 RefreshToolbar();
333 }
334
335 // Restore runtime timer frequency after startup
336 m_state_timer.Start(500, wxTIMER_CONTINUOUS);
337}
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.