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 = td->b_show_track_old;
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_old = td->b_show_track;
183 td->b_show_track = true;
184 }
185 }
186 }
187 }
188}
189
190bool AISTargetQueryDialog::Create(wxWindow *parent, wxWindowID id,
191 const wxString &caption, const wxPoint &pos,
192 const wxSize &size, long style) {
193 long wstyle = AIS_TARGET_QUERY_STYLE;
194
195 if (!wxFrame::Create(parent, id, caption, pos, size, wstyle)) return false;
196
197 m_parent = parent;
198
199 wxFont *dFont = FontMgr::Get().GetFont(_("AISTargetQuery"));
200 int font_size = wxMax(8, dFont->GetPointSize());
201 wxString face = dFont->GetFaceName();
202#ifdef __WXGTK__
203 face = _T("Monospace");
204#endif
205 m_basefont = FontMgr::Get().FindOrCreateFont(font_size, wxFONTFAMILY_MODERN,
206 wxFONTSTYLE_NORMAL,
207 dFont->GetWeight(), false, face);
208 m_adjustedFontSize = dFont->GetPointSize();
209 m_control_font_size = dFont->GetPointSize();
210 SetFont(*m_basefont);
211
212 CreateControls();
213
214 SetColorScheme(global_color_scheme);
215
216 // Set the maximum size of the entire settings dialog
217 wxSize sz = g_Platform->getDisplaySize();
218 SetSizeHints(50, 50, sz.x - 20, sz.y - 40);
219
220 if (!m_bautosize) {
221 Fit(); // Sets the horizontal size OK
222 Layout();
223 SetSize(-1, m_adjustedFontSize * 30); // Estimated vertical size
224 }
225
226 return true;
227}
228
229void AISTargetQueryDialog::SetMMSI(int mmsi) {
230 m_MMSI = mmsi;
231
232 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
233 AdjustBestSize(td.get());
234}
235
236void AISTargetQueryDialog::RecalculateSize() {
237 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
238 AdjustBestSize(td.get());
239 return;
240}
241
242void AISTargetQueryDialog::SetColorScheme(ColorScheme cs) {
243 DimeControl(this);
244 wxColor bg = GetBackgroundColour();
245 m_pQueryTextCtl->SetBackgroundColour(bg);
246 SetBackgroundColour(
247 bg); // This looks like non-sense, but is needed for __WXGTK__
248 // to get colours to propagate down the control's family tree.
249
250#ifdef __WXQT__
251 // wxQT has some trouble clearing the background of HTML window...
252 wxBitmap tbm(GetSize().x, GetSize().y, -1);
253 wxMemoryDC tdc(tbm);
254 // wxColour cback = GetGlobalColor( _T("YELO1") );
255 tdc.SetBackground(bg);
256 tdc.Clear();
257 m_pQueryTextCtl->SetBackgroundImage(tbm);
258#endif
259
260 if (cs != m_colorscheme) {
261 Refresh();
262 }
263 m_colorscheme = cs;
264}
265
266void AISTargetQueryDialog::CreateControls() {
267 wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
268 SetSizer(topSizer);
269
270 m_pQueryTextCtl =
271 new wxHtmlWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
272 wxHW_SCROLLBAR_AUTO | wxHW_NO_SELECTION);
273 m_pQueryTextCtl->SetBorders(1);
274 m_pQueryTextCtl->SetFont(*m_basefont);
275 topSizer->Add(m_pQueryTextCtl, 1, wxALL | wxEXPAND, 5);
276
277 wxSizer *opt = new wxBoxSizer(wxHORIZONTAL);
278 m_createWptBtn = new wxButton(this, xID_WPT_CREATE, _("Create Waypoint"),
279 wxDefaultPosition, wxDefaultSize, 0);
280 opt->Add(m_createWptBtn, 0, wxALL | wxEXPAND, 5);
281
282 m_createTrkBtn = new wxButton(this, xID_TRK_CREATE, _("Record Track"),
283 wxDefaultPosition, wxDefaultSize, 0);
284 opt->Add(m_createTrkBtn, 0, wxALL | wxEXPAND, 5);
285 topSizer->Add(opt, 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
286
287 topSizer->Add(new wxButton(this, xID_OK, _("OK")), 0,
288 wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
289
290 Fit();
291}
292
293void AISTargetQueryDialog::UpdateText() {
294 wxString html;
295
296 if (!m_pQueryTextCtl) return;
297
298 int scroll_x, scroll_y;
299 m_pQueryTextCtl->GetViewStart(&scroll_x, &scroll_y);
300
301 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
302 // AdjustBestSize(td);
303
304 DimeControl(this);
305 wxColor bg = GetBackgroundColour();
306 m_pQueryTextCtl->SetBackgroundColour(bg);
307 SetBackgroundColour(bg);
308
309 if (td) {
310 if (td->b_PersistTrack)
311 m_createTrkBtn->SetLabel(_("Save Track"));
312 else
313 m_createTrkBtn->SetLabel(_("Record Track"));
314
315 m_createWptBtn->Enable(td->b_positionOnceValid);
316
317 if (td->Class == AIS_METEO || td->Class == AIS_BASE)
318 m_createTrkBtn->Disable();
319 else
320 m_createTrkBtn->Enable();
321
322 RenderHTMLQuery(td.get());
323 }
324
325#ifdef __WXQT__
326 SetColorScheme(m_colorscheme);
327#endif
328
329 m_pQueryTextCtl->Scroll(scroll_x, scroll_y);
330}
331
332void AISTargetQueryDialog::OnMove(wxMoveEvent &event) {
333 // Record the dialog position
334 wxPoint p = event.GetPosition();
335 g_ais_query_dialog_x = p.x;
336 g_ais_query_dialog_y = p.y;
337
338 event.Skip();
339}
340
341void AISTargetQueryDialog::OnSize(wxSizeEvent &event) { event.Skip(); }
342
343void AISTargetQueryDialog::AdjustBestSize(AisTargetData *td) {
344 if (!td) return;
345
346 wxSize origSize = GetSize();
347
348 Fit();
349 RenderHTMLQuery(td);
350
351 int target_x = -1;
352 int target_y = -1;
353
354 // Width adjustments
355
356 if (m_bautosize) {
357 // Reduce the font size if necessary to eliminate horizontal scroll bars.
358 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
359 if (szv.x > m_pQueryTextCtl->GetSize().x) {
360 while ((szv.x > m_pQueryTextCtl->GetSize().x) &&
361 (m_adjustedFontSize > 8)) { // fluff
362 m_adjustedFontSize--;
363
364 RenderHTMLQuery(td);
365 m_pQueryTextCtl->Refresh();
366 m_pQueryTextCtl->Update();
367 Layout();
368 szv = m_pQueryTextCtl->GetVirtualSize();
369 }
370
371 m_adjustedFontSize--;
372 }
373 target_x = szv.x * 12 /
374 10; // Making the winfow a bit wider than absolutely nesessary
375 // gives a little better results in real world
376 } else {
377 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
378 int csz = g_Platform->getDisplaySize().x * 8 / 10;
379 if ((szv.x) < csz) {
380 if (szv.x > m_pQueryTextCtl->GetSize().x) target_x = szv.x; // * 11/10;
381 }
382 target_x = szv.x * 12 /
383 10; // Making the winfow a bit wider than absolutely nesessary
384 // gives a little better results in real world
385 }
386
387#ifdef __ANDROID__
388 // Now adjust the font size used for the control buttons.
389 // This adjustment makes sure that the two horizontal buttons are not wider
390 // than the platform display allows. This may be a problem on phones,
391 // but probably never on normal computer displays. some platforms also don't
392 // support this at all
393
394 if (m_createWptBtn && m_createTrkBtn) {
395 wxSize psz = g_Platform->getDisplaySize();
396
397 wxScreenDC dc;
398 wxFont *tFont = FontMgr::Get().FindOrCreateFont(
399 m_control_font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
400 m_basefont->GetWeight(), false, m_basefont->GetFaceName());
401 dc.SetFont(*tFont);
402
403 wxSize tsz = dc.GetTextExtent(m_createWptBtn->GetLabel() +
404 m_createTrkBtn->GetLabel());
405
406 float totalButtonWidth = tsz.x;
407
408 if (totalButtonWidth * 1.5 > psz.x) {
409 float delta = (float)totalButtonWidth * 2. / psz.x;
410
411 float font_size = m_control_font_size / delta;
412
413 wxFont *fp_font = FontMgr::Get().FindOrCreateFont(
414 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
415 m_basefont->GetWeight(), false, m_basefont->GetFaceName());
416
417 m_createWptBtn->SetFont(*fp_font);
418 m_createTrkBtn->SetFont(*fp_font);
419
420 m_control_font_size = font_size;
421 }
422 }
423#endif
424
425 // Height adjustments
426 // Try to avoid vertical scroll bar if possible.
427
428 // Estimate the control button area height
429 int yb = 0;
430 if (m_createWptBtn) yb = m_createWptBtn->GetSize().y * 4;
431
432 wxSize szyv = m_pQueryTextCtl->GetVirtualSize();
433 int csz = g_Platform->getDisplaySize().y * 85 / 100;
434 if ((szyv.y + yb) < csz) {
435 if (szyv.y > m_pQueryTextCtl->GetSize().y) target_y = szyv.y * 12 / 10 + yb;
436 } else {
437 target_y = csz;
438 }
439 SetSize(target_x, target_y);
440
441 wxSize nowSize = GetSize();
442
443 if (nowSize != origSize) {
444 if (m_bautoCentre) Centre();
445 }
446}
447
448void AISTargetQueryDialog::RenderHTMLQuery(AisTargetData *td) {
449 int font_size = m_adjustedFontSize;
450 wxFont *fp_font = FontMgr::Get().FindOrCreateFont(
451 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
452 m_basefont->GetWeight(), false, m_basefont->GetFaceName());
453
454 SetFont(*fp_font);
455
456 int sizes[7];
457 for (int i = -2; i < 5; i++) {
458 sizes[i + 2] = fp_font->GetPointSize() + i + (i > 0 ? i : 0);
459 }
460
461 wxString html;
462 wxColor bg = GetBackgroundColour();
463 wxColor fg = GetForegroundColour();
464
465 html.Printf(
466 _T("<html><body bgcolor=#%02x%02x%02x><font ")
467 _T("color=#%02x%02x%02x><center>"),
468 bg.Red(), bg.Green(), bg.Blue(), fg.Red(), fg.Green(), fg.Blue());
469
470 html << td->BuildQueryResult();
471
472 html << _T("</center></font></body></html>");
473
474 m_pQueryTextCtl->SetFonts(fp_font->GetFaceName(), fp_font->GetFaceName(),
475 sizes);
476
477 wxCharBuffer buf = html.ToUTF8();
478 if (buf.data()) // string OK?
479 m_pQueryTextCtl->SetPage(html);
480}
Class AisDecoder and helpers.
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:467
wxFont * GetFont(const wxString &TextElement, int requested_font_size=0)
Gets a font object for a UI element.
Definition FontMgr.cpp:203
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.