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