OpenCPN Partial API docs
Loading...
Searching...
No Matches
route_printout.cpp
1/***************************************************************************
2 * Copyright (C) 2012 by David S. Register *
3 * Copyright (C) 2025 by NoCodeHummel *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 **************************************************************************/
20
21#include <iostream>
22
23#include <wx/wxprec.h>
24
25#ifndef WX_PRECOMP
26#include <wx/wx.h>
27#endif // precompiled headers
28#ifdef __WXMSW__
29// #include "c:\\Program Files\\visual leak detector\\include\\vld.h"
30#endif
31
32#include <wx/print.h>
33#include <wx/printdlg.h>
34#include <wx/artprov.h>
35#include <wx/stdpaths.h>
36#include <wx/intl.h>
37#include <wx/listctrl.h>
38#include <wx/aui/aui.h>
39#include <wx/dialog.h>
40#include <wx/progdlg.h>
41#include <wx/brush.h>
42#include <wx/colour.h>
43#include <wx/dialog.h>
44
45#ifdef __WXMSW__
46#include <stdlib.h>
47#include <math.h>
48#include <time.h>
49#include <psapi.h>
50#endif
51
52#ifndef __WXMSW__
53#include <signal.h>
54#include <setjmp.h>
55#endif
56
57#include "dychart.h"
58#include "gui_lib.h"
59#include "model/navutil_base.h"
60#include "model/route.h"
61#include "model/track.h"
62#include "model/wx28compat.h"
63#include "navutil.h"
64#include "print_dialog.h"
65#include "printtable.h"
66#include "route_printout.h"
67
68using namespace std;
69
70RoutePrintout::RoutePrintout(Route* route, const std::set<int>& options)
71 : BasePrintout(_("Route Print").ToStdString()), m_route(route) {
72 // Offset text from the edge of the cell (Needed on Linux)
73 m_text_offset_x = 5;
74 m_text_offset_y = 8;
75
76 m_table.StartFillHeader();
77
78 m_table << _("Leg");
79
80 if (GUI::HasKey(options, RoutePrintOptions::kWaypointName)) {
81 m_table << _("To Waypoint");
82 }
83 if (GUI::HasKey(options, RoutePrintOptions::kWaypointPosition)) {
84 m_table << _("Position");
85 }
86 if (GUI::HasKey(options, RoutePrintOptions::kWaypointCourse)) {
87 m_table << _("Course");
88 }
89 if (GUI::HasKey(options, RoutePrintOptions::kWaypointDistance)) {
90 m_table << _("Distance");
91 }
92 if (GUI::HasKey(options, RoutePrintOptions::kWaypointDescription)) {
93 m_table << _("Description");
94 }
95
96 // setup widths for columns
97 m_table.StartFillWidths();
98 m_table << 20;
99
100 if (GUI::HasKey(options, RoutePrintOptions::kWaypointName)) {
101 m_table << 40;
102 }
103 if (GUI::HasKey(options, RoutePrintOptions::kWaypointPosition)) {
104 m_table << 40;
105 }
106 if (GUI::HasKey(options, RoutePrintOptions::kWaypointCourse)) {
107 m_table << 40;
108 }
109 if (GUI::HasKey(options, RoutePrintOptions::kWaypointDistance)) {
110 m_table << 80;
111 }
112 if (GUI::HasKey(options, RoutePrintOptions::kWaypointDescription)) {
113 m_table << 100;
114 }
115
116 m_table.StartFillData();
117
118 for (int n = 1; n <= m_route->GetnPoints(); n++) {
119 RoutePoint* point = m_route->GetPoint(n);
120 if (NULL == point) continue;
121
122 if (n > 1) {
123 m_table << n - 1;
124 } else {
125 m_table << "---";
126 }
127
128 if (GUI::HasKey(options, RoutePrintOptions::kWaypointName)) {
129 m_table << point->GetName();
130 }
131 if (GUI::HasKey(options, RoutePrintOptions::kWaypointPosition)) {
132 std::wostringstream point_position;
133 point_position << toSDMM(1, point->m_lat, false) << "\n"
134 << toSDMM(2, point->m_lon, false);
135 m_table << point_position.str();
136 }
137 if (GUI::HasKey(options, RoutePrintOptions::kWaypointCourse)) {
138 if (n > 1) {
139 m_table << formatAngle(point->GetCourse());
140 } else {
141 m_table << "---";
142 }
143 }
144 if (GUI::HasKey(options, RoutePrintOptions::kWaypointDistance)) {
145 if (n > 1) {
146 std::ostringstream point_distance;
147 point_distance << std::fixed << std::setprecision(2) << std::setw(6)
148 << toUsrDistance(point->GetDistance())
149 << getUsrDistanceUnit();
150 m_table << point_distance.str();
151 } else {
152 m_table << "---";
153 }
154 }
155 if (GUI::HasKey(options, RoutePrintOptions::kWaypointDescription)) {
156 m_table << point->GetDescription();
157 }
158 m_table << "\n";
159 }
160}
161
162void RoutePrintout::OnPreparePrinting() {
163 wxDC* dc = GetDC();
164 wxFont routePrintFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
165 wxFONTWEIGHT_NORMAL);
166 dc->SetFont(routePrintFont);
167
168 // Get the size of the DC in pixels
169 int w, h;
170 dc->GetSize(&w, &h);
171
172 // We don't know before hand what size the Print DC will be, in pixels. Varies
173 // by host. So, if the dc size is greater than 1000 pixels, we scale
174 // accordinly.
175 int max_x = wxMin(w, 1000);
176 int max_y = wxMin(h, 1000);
177
178 // Calculate a suitable scaling factor
179 double scale_x = (double)(w / max_x);
180 double scale_y = (double)(h / max_y);
181
182 // Use x or y scaling factor, whichever fits on the DC
183 double actual_scale = wxMin(scale_x, scale_y);
184
185 // Set the scale and origin
186 dc->SetUserScale(actual_scale, actual_scale);
187 dc->SetDeviceOrigin((long)m_margin_x, (long)m_margin_y);
188
189 m_table.AdjustCells(dc, m_margin_x, m_margin_y);
190 m_pages = m_table.GetNumberPages();
191}
192
193void RoutePrintout::DrawPage(wxDC* dc, int page) {
194 wxFont routePrintFont_bold(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
195 wxFONTWEIGHT_BOLD);
196 dc->SetFont(routePrintFont_bold);
197 wxBrush brush(wxColour(255, 255, 255), wxBRUSHSTYLE_TRANSPARENT);
198 dc->SetBrush(brush);
199
200 int header_text_offset_x = 2;
201 int header_text_offset_y = 2;
202
203 dc->DrawText(m_route->m_RouteNameString, 150, 20);
204
205 int current_x = m_margin_x;
206 int current_y = m_margin_y;
207 vector<PrintCell>& header_content = m_table.GetHeader();
208 for (size_t j = 0; j < header_content.size(); j++) {
209 PrintCell& cell = header_content[j];
210 dc->DrawRectangle(current_x, current_y, cell.GetWidth(), cell.GetHeight());
211 dc->DrawText(cell.GetText(), current_x + header_text_offset_x,
212 current_y + header_text_offset_y);
213 current_x += cell.GetWidth();
214 }
215
216 wxFont routePrintFont_normal(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
217 wxFONTWEIGHT_NORMAL);
218 dc->SetFont(routePrintFont_normal);
219
220 vector<vector<PrintCell> >& cells = m_table.GetContent();
221 current_y = m_margin_y + m_table.GetHeaderHeight();
222 int current_height = 0;
223 for (size_t i = 0; i < cells.size(); i++) {
224 vector<PrintCell>& content_row = cells[i];
225 current_x = m_margin_x;
226 for (size_t j = 0; j < content_row.size(); j++) {
227 PrintCell& cell = content_row[j];
228 if (cell.GetPage() == page) {
229 wxRect r(current_x, current_y, cell.GetWidth(), cell.GetHeight());
230 dc->DrawRectangle(r);
231 r.Offset(m_text_offset_x, m_text_offset_y);
232 dc->DrawLabel(cell.GetText(), r);
233 current_x += cell.GetWidth();
234 current_height = cell.GetHeight();
235 }
236 }
237 current_y += current_height;
238 }
239}
Application print support.
This class takes multilined string and modifies it to fit into given width for given device.
Definition printtable.h:113
Represents a waypoint or mark within the navigation system.
Definition route_point.h:70
void DrawPage(wxDC *dc, int page) override
Called by the print framework to draw the page.
RoutePrintout(Route *route, const std::set< int > &options)
Create route prinout.
Represents a navigational route in the navigation system.
Definition route.h:98
wxString m_RouteNameString
User-assigned name for the route.
Definition route.h:246
General purpose GUI support.
bool HasKey(const std::set< int > &set, T key)
Check if a key exists in a set.
Definition ui_utils.h:60