27#include <wx/html/htmlwin.h>
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"
37#include "AISTargetQueryDialog.h"
41#include "ocpn_frame.h"
42#include "OCPNPlatform.h"
43#include "routemanagerdialog.h"
46extern ColorScheme global_color_scheme;
47extern wxString g_default_wp_icon;
50extern std::vector<Track *> g_TrackList;
55#define xID_WPT_CREATE 10010
56#define xID_TRK_CREATE 10011
60EVT_BUTTON(xID_OK, AISTargetQueryDialog::OnIdOKClick)
61EVT_BUTTON(xID_WPT_CREATE, AISTargetQueryDialog::OnIdWptCreateClick)
62EVT_BUTTON(xID_TRK_CREATE, AISTargetQueryDialog::OnIdTrkCreateClick)
63EVT_CLOSE(AISTargetQueryDialog::OnClose)
64EVT_MOVE(AISTargetQueryDialog::OnMove)
65EVT_SIZE(AISTargetQueryDialog::OnSize)
66EVT_CHAR_HOOK(AISTargetQueryDialog::OnKey)
72 const wxString &caption,
74 const wxSize &size,
long style) {
76 Create(parent,
id, caption, pos, size, style);
79AISTargetQueryDialog::~AISTargetQueryDialog() {
delete m_pQueryTextCtl; }
83 m_pQueryTextCtl = NULL;
85 m_colorscheme = (ColorScheme)(-1);
87 m_bautoCentre =
false;
90void AISTargetQueryDialog::OnClose(wxCloseEvent &event) {
92 g_pais_query_dialog_active = NULL;
95void AISTargetQueryDialog::OnIdOKClick(wxCommandEvent &event) { Close(); }
97void AISTargetQueryDialog::OnKey(wxKeyEvent &ke) {
98 if (ke.GetKeyCode() == WXK_ESCAPE)
104void AISTargetQueryDialog::OnIdWptCreateClick(wxCommandEvent &event) {
106 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
108 wxString n0 = wxString::Format(wxT(
"%s"), td->ShipName);
109 n0.Replace(_T(
"@"), _T(
" "));
111 wxString mmsi = wxString::Format(wxT(
"%i "), td->MMSI);
112 wxString n = _T(
"\"") + n0 + _T(
"\" ") + mmsi;
113 n.append(wxDateTime::Now().Format(wxT(
"%H:%M")));
117 new RoutePoint(td->Lat, td->Lon, g_default_wp_icon, n, wxEmptyString);
118 pWP->m_bIsolatedMark =
true;
119 pSelect->AddSelectableRoutePoint(td->Lat, td->Lon, pWP);
120 pConfig->AddNewWayPoint(pWP, -1);
122 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
123 pRouteManagerDialog->UpdateWptListCtrl();
124 gFrame->GetPrimaryCanvas()->undo->BeforeUndoableAction(
125 Undo_CreateWaypoint, pWP, Undo_HasParent, NULL);
126 gFrame->GetPrimaryCanvas()->undo->AfterUndoableAction(NULL);
132void AISTargetQueryDialog::OnIdTrkCreateClick(wxCommandEvent &event) {
134 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
136 if (td->b_PersistTrack)
139 td->b_PersistTrack =
false;
140 g_pAIS->m_persistent_tracks.erase(td->MMSI);
141 m_createTrkBtn->SetLabel(_(
"Record Track"));
142 td->b_show_track =
false;
149 t->SetName(wxString::Format(_T(
"AIS %s (%u) %s %s"),
150 td->GetFullName().c_str(), td->MMSI,
151 wxDateTime::Now().FormatISODate().c_str(),
152 wxDateTime::Now().FormatISOTime().c_str()));
154 vector2D point(ptrack_point.m_lon, ptrack_point.m_lat);
155 tp1 = t->AddNewPoint(point, wxDateTime(ptrack_point.m_time).ToUTC());
157 pSelect->AddSelectableTrackSegment(tp->m_lat, tp->m_lon, tp1->m_lat,
158 tp1->m_lon, tp, tp1, t);
163 g_TrackList.push_back(t);
164 pConfig->AddNewTrack(t);
168 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
169 pRouteManagerDialog->UpdateTrkListCtrl();
175 _(
"The recently captured track of this target has been "
176 "recorded.\nDo you want to continue recording until the end "
177 "of the current OpenCPN session?"),
178 _(
"OpenCPN Info"), wxYES_NO | wxCENTER, 60)) {
179 td->b_PersistTrack =
true;
180 g_pAIS->m_persistent_tracks[td->MMSI] = t;
181 td->b_show_track =
true;
189 const wxString &caption,
const wxPoint &pos,
190 const wxSize &size,
long style) {
191 long wstyle = AIS_TARGET_QUERY_STYLE;
193 if (!wxFrame::Create(parent,
id, caption, pos, size, wstyle))
return false;
197 wxFont *dFont = FontMgr::Get().
GetFont(_(
"AISTargetQuery"));
198 int font_size = wxMax(8, dFont->GetPointSize());
199 wxString face = dFont->GetFaceName();
201 face = _T(
"Monospace");
203 m_basefont = FontMgr::Get().
FindOrCreateFont(font_size, wxFONTFAMILY_MODERN,
205 dFont->GetWeight(),
false, face);
207 m_adjustedFontSize = dFont->GetPointSize();
208 m_control_font_size = dFont->GetPointSize();
212 SetColorScheme(global_color_scheme);
216 SetSizeHints(50, 50, sz.x - 20, sz.y - 40);
221 SetSize(-1, m_adjustedFontSize * 30);
227void AISTargetQueryDialog::SetMMSI(
int mmsi) {
230 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
231 AdjustBestSize(td.get());
234void AISTargetQueryDialog::RecalculateSize() {
235 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
236 AdjustBestSize(td.get());
240void AISTargetQueryDialog::SetColorScheme(ColorScheme cs) {
242 wxColor bg = GetBackgroundColour();
243 m_pQueryTextCtl->SetBackgroundColour(bg);
250 wxBitmap tbm(GetSize().x, GetSize().y, -1);
253 tdc.SetBackground(bg);
255 m_pQueryTextCtl->SetBackgroundImage(tbm);
258 if (cs != m_colorscheme) {
264void AISTargetQueryDialog::CreateControls() {
265 wxBoxSizer *topSizer =
new wxBoxSizer(wxVERTICAL);
269 new wxHtmlWindow(
this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
270 wxHW_SCROLLBAR_AUTO | wxHW_NO_SELECTION);
271 m_pQueryTextCtl->SetBorders(1);
272 m_pQueryTextCtl->SetFont(*m_basefont);
273 topSizer->Add(m_pQueryTextCtl, 1, wxALL | wxEXPAND, 5);
275 wxSizer *opt =
new wxBoxSizer(wxHORIZONTAL);
276 m_createWptBtn =
new wxButton(
this, xID_WPT_CREATE, _(
"Create Waypoint"),
277 wxDefaultPosition, wxDefaultSize, 0);
278 opt->Add(m_createWptBtn, 0, wxALL | wxEXPAND, 5);
280 m_createTrkBtn =
new wxButton(
this, xID_TRK_CREATE, _(
"Record Track"),
281 wxDefaultPosition, wxDefaultSize, 0);
282 opt->Add(m_createTrkBtn, 0, wxALL | wxEXPAND, 5);
283 topSizer->Add(opt, 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
285 topSizer->Add(
new wxButton(
this, xID_OK, _(
"OK")), 0,
286 wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
291void AISTargetQueryDialog::UpdateText() {
294 if (!m_pQueryTextCtl)
return;
296 int scroll_x, scroll_y;
297 m_pQueryTextCtl->GetViewStart(&scroll_x, &scroll_y);
299 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
303 wxColor bg = GetBackgroundColour();
304 m_pQueryTextCtl->SetBackgroundColour(bg);
305 SetBackgroundColour(bg);
308 if (td->b_PersistTrack)
309 m_createTrkBtn->SetLabel(_(
"Stop Tracking"));
311 m_createTrkBtn->SetLabel(_(
"Record Track"));
313 m_createWptBtn->Enable(td->b_positionOnceValid);
315 if (td->Class == AIS_METEO || td->Class == AIS_BASE)
316 m_createTrkBtn->Disable();
318 m_createTrkBtn->Enable();
320 RenderHTMLQuery(td.get());
324 SetColorScheme(m_colorscheme);
327 m_pQueryTextCtl->Scroll(scroll_x, scroll_y);
330void AISTargetQueryDialog::OnMove(wxMoveEvent &event) {
332 wxPoint p =
event.GetPosition();
333 g_ais_query_dialog_x = p.x;
334 g_ais_query_dialog_y = p.y;
339void AISTargetQueryDialog::OnSize(wxSizeEvent &event) {
event.Skip(); }
341void AISTargetQueryDialog::AdjustBestSize(
AisTargetData *td) {
344 wxSize origSize = GetSize();
356 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
357 if (szv.x > m_pQueryTextCtl->GetSize().x) {
358 while ((szv.x > m_pQueryTextCtl->GetSize().x) &&
359 (m_adjustedFontSize > 8)) {
360 m_adjustedFontSize--;
363 m_pQueryTextCtl->Refresh();
364 m_pQueryTextCtl->Update();
366 szv = m_pQueryTextCtl->GetVirtualSize();
369 m_adjustedFontSize--;
371 target_x = szv.x * 12 /
375 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
378 if (szv.x > m_pQueryTextCtl->GetSize().x) target_x = szv.x;
380 target_x = szv.x * 12 /
392 if (m_createWptBtn && m_createTrkBtn) {
397 m_control_font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
398 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
401 wxSize tsz = dc.GetTextExtent(m_createWptBtn->GetLabel() +
402 m_createTrkBtn->GetLabel());
404 float totalButtonWidth = tsz.x;
406 if (totalButtonWidth * 1.5 > psz.x) {
407 float delta = (float)totalButtonWidth * 2. / psz.x;
409 float font_size = m_control_font_size / delta;
412 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
413 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
415 m_createWptBtn->SetFont(*fp_font);
416 m_createTrkBtn->SetFont(*fp_font);
418 m_control_font_size = font_size;
428 if (m_createWptBtn) yb = m_createWptBtn->GetSize().y * 4;
430 wxSize szyv = m_pQueryTextCtl->GetVirtualSize();
432 if ((szyv.y + yb) < csz) {
433 if (szyv.y > m_pQueryTextCtl->GetSize().y) target_y = szyv.y * 12 / 10 + yb;
437 SetSize(target_x, target_y);
439 wxSize nowSize = GetSize();
441 if (nowSize != origSize) {
442 if (m_bautoCentre) Centre();
446void AISTargetQueryDialog::RenderHTMLQuery(
AisTargetData *td) {
447 int font_size = m_adjustedFontSize;
449 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
450 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
455 for (
int i = -2; i < 5; i++) {
456 sizes[i + 2] = fp_font->GetPointSize() + i + (i > 0 ? i : 0);
460 wxColor bg = GetBackgroundColour();
461 wxColor fg = GetForegroundColour();
464 _T(
"<html><body bgcolor=#%02x%02x%02x><font ")
465 _T(
"color=#%02x%02x%02x><center>"),
466 bg.Red(), bg.Green(), bg.Blue(), fg.Red(), fg.Green(), fg.Blue());
468 html << td->BuildQueryResult();
470 html << _T(
"</center></font></body></html>");
472 m_pQueryTextCtl->SetFonts(fp_font->GetFaceName(), fp_font->GetFaceName(),
475 wxCharBuffer buf = html.ToUTF8();
477 m_pQueryTextCtl->SetPage(html);
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.
wxFont * GetFont(const wxString &TextElement, int requested_font_size=0)
Gets a font object for a UI element.
Represents a single point in a track.
Represents a track, which is a series of connected track points.