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 "print_dialog.h"
60#include "printtable.h"
61
62using namespace std;
63
65 std::set<int> options)
66 : BasePrintout(_("Track Print").ToStdString()), m_track(track) {
67 // Offset text from the edge of the cell (Needed on Linux)
68 m_text_offset_x = 5;
69 m_text_offset_y = 8;
70
71 m_table.StartFillHeader();
72 // setup widths for columns
73
74 m_table << (const char*)wxString(_("Leg")).mb_str();
75
76 if (GUI::HasKey(options, TrackPrintOptions::kTrackPosition)) {
77 m_table << (const char*)wxString(_("Position")).mb_str();
78 }
79 if (GUI::HasKey(options, TrackPrintOptions::kTrackCourse)) {
80 m_table << (const char*)wxString(_("Course")).mb_str();
81 }
82 if (GUI::HasKey(options, TrackPrintOptions::kTrackDistance)) {
83 m_table << (const char*)wxString(_("Distance")).mb_str();
84 }
85 if (GUI::HasKey(options, TrackPrintOptions::kTrackTime)) {
86 m_table << (const char*)wxString(_("Time")).mb_str();
87 }
88 if (GUI::HasKey(options, TrackPrintOptions::kTrackSpeed)) {
89 m_table << (const char*)wxString(_("Speed")).mb_str();
90 }
91
92 m_table.StartFillWidths();
93
94 m_table << 20; // "Leg" column
95 // setup widths for columns
96 if (GUI::HasKey(options, TrackPrintOptions::kTrackPosition)) m_table << 80;
97 if (GUI::HasKey(options, TrackPrintOptions::kTrackCourse)) m_table << 40;
98 if (GUI::HasKey(options, TrackPrintOptions::kTrackDistance)) m_table << 40;
99 if (GUI::HasKey(options, TrackPrintOptions::kTrackTime)) m_table << 60;
100 if (GUI::HasKey(options, TrackPrintOptions::kTrackSpeed)) m_table << 40;
101
102 m_table.StartFillData();
103 for (int n = 0; n <= m_track->GetnPoints(); n++) {
104 m_table << lcPoints->OnGetItemText(n, 0); // leg
105
106 if (GUI::HasKey(options, TrackPrintOptions::kTrackPosition)) {
107 m_table << lcPoints->OnGetItemText(n, 3) + _(" ") +
108 lcPoints->OnGetItemText(n, 4); // position
109 }
110 if (GUI::HasKey(options, TrackPrintOptions::kTrackCourse))
111 m_table << lcPoints->OnGetItemText(n, 2); // bearing
112 if (GUI::HasKey(options, TrackPrintOptions::kTrackDistance))
113 m_table << lcPoints->OnGetItemText(n, 1); // distance
114 if (GUI::HasKey(options, TrackPrintOptions::kTrackTime))
115 m_table << lcPoints->OnGetItemText(n, 5); // time
116 if (GUI::HasKey(options, TrackPrintOptions::kTrackSpeed))
117 m_table << lcPoints->OnGetItemText(n, 6); // speed
118 m_table << "\n";
119 }
120}
121
122void TrackPrintout::OnPreparePrinting() {
123 wxDC* dc = GetDC();
124 wxFont trackPrintFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
125 wxFONTWEIGHT_NORMAL);
126 dc->SetFont(trackPrintFont);
127
128 // Get the size of the DC in pixels
129 int w, h;
130 dc->GetSize(&w, &h);
131
132 // We don't know before hand what size the Print DC will be, in pixels. Varies
133 // by host. So, if the dc size is greater than 1000 pixels, we scale
134 // accordinly.
135 int max_x = wxMin(w, 1000);
136 int max_y = wxMin(h, 1000);
137
138 // Calculate a suitable scaling factor
139 double scale_x = (double)(w / max_x);
140 double scale_y = (double)(h / max_y);
141
142 // Use x or y scaling factor, whichever fits on the DC
143 double actual_scale = wxMin(scale_x, scale_y);
144
145 // Set the scale and origin
146 dc->SetUserScale(actual_scale, actual_scale);
147 dc->SetDeviceOrigin((long)m_margin_x, (long)m_margin_y);
148
149 m_table.AdjustCells(dc, m_margin_x, m_margin_y);
150 m_pages = m_table.GetNumberPages();
151}
152
153void TrackPrintout::DrawPage(wxDC* dc, int page) {
154 wxFont trackPrintFont_bold(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
155 wxFONTWEIGHT_BOLD);
156 dc->SetFont(trackPrintFont_bold);
157 wxBrush brush(wxColour(255, 255, 255), wxBRUSHSTYLE_TRANSPARENT);
158 dc->SetBrush(brush);
159
160 int header_text_offset_x = 2;
161 int header_text_offset_y = 2;
162
163 dc->DrawText(m_track->GetName(), 150, 20);
164
165 int current_x = m_margin_x;
166 int current_y = m_margin_y;
167 vector<PrintCell>& header_content = m_table.GetHeader();
168 for (size_t j = 0; j < header_content.size(); j++) {
169 PrintCell& cell = header_content[j];
170 dc->DrawRectangle(current_x, current_y, cell.GetWidth(), cell.GetHeight());
171 dc->DrawText(cell.GetText(), current_x + header_text_offset_x,
172 current_y + header_text_offset_y);
173 current_x += cell.GetWidth();
174 }
175
176 wxFont trackPrintFont_normal(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
177 wxFONTWEIGHT_NORMAL);
178 dc->SetFont(trackPrintFont_normal);
179
180 vector<vector<PrintCell> >& cells = m_table.GetContent();
181 current_y = m_margin_y + m_table.GetHeaderHeight();
182 int current_height = 0;
183 for (size_t i = 0; i < cells.size(); i++) {
184 vector<PrintCell>& content_row = cells[i];
185 current_x = m_margin_x;
186 for (size_t j = 0; j < content_row.size(); j++) {
187 PrintCell& cell = content_row[j];
188 if (cell.GetPage() == page) {
189 wxRect r(current_x, current_y, cell.GetWidth(), cell.GetHeight());
190 dc->DrawRectangle(r);
191 r.Offset(m_text_offset_x, m_text_offset_y);
192 dc->DrawLabel(cell.GetText(), r);
193 current_x += cell.GetWidth();
194 current_height = cell.GetHeight();
195 }
196 }
197 current_y += current_height;
198 }
199}
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
Generic, styled prit dialog.
OpenCPN Route table printout.
Recorded track abstraction.
Track print dialog.