OpenCPN Partial API docs
Loading...
Searching...
No Matches
track_printout.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2017 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, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25#include <iostream>
26
27#ifdef __WXMSW__
28#include <stdlib.h>
29#include <math.h>
30#include <time.h>
31#include <psapi.h>
32#endif
33
34#include <wx/wxprec.h>
35#ifndef WX_PRECOMP
36#include <wx/wx.h>
37#endif
38
39#include "gl_headers.h" // Must be included before anything using GL stuff
40
41#include <wx/artprov.h>
42#include <wx/aui/aui.h>
43#include <wx/brush.h>
44#include <wx/colour.h>
45#include <wx/dialog.h>
46#include <wx/intl.h>
47#include <wx/listctrl.h>
48#include <wx/printdlg.h>
49#include <wx/print.h>
50#include <wx/progdlg.h>
51#include <wx/stdpaths.h>
52
53#include "track_printout.h"
54
55#include "model/track.h"
56
57#include "dychart.h"
58#include "gui_lib.h"
59#include "ocpn_frame.h"
60#include "print_dialog.h"
61#include "printtable.h"
62
63using namespace std;
64
66 std::set<int> options)
67 : BasePrintout(_("Track Print").ToStdString()), m_track(track) {
68 // Offset text from the edge of the cell (Needed on Linux)
69 m_text_offset_x = 5;
70 m_text_offset_y = 8;
71
72 m_table.StartFillHeader();
73 // setup widths for columns
74
75 m_table << (const char*)wxString(_("Leg")).mb_str();
76
77 if (GUI::HasKey(options, TrackPrintOptions::kTrackPosition)) {
78 m_table << (const char*)wxString(_("Position")).mb_str();
79 }
80 if (GUI::HasKey(options, TrackPrintOptions::kTrackCourse)) {
81 m_table << (const char*)wxString(_("Course")).mb_str();
82 }
83 if (GUI::HasKey(options, TrackPrintOptions::kTrackDistance)) {
84 m_table << (const char*)wxString(_("Distance")).mb_str();
85 }
86 if (GUI::HasKey(options, TrackPrintOptions::kTrackTime)) {
87 m_table << (const char*)wxString(_("Time")).mb_str();
88 }
89 if (GUI::HasKey(options, TrackPrintOptions::kTrackSpeed)) {
90 m_table << (const char*)wxString(_("Speed")).mb_str();
91 }
92
93 m_table.StartFillWidths();
94
95 m_table << 20; // "Leg" column
96 // setup widths for columns
97 if (GUI::HasKey(options, TrackPrintOptions::kTrackPosition)) m_table << 80;
98 if (GUI::HasKey(options, TrackPrintOptions::kTrackCourse)) m_table << 40;
99 if (GUI::HasKey(options, TrackPrintOptions::kTrackDistance)) m_table << 40;
100 if (GUI::HasKey(options, TrackPrintOptions::kTrackTime)) m_table << 60;
101 if (GUI::HasKey(options, TrackPrintOptions::kTrackSpeed)) m_table << 40;
102
103 m_table.StartFillData();
104 for (int n = 0; n <= m_track->GetnPoints(); n++) {
105 m_table << lcPoints->OnGetItemText(n, 0); // leg
106
107 if (GUI::HasKey(options, TrackPrintOptions::kTrackPosition)) {
108 m_table << lcPoints->OnGetItemText(n, 3) + _(" ") +
109 lcPoints->OnGetItemText(n, 4); // position
110 }
111 if (GUI::HasKey(options, TrackPrintOptions::kTrackCourse))
112 m_table << lcPoints->OnGetItemText(n, 2); // bearing
113 if (GUI::HasKey(options, TrackPrintOptions::kTrackDistance))
114 m_table << lcPoints->OnGetItemText(n, 1); // distance
115 if (GUI::HasKey(options, TrackPrintOptions::kTrackTime))
116 m_table << lcPoints->OnGetItemText(n, 5); // time
117 if (GUI::HasKey(options, TrackPrintOptions::kTrackSpeed))
118 m_table << lcPoints->OnGetItemText(n, 6); // speed
119 m_table << "\n";
120 }
121}
122
123void TrackPrintout::OnPreparePrinting() {
124 wxDC* dc = GetDC();
125 wxFont trackPrintFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
126 wxFONTWEIGHT_NORMAL);
127 dc->SetFont(trackPrintFont);
128
129 // Get the size of the DC in pixels
130 int w, h;
131 dc->GetSize(&w, &h);
132
133 // We don't know before hand what size the Print DC will be, in pixels. Varies
134 // by host. So, if the dc size is greater than 1000 pixels, we scale
135 // accordinly.
136 int max_x = wxMin(w, 1000);
137 int max_y = wxMin(h, 1000);
138
139 // Calculate a suitable scaling factor
140 double scale_x = (double)(w / max_x);
141 double scale_y = (double)(h / max_y);
142
143 // Use x or y scaling factor, whichever fits on the DC
144 double actual_scale = wxMin(scale_x, scale_y);
145
146 // Set the scale and origin
147 dc->SetUserScale(actual_scale, actual_scale);
148 dc->SetDeviceOrigin((long)m_margin_x, (long)m_margin_y);
149
150 m_table.AdjustCells(dc, m_margin_x, m_margin_y);
151 m_pages = m_table.GetNumberPages();
152}
153
154void TrackPrintout::DrawPage(wxDC* dc, int page) {
155 wxFont trackPrintFont_bold(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
156 wxFONTWEIGHT_BOLD);
157 dc->SetFont(trackPrintFont_bold);
158 wxBrush brush(wxColour(255, 255, 255), wxBRUSHSTYLE_TRANSPARENT);
159 dc->SetBrush(brush);
160
161 int header_text_offset_x = 2;
162 int header_text_offset_y = 2;
163
164 dc->DrawText(m_track->GetName(), 150, 20);
165
166 int current_x = m_margin_x;
167 int current_y = m_margin_y;
168 vector<PrintCell>& header_content = m_table.GetHeader();
169 for (size_t j = 0; j < header_content.size(); j++) {
170 PrintCell& cell = header_content[j];
171 dc->DrawRectangle(current_x, current_y, cell.GetWidth(), cell.GetHeight());
172 dc->DrawText(cell.GetText(), current_x + header_text_offset_x,
173 current_y + header_text_offset_y);
174 current_x += cell.GetWidth();
175 }
176
177 wxFont trackPrintFont_normal(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
178 wxFONTWEIGHT_NORMAL);
179 dc->SetFont(trackPrintFont_normal);
180
181 vector<vector<PrintCell> >& cells = m_table.GetContent();
182 current_y = m_margin_y + m_table.GetHeaderHeight();
183 int current_height = 0;
184 for (size_t i = 0; i < cells.size(); i++) {
185 vector<PrintCell>& content_row = cells[i];
186 current_x = m_margin_x;
187 for (size_t j = 0; j < content_row.size(); j++) {
188 PrintCell& cell = content_row[j];
189 if (cell.GetPage() == page) {
190 wxRect r(current_x, current_y, cell.GetWidth(), cell.GetHeight());
191 dc->DrawRectangle(r);
192 r.Offset(m_text_offset_x, m_text_offset_y);
193 dc->DrawLabel(cell.GetText(), r);
194 current_x += cell.GetWidth();
195 current_height = cell.GetHeight();
196 }
197 }
198 current_y += current_height;
199 }
200}
Application print support.
This class takes multilined string and modifies it to fit into given width for given device.
Definition printtable.h:106
TrackPrintout(Track *track, OCPNTrackListCtrl *lcPoints, std::set< int > options)
Create track printout.
void DrawPage(wxDC *dc, int page) override
Called by the print framework to draw the page.
Represents a track, which is a series of connected track points.
Definition track.h:117
Platform independent GL includes.
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
OpenCPN top window.
Generic, styled prit dialog.
OpenCPN Route table printout.
Recorded track abstraction.
Track print dialog.