27#include <wx/html/htmlwin.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"
47extern ColorScheme global_color_scheme;
48extern wxString g_default_wp_icon;
51extern std::vector<Track *> g_TrackList;
56#define xID_WPT_CREATE 10010
57#define xID_TRK_CREATE 10011
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)
73 const wxString &caption,
75 const wxSize &size,
long style) {
77 Create(parent,
id, caption, pos, size, style);
80AISTargetQueryDialog::~AISTargetQueryDialog() {
delete m_pQueryTextCtl; }
84 m_pQueryTextCtl = NULL;
86 m_colorscheme = (ColorScheme)(-1);
88 m_bautoCentre =
false;
91void AISTargetQueryDialog::OnClose(wxCloseEvent &event) {
93 g_pais_query_dialog_active = NULL;
96void AISTargetQueryDialog::OnIdOKClick(wxCommandEvent &event) { Close(); }
98void AISTargetQueryDialog::OnKey(wxKeyEvent &ke) {
99 if (ke.GetKeyCode() == WXK_ESCAPE)
105void AISTargetQueryDialog::OnIdWptCreateClick(wxCommandEvent &event) {
107 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
109 wxString n0 = wxString::Format(wxT(
"%s"), td->ShipName);
110 n0.Replace(_T(
"@"), _T(
" "));
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")));
118 new RoutePoint(td->Lat, td->Lon, g_default_wp_icon, n, wxEmptyString);
120 pSelect->AddSelectableRoutePoint(td->Lat, td->Lon, pWP);
122 NavObj_dB::GetInstance().InsertRoutePoint(pWP);
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);
134void AISTargetQueryDialog::OnIdTrkCreateClick(wxCommandEvent &event) {
136 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
138 if (td->b_PersistTrack)
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;
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()));
156 vector2D point(ptrack_point.m_lon, ptrack_point.m_lat);
157 tp1 = t->AddNewPoint(point, wxDateTime(ptrack_point.m_time).ToUTC());
159 pSelect->AddSelectableTrackSegment(tp->m_lat, tp->m_lon, tp1->m_lat,
160 tp1->m_lon, tp, tp1, t);
165 g_TrackList.push_back(t);
167 NavObj_dB::GetInstance().InsertTrack(t);
169 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
170 pRouteManagerDialog->UpdateTrkListCtrl();
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;
191 const wxString &caption,
const wxPoint &pos,
192 const wxSize &size,
long style) {
193 long wstyle = AIS_TARGET_QUERY_STYLE;
195 if (!wxFrame::Create(parent,
id, caption, pos, size, wstyle))
return false;
199 wxFont *dFont = FontMgr::Get().
GetFont(_(
"AISTargetQuery"));
200 int font_size = wxMax(8, dFont->GetPointSize());
201 wxString face = dFont->GetFaceName();
203 face = _T(
"Monospace");
205 m_basefont = FontMgr::Get().
FindOrCreateFont(font_size, wxFONTFAMILY_MODERN,
207 dFont->GetWeight(),
false, face);
208 m_adjustedFontSize = dFont->GetPointSize();
209 m_control_font_size = dFont->GetPointSize();
210 SetFont(*m_basefont);
214 SetColorScheme(global_color_scheme);
218 SetSizeHints(50, 50, sz.x - 20, sz.y - 40);
223 SetSize(-1, m_adjustedFontSize * 30);
229void AISTargetQueryDialog::SetMMSI(
int mmsi) {
232 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
233 AdjustBestSize(td.get());
236void AISTargetQueryDialog::RecalculateSize() {
237 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
238 AdjustBestSize(td.get());
242void AISTargetQueryDialog::SetColorScheme(ColorScheme cs) {
244 wxColor bg = GetBackgroundColour();
245 m_pQueryTextCtl->SetBackgroundColour(bg);
252 wxBitmap tbm(GetSize().x, GetSize().y, -1);
255 tdc.SetBackground(bg);
257 m_pQueryTextCtl->SetBackgroundImage(tbm);
260 if (cs != m_colorscheme) {
266void AISTargetQueryDialog::CreateControls() {
267 wxBoxSizer *topSizer =
new wxBoxSizer(wxVERTICAL);
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);
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);
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);
287 topSizer->Add(
new wxButton(
this, xID_OK, _(
"OK")), 0,
288 wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
293void AISTargetQueryDialog::UpdateText() {
296 if (!m_pQueryTextCtl)
return;
298 int scroll_x, scroll_y;
299 m_pQueryTextCtl->GetViewStart(&scroll_x, &scroll_y);
301 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
305 wxColor bg = GetBackgroundColour();
306 m_pQueryTextCtl->SetBackgroundColour(bg);
307 SetBackgroundColour(bg);
310 if (td->b_PersistTrack)
311 m_createTrkBtn->SetLabel(_(
"Save Track"));
313 m_createTrkBtn->SetLabel(_(
"Record Track"));
315 m_createWptBtn->Enable(td->b_positionOnceValid);
317 if (td->Class == AIS_METEO || td->Class == AIS_BASE)
318 m_createTrkBtn->Disable();
320 m_createTrkBtn->Enable();
322 RenderHTMLQuery(td.get());
326 SetColorScheme(m_colorscheme);
329 m_pQueryTextCtl->Scroll(scroll_x, scroll_y);
332void AISTargetQueryDialog::OnMove(wxMoveEvent &event) {
334 wxPoint p =
event.GetPosition();
335 g_ais_query_dialog_x = p.x;
336 g_ais_query_dialog_y = p.y;
341void AISTargetQueryDialog::OnSize(wxSizeEvent &event) {
event.Skip(); }
343void AISTargetQueryDialog::AdjustBestSize(
AisTargetData *td) {
346 wxSize origSize = GetSize();
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)) {
362 m_adjustedFontSize--;
365 m_pQueryTextCtl->Refresh();
366 m_pQueryTextCtl->Update();
368 szv = m_pQueryTextCtl->GetVirtualSize();
371 m_adjustedFontSize--;
373 target_x = szv.x * 12 /
377 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
380 if (szv.x > m_pQueryTextCtl->GetSize().x) target_x = szv.x;
382 target_x = szv.x * 12 /
394 if (m_createWptBtn && m_createTrkBtn) {
399 m_control_font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
400 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
403 wxSize tsz = dc.GetTextExtent(m_createWptBtn->GetLabel() +
404 m_createTrkBtn->GetLabel());
406 float totalButtonWidth = tsz.x;
408 if (totalButtonWidth * 1.5 > psz.x) {
409 float delta = (float)totalButtonWidth * 2. / psz.x;
411 float font_size = m_control_font_size / delta;
414 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
415 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
417 m_createWptBtn->SetFont(*fp_font);
418 m_createTrkBtn->SetFont(*fp_font);
420 m_control_font_size = font_size;
430 if (m_createWptBtn) yb = m_createWptBtn->GetSize().y * 4;
432 wxSize szyv = m_pQueryTextCtl->GetVirtualSize();
434 if ((szyv.y + yb) < csz) {
435 if (szyv.y > m_pQueryTextCtl->GetSize().y) target_y = szyv.y * 12 / 10 + yb;
439 SetSize(target_x, target_y);
441 wxSize nowSize = GetSize();
443 if (nowSize != origSize) {
444 if (m_bautoCentre) Centre();
448void AISTargetQueryDialog::RenderHTMLQuery(
AisTargetData *td) {
449 int font_size = m_adjustedFontSize;
451 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
452 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
457 for (
int i = -2; i < 5; i++) {
458 sizes[i + 2] = fp_font->GetPointSize() + i + (i > 0 ? i : 0);
462 wxColor bg = GetBackgroundColour();
463 wxColor fg = GetForegroundColour();
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());
470 html << td->BuildQueryResult();
472 html << _T(
"</center></font></body></html>");
474 m_pQueryTextCtl->SetFonts(fp_font->GetFaceName(), fp_font->GetFaceName(),
477 wxCharBuffer buf = html.ToUTF8();
479 m_pQueryTextCtl->SetPage(html);
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.
wxFont * GetFont(const wxString &TextElement, int requested_font_size=0)
Gets a font object for a UI element.
Represents a waypoint or mark within the navigation system.
bool m_bIsolatedMark
Flag indicating if the waypoint is a standalone mark.
Represents a single point in a track.
Represents a track, which is a series of connected track points.