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