OpenCPN Partial API docs
Loading...
Searching...
No Matches
AISTargetQueryDialog.cpp
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2013 by David S. Register *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************
23 */
24
25#include <wx/wxprec.h>
26
27#include <wx/html/htmlwin.h>
28
29#include "model/ais_decoder.h"
31#include "model/ais_target_data.h"
32#include "model/route_point.h"
33#include "model/select.h"
34#include "model/track.h"
35
36#include "ais.h"
37#include "AISTargetQueryDialog.h"
38#include "chcanv.h"
39#include "FontMgr.h"
40#include "navutil.h"
41#include "ocpn_frame.h"
42#include "OCPNPlatform.h"
43#include "routemanagerdialog.h"
44#include "model/navobj_db.h"
45
46extern AISTargetQueryDialog *g_pais_query_dialog_active;
47extern ColorScheme global_color_scheme;
48extern wxString g_default_wp_icon;
49extern MyConfig *pConfig;
50extern RouteManagerDialog *pRouteManagerDialog;
51extern std::vector<Track *> g_TrackList;
52extern OCPNPlatform *g_Platform;
53extern MyFrame *gFrame;
54
55#define xID_OK 10009
56#define xID_WPT_CREATE 10010
57#define xID_TRK_CREATE 10011
58IMPLEMENT_CLASS(AISTargetQueryDialog, wxDialog)
59// AISTargetQueryDialog event table definition
60BEGIN_EVENT_TABLE(AISTargetQueryDialog, wxFrame)
61EVT_BUTTON(xID_OK, AISTargetQueryDialog::OnIdOKClick)
62EVT_BUTTON(xID_WPT_CREATE, AISTargetQueryDialog::OnIdWptCreateClick)
63EVT_BUTTON(xID_TRK_CREATE, AISTargetQueryDialog::OnIdTrkCreateClick)
64EVT_CLOSE(AISTargetQueryDialog::OnClose)
65EVT_MOVE(AISTargetQueryDialog::OnMove)
66EVT_SIZE(AISTargetQueryDialog::OnSize)
67EVT_CHAR_HOOK(AISTargetQueryDialog::OnKey)
68END_EVENT_TABLE()
69
71
72AISTargetQueryDialog::AISTargetQueryDialog(wxWindow *parent, wxWindowID id,
73 const wxString &caption,
74 const wxPoint &pos,
75 const wxSize &size, long style) {
76 Init();
77 Create(parent, id, caption, pos, size, style);
78}
79
80AISTargetQueryDialog::~AISTargetQueryDialog() { delete m_pQueryTextCtl; }
81
83 m_MMSI = -1;
84 m_pQueryTextCtl = NULL;
85 m_nl = 0;
86 m_colorscheme = (ColorScheme)(-1);
87 m_okButton = NULL;
88 m_bautoCentre = false;
89 m_bautosize = false;
90}
91void AISTargetQueryDialog::OnClose(wxCloseEvent &event) {
92 Destroy();
93 g_pais_query_dialog_active = NULL;
94}
95
96void AISTargetQueryDialog::OnIdOKClick(wxCommandEvent &event) { Close(); }
97
98void AISTargetQueryDialog::OnKey(wxKeyEvent &ke) {
99 if (ke.GetKeyCode() == WXK_ESCAPE)
100 Close(true);
101 else
102 ke.Skip();
103}
104
105void AISTargetQueryDialog::OnIdWptCreateClick(wxCommandEvent &event) {
106 if (m_MMSI != 0) { // Faulty MMSI could be reported as 0
107 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
108 if (td) {
109 wxString n0 = wxString::Format(wxT("%s"), td->ShipName);
110 n0.Replace(_T("@"), _T(" "));
111 n0.Trim();
112 wxString mmsi = wxString::Format(wxT("%i "), td->MMSI);
113 wxString n = _T("\"") + n0 + _T("\" ") + mmsi;
114 n.append(wxDateTime::Now().Format(wxT("%H:%M")));
115 // wxString n = wxString::Format(wxT("\"%s\" %i "),td->ShipName,
116 // td->MMSI).append(wxDateTime::Now().Format(wxT("%H:%M")));
117 RoutePoint *pWP =
118 new RoutePoint(td->Lat, td->Lon, g_default_wp_icon, n, wxEmptyString);
119 pWP->m_bIsolatedMark = true; // This is an isolated mark
120 pSelect->AddSelectableRoutePoint(td->Lat, td->Lon, pWP);
121 // pConfig->AddNewWayPoint(pWP, -1); // use auto next num
122 NavObj_dB::GetInstance().InsertRoutePoint(pWP);
123
124 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
125 pRouteManagerDialog->UpdateWptListCtrl();
126 gFrame->GetPrimaryCanvas()->undo->BeforeUndoableAction(
127 Undo_CreateWaypoint, pWP, Undo_HasParent, NULL);
128 gFrame->GetPrimaryCanvas()->undo->AfterUndoableAction(NULL);
129 Refresh(false);
130 }
131 }
132}
133
134void AISTargetQueryDialog::OnIdTrkCreateClick(wxCommandEvent &event) {
135 if (m_MMSI != 0) { // Faulty MMSI could be reported as 0
136 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
137 if (td) {
138 if (td->b_PersistTrack) // The target was tracked and the user wants to
139 // stop it
140 {
141 td->b_PersistTrack = false;
142 g_pAIS->m_persistent_tracks.erase(td->MMSI);
143 m_createTrkBtn->SetLabel(_("Record Track"));
144 td->b_show_track = false;
145 } else {
146 TrackPoint *tp = NULL;
147 TrackPoint *tp1 = NULL;
148
149 Track *t = new Track();
150
151 t->SetName(wxString::Format(_T("AIS %s (%u) %s %s"),
152 td->GetFullName().c_str(), td->MMSI,
153 wxDateTime::Now().FormatISODate().c_str(),
154 wxDateTime::Now().FormatISOTime().c_str()));
155 for (const AISTargetTrackPoint &ptrack_point : td->m_ptrack) {
156 vector2D point(ptrack_point.m_lon, ptrack_point.m_lat);
157 tp1 = t->AddNewPoint(point, wxDateTime(ptrack_point.m_time).ToUTC());
158 if (tp) {
159 pSelect->AddSelectableTrackSegment(tp->m_lat, tp->m_lon, tp1->m_lat,
160 tp1->m_lon, tp, tp1, t);
161 }
162 tp = tp1;
163 }
164
165 g_TrackList.push_back(t);
166 // pConfig->AddNewTrack(t);
167 NavObj_dB::GetInstance().InsertTrack(t);
168
169 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
170 pRouteManagerDialog->UpdateTrkListCtrl();
171 Refresh(false);
172
173 if (wxID_YES ==
174 OCPNMessageBox(
175 gFrame,
176 _("The recently captured track of this target has been "
177 "recorded.\nDo you want to continue recording until the end "
178 "of the current OpenCPN session?"),
179 _("OpenCPN Info"), wxYES_NO | wxCENTER, 60)) {
180 td->b_PersistTrack = true;
181 g_pAIS->m_persistent_tracks[td->MMSI] = t;
182 td->b_show_track = true;
183 }
184 }
185 }
186 }
187}
188
189bool AISTargetQueryDialog::Create(wxWindow *parent, wxWindowID id,
190 const wxString &caption, const wxPoint &pos,
191 const wxSize &size, long style) {
192 long wstyle = AIS_TARGET_QUERY_STYLE;
193
194 if (!wxFrame::Create(parent, id, caption, pos, size, wstyle)) return false;
195
196 m_parent = parent;
197
198 wxFont *dFont = FontMgr::Get().GetFont(_("AISTargetQuery"));
199 int font_size = wxMax(8, dFont->GetPointSize());
200 wxString face = dFont->GetFaceName();
201#ifdef __WXGTK__
202 face = _T("Monospace");
203#endif
204 m_basefont = FontMgr::Get().FindOrCreateFont(font_size, wxFONTFAMILY_MODERN,
205 wxFONTSTYLE_NORMAL,
206 dFont->GetWeight(), false, face);
207
208 m_adjustedFontSize = dFont->GetPointSize();
209 m_control_font_size = dFont->GetPointSize();
210
211 CreateControls();
212
213 SetColorScheme(global_color_scheme);
214
215 // Set the maximum size of the entire settings dialog
216 wxSize sz = g_Platform->getDisplaySize();
217 SetSizeHints(50, 50, sz.x - 20, sz.y - 40);
218
219 if (!m_bautosize) {
220 Fit(); // Sets the horizontal size OK
221 Layout();
222 SetSize(-1, m_adjustedFontSize * 30); // Estimated vertical size
223 }
224
225 return true;
226}
227
228void AISTargetQueryDialog::SetMMSI(int mmsi) {
229 m_MMSI = mmsi;
230
231 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
232 AdjustBestSize(td.get());
233}
234
235void AISTargetQueryDialog::RecalculateSize() {
236 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
237 AdjustBestSize(td.get());
238 return;
239}
240
241void AISTargetQueryDialog::SetColorScheme(ColorScheme cs) {
242 DimeControl(this);
243 wxColor bg = GetBackgroundColour();
244 m_pQueryTextCtl->SetBackgroundColour(bg);
245 SetBackgroundColour(
246 bg); // This looks like non-sense, but is needed for __WXGTK__
247 // to get colours to propagate down the control's family tree.
248
249#ifdef __WXQT__
250 // wxQT has some trouble clearing the background of HTML window...
251 wxBitmap tbm(GetSize().x, GetSize().y, -1);
252 wxMemoryDC tdc(tbm);
253 // wxColour cback = GetGlobalColor( _T("YELO1") );
254 tdc.SetBackground(bg);
255 tdc.Clear();
256 m_pQueryTextCtl->SetBackgroundImage(tbm);
257#endif
258
259 if (cs != m_colorscheme) {
260 Refresh();
261 }
262 m_colorscheme = cs;
263}
264
265void AISTargetQueryDialog::CreateControls() {
266 wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
267 SetSizer(topSizer);
268
269 m_pQueryTextCtl =
270 new wxHtmlWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
271 wxHW_SCROLLBAR_AUTO | wxHW_NO_SELECTION);
272 m_pQueryTextCtl->SetBorders(1);
273 m_pQueryTextCtl->SetFont(*m_basefont);
274 topSizer->Add(m_pQueryTextCtl, 1, wxALL | wxEXPAND, 5);
275
276 wxSizer *opt = new wxBoxSizer(wxHORIZONTAL);
277 m_createWptBtn = new wxButton(this, xID_WPT_CREATE, _("Create Waypoint"),
278 wxDefaultPosition, wxDefaultSize, 0);
279 opt->Add(m_createWptBtn, 0, wxALL | wxEXPAND, 5);
280
281 m_createTrkBtn = new wxButton(this, xID_TRK_CREATE, _("Record Track"),
282 wxDefaultPosition, wxDefaultSize, 0);
283 opt->Add(m_createTrkBtn, 0, wxALL | wxEXPAND, 5);
284 topSizer->Add(opt, 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
285
286 topSizer->Add(new wxButton(this, xID_OK, _("OK")), 0,
287 wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
288
289 Fit();
290}
291
292void AISTargetQueryDialog::UpdateText() {
293 wxString html;
294
295 if (!m_pQueryTextCtl) return;
296
297 int scroll_x, scroll_y;
298 m_pQueryTextCtl->GetViewStart(&scroll_x, &scroll_y);
299
300 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
301 // AdjustBestSize(td);
302
303 DimeControl(this);
304 wxColor bg = GetBackgroundColour();
305 m_pQueryTextCtl->SetBackgroundColour(bg);
306 SetBackgroundColour(bg);
307
308 if (td) {
309 if (td->b_PersistTrack)
310 m_createTrkBtn->SetLabel(_("Stop Tracking"));
311 else
312 m_createTrkBtn->SetLabel(_("Record Track"));
313
314 m_createWptBtn->Enable(td->b_positionOnceValid);
315
316 if (td->Class == AIS_METEO || td->Class == AIS_BASE)
317 m_createTrkBtn->Disable();
318 else
319 m_createTrkBtn->Enable();
320
321 RenderHTMLQuery(td.get());
322 }
323
324#ifdef __WXQT__
325 SetColorScheme(m_colorscheme);
326#endif
327
328 m_pQueryTextCtl->Scroll(scroll_x, scroll_y);
329}
330
331void AISTargetQueryDialog::OnMove(wxMoveEvent &event) {
332 // Record the dialog position
333 wxPoint p = event.GetPosition();
334 g_ais_query_dialog_x = p.x;
335 g_ais_query_dialog_y = p.y;
336
337 event.Skip();
338}
339
340void AISTargetQueryDialog::OnSize(wxSizeEvent &event) { event.Skip(); }
341
342void AISTargetQueryDialog::AdjustBestSize(AisTargetData *td) {
343 if (!td) return;
344
345 wxSize origSize = GetSize();
346
347 Fit();
348 RenderHTMLQuery(td);
349
350 int target_x = -1;
351 int target_y = -1;
352
353 // Width adjustments
354
355 if (m_bautosize) {
356 // Reduce the font size if necessary to eliminate horizontal scroll bars.
357 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
358 if (szv.x > m_pQueryTextCtl->GetSize().x) {
359 while ((szv.x > m_pQueryTextCtl->GetSize().x) &&
360 (m_adjustedFontSize > 8)) { // fluff
361 m_adjustedFontSize--;
362
363 RenderHTMLQuery(td);
364 m_pQueryTextCtl->Refresh();
365 m_pQueryTextCtl->Update();
366 Layout();
367 szv = m_pQueryTextCtl->GetVirtualSize();
368 }
369
370 m_adjustedFontSize--;
371 }
372 target_x = szv.x * 12 /
373 10; // Making the winfow a bit wider than absolutely nesessary
374 // gives a little better results in real world
375 } else {
376 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
377 int csz = g_Platform->getDisplaySize().x * 8 / 10;
378 if ((szv.x) < csz) {
379 if (szv.x > m_pQueryTextCtl->GetSize().x) target_x = szv.x; // * 11/10;
380 }
381 target_x = szv.x * 12 /
382 10; // Making the winfow a bit wider than absolutely nesessary
383 // gives a little better results in real world
384 }
385
386#ifdef __ANDROID__
387 // Now adjust the font size used for the control buttons.
388 // This adjustment makes sure that the two horizontal buttons are not wider
389 // than the platform display allows. This may be a problem on phones,
390 // but probably never on normal computer displays. some platforms also don't
391 // support this at all
392
393 if (m_createWptBtn && m_createTrkBtn) {
394 wxSize psz = g_Platform->getDisplaySize();
395
396 wxScreenDC dc;
397 wxFont *tFont = FontMgr::Get().FindOrCreateFont(
398 m_control_font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
399 m_basefont->GetWeight(), false, m_basefont->GetFaceName());
400 dc.SetFont(*tFont);
401
402 wxSize tsz = dc.GetTextExtent(m_createWptBtn->GetLabel() +
403 m_createTrkBtn->GetLabel());
404
405 float totalButtonWidth = tsz.x;
406
407 if (totalButtonWidth * 1.5 > psz.x) {
408 float delta = (float)totalButtonWidth * 2. / psz.x;
409
410 float font_size = m_control_font_size / delta;
411
412 wxFont *fp_font = FontMgr::Get().FindOrCreateFont(
413 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
414 m_basefont->GetWeight(), false, m_basefont->GetFaceName());
415
416 m_createWptBtn->SetFont(*fp_font);
417 m_createTrkBtn->SetFont(*fp_font);
418
419 m_control_font_size = font_size;
420 }
421 }
422#endif
423
424 // Height adjustments
425 // Try to avoid vertical scroll bar if possible.
426
427 // Estimate the control button area height
428 int yb = 0;
429 if (m_createWptBtn) yb = m_createWptBtn->GetSize().y * 4;
430
431 wxSize szyv = m_pQueryTextCtl->GetVirtualSize();
432 int csz = g_Platform->getDisplaySize().y * 85 / 100;
433 if ((szyv.y + yb) < csz) {
434 if (szyv.y > m_pQueryTextCtl->GetSize().y) target_y = szyv.y * 12 / 10 + yb;
435 } else {
436 target_y = csz;
437 }
438 SetSize(target_x, target_y);
439
440 wxSize nowSize = GetSize();
441
442 if (nowSize != origSize) {
443 if (m_bautoCentre) Centre();
444 }
445}
446
447void AISTargetQueryDialog::RenderHTMLQuery(AisTargetData *td) {
448 int font_size = m_adjustedFontSize;
449 wxFont *fp_font = FontMgr::Get().FindOrCreateFont(
450 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
451 m_basefont->GetWeight(), false, m_basefont->GetFaceName());
452
453 SetFont(*fp_font);
454
455 int sizes[7];
456 for (int i = -2; i < 5; i++) {
457 sizes[i + 2] = fp_font->GetPointSize() + i + (i > 0 ? i : 0);
458 }
459
460 wxString html;
461 wxColor bg = GetBackgroundColour();
462 wxColor fg = GetForegroundColour();
463
464 html.Printf(
465 _T("<html><body bgcolor=#%02x%02x%02x><font ")
466 _T("color=#%02x%02x%02x><center>"),
467 bg.Red(), bg.Green(), bg.Blue(), fg.Red(), fg.Green(), fg.Blue());
468
469 html << td->BuildQueryResult();
470
471 html << _T("</center></font></body></html>");
472
473 m_pQueryTextCtl->SetFonts(fp_font->GetFaceName(), fp_font->GetFaceName(),
474 sizes);
475
476 wxCharBuffer buf = html.ToUTF8();
477 if (buf.data()) // string OK?
478 m_pQueryTextCtl->SetPage(html);
479}
Global state for AIS decoder.
Dialog for querying detailed information about an AIS target.
AISTargetQueryDialog()
Constructors.
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &caption=_("Object Query"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=AIS_TARGET_QUERY_STYLE)
Creation.
void Init()
Initialise our variables.
wxFont * FindOrCreateFont(int point_size, wxFontFamily family, wxFontStyle style, wxFontWeight weight, bool underline=false, const wxString &facename=wxEmptyString, wxFontEncoding encoding=wxFONTENCODING_DEFAULT)
Creates or finds a matching font in the font cache.
Definition FontMgr.cpp:450
wxFont * GetFont(const wxString &TextElement, int requested_font_size=0)
Gets a font object for a UI element.
Definition FontMgr.cpp:186
Main application frame.
Definition ocpn_frame.h:136
Provides platform-specific support utilities for OpenCPN.
wxSize getDisplaySize()
Get the display size in logical pixels.
Represents a waypoint or mark within the navigation system.
Definition route_point.h:70
bool m_bIsolatedMark
Flag indicating if the waypoint is a standalone mark.
Represents a single point in a track.
Definition track.h:53
Represents a track, which is a series of connected track points.
Definition track.h:111
Class NavObj_dB.