1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
// SPDX-License-Identifier: GPL-2.0
#include "printer.h"
#include "templatelayout.h"
#include "core/statistics.h"
#include "core/helpers.h"
#include <algorithm>
#include <QPainter>
#ifdef USE_WEBENGINE
#include <QtWebEngineWidgets>
#else
#include <QtWebKitWidgets>
#include <QWebElementCollection>
#include <QWebElement>
#endif
#include "profile-widget/profilewidget2.h"
Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode)
{
this->paintDevice = paintDevice;
this->printOptions = printOptions;
this->templateOptions = templateOptions;
this->printMode = printMode;
dpi = 0;
done = 0;
#ifdef USE_WEBENGINE
webView = new QWebEngineView();
#else
webView = new QWebView();
#endif
}
Printer::~Printer()
{
delete webView;
}
void Printer::putProfileImage(QRect profilePlaceholder, QRect viewPort, QPainter *painter, struct dive *dive, QPointer<ProfileWidget2> profile)
{
int x = profilePlaceholder.x() - viewPort.x();
int y = profilePlaceholder.y() - viewPort.y();
// use the placeHolder and the viewPort position to calculate the relative position of the dive profile.
QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height());
profile->plotDive(dive, true, true);
if (!printOptions->color_selected) {
QImage image(pos.width(), pos.height(), QImage::Format_ARGB32);
QPainter imgPainter(&image);
imgPainter.setRenderHint(QPainter::Antialiasing);
imgPainter.setRenderHint(QPainter::SmoothPixmapTransform);
profile->render(&imgPainter, QRect(0, 0, pos.width(), pos.height()));
imgPainter.end();
// convert QImage to grayscale before rendering
for (int i = 0; i < image.height(); i++) {
QRgb *pixel = reinterpret_cast<QRgb *>(image.scanLine(i));
QRgb *end = pixel + image.width();
for (; pixel != end; pixel++) {
int gray_val = qGray(*pixel);
*pixel = QColor(gray_val, gray_val, gray_val).rgb();
}
}
painter->drawImage(pos, image);
} else {
profile->render(painter, pos);
}
}
void Printer::flowRender()
{
// add extra padding at the bottom to pages with height not divisible by view port
#ifndef USE_WEBENGINE
int paddingBottom = pageSize.height() - (webView->page()->mainFrame()->contentsSize().height() % pageSize.height());
QString styleString = QString::fromUtf8("padding-bottom: ") + QString::number(paddingBottom) + "px;";
webView->page()->mainFrame()->findFirstElement("body").setAttribute("style", styleString);
// render the Qwebview
QPainter painter;
QRect viewPort(0, 0, 0, 0);
painter.begin(paintDevice);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
// get all references to dontbreak divs
int start = 0, end = 0;
int fullPageResolution = webView->page()->mainFrame()->contentsSize().height();
QWebElementCollection dontbreak = webView->page()->mainFrame()->findAllElements(".dontbreak");
foreach (QWebElement dontbreakElement, dontbreak) {
if ((dontbreakElement.geometry().y() + dontbreakElement.geometry().height()) - start < pageSize.height()) {
// One more element can be placed
end = dontbreakElement.geometry().y() + dontbreakElement.geometry().height();
} else {
// fill the page with background color
QRect fullPage(0, 0, pageSize.width(), pageSize.height());
QBrush fillBrush(templateOptions->color_palette.color1);
painter.fillRect(fullPage, fillBrush);
QRegion reigon(0, 0, pageSize.width(), end - start);
viewPort.setRect(0, start, pageSize.width(), end - start);
// render the base Html template
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon);
// scroll the webview to the next page
webView->page()->mainFrame()->scroll(0, dontbreakElement.geometry().y() - start);
// rendering progress is 4/5 of total work
emit(progessUpdated(lrint((end * 80.0 / fullPageResolution) + done)));
// add new pages only in print mode, while previewing we don't add new pages
if (printMode == Printer::PRINT)
static_cast<QPrinter*>(paintDevice)->newPage();
else {
painter.end();
return;
}
start = dontbreakElement.geometry().y();
}
}
// render the remianing page
QRect fullPage(0, 0, pageSize.width(), pageSize.height());
QBrush fillBrush(templateOptions->color_palette.color1);
painter.fillRect(fullPage, fillBrush);
QRegion reigon(0, 0, pageSize.width(), end - start);
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon);
painter.end();
#else
// FIX ME
#endif
}
void Printer::render(int Pages = 0)
{
// keep original preferences
QPointer<ProfileWidget2> profile = MainWindow::instance()->graphics();
int profileFrameStyle = profile->frameStyle();
int animationOriginal = prefs.animation_speed;
double fontScale = profile->getFontPrintScale();
double printFontScale = 1.0;
// apply printing settings to profile
profile->setFrameStyle(QFrame::NoFrame);
profile->setPrintMode(true, !printOptions->color_selected);
profile->setToolTipVisibile(false);
prefs.animation_speed = 0;
// render the Qwebview
QPainter painter;
QRect viewPort(0, 0, pageSize.width(), pageSize.height());
painter.begin(paintDevice);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
// get all refereces to diveprofile class in the Html template
#ifdef USE_WEBENGINE
//FIX ME
#else
QWebElementCollection collection = webView->page()->mainFrame()->findAllElements(".diveprofile");
QSize originalSize = profile->size();
if (collection.count() > 0) {
printFontScale = (double)collection.at(0).geometry().size().height() / (double)profile->size().height();
profile->resize(collection.at(0).geometry().size());
}
profile->setFontPrintScale(printFontScale);
int elemNo = 0;
for (int i = 0; i < Pages; i++) {
// render the base Html template
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer);
// render all the dive profiles in the current page
while (elemNo < collection.count() && collection.at(elemNo).geometry().y() < viewPort.y() + viewPort.height()) {
// dive id field should be dive_{{dive_no}} se we remove the first 5 characters
QString diveIdString = collection.at(elemNo).attribute("id");
int diveId = diveIdString.remove(0, 5).toInt(0, 10);
putProfileImage(collection.at(elemNo).geometry(), viewPort, &painter, get_dive_by_uniq_id(diveId), profile);
elemNo++;
}
// scroll the webview to the next page
webView->page()->mainFrame()->scroll(0, pageSize.height());
viewPort.adjust(0, pageSize.height(), 0, pageSize.height());
// rendering progress is 4/5 of total work
emit(progessUpdated(lrint((i * 80.0 / Pages) + done)));
if (i < Pages - 1 && printMode == Printer::PRINT)
static_cast<QPrinter*>(paintDevice)->newPage();
}
painter.end();
#endif
// return profle settings
profile->setFrameStyle(profileFrameStyle);
profile->setPrintMode(false);
profile->setFontPrintScale(fontScale);
profile->setToolTipVisibile(true);
#ifdef USE_WEBENGINE
//FIXME
#else
profile->resize(originalSize);
#endif
prefs.animation_speed = animationOriginal;
//replot the dive after returning the settings
profile->plotDive(0, true, true);
}
//value: ranges from 0 : 100 and shows the progress of the templating engine
void Printer::templateProgessUpdated(int value)
{
done = value / 5; //template progess if 1/5 of total work
emit progessUpdated(done);
}
void Printer::print()
{
// we can only print if "PRINT" mode is selected
if (printMode != Printer::PRINT) {
return;
}
QPrinter *printerPtr;
printerPtr = static_cast<QPrinter*>(paintDevice);
TemplateLayout t(printOptions, templateOptions);
connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
dpi = printerPtr->resolution();
//rendering resolution = selected paper size in inchs * printer dpi
pageSize.setHeight(qCeil(printerPtr->pageRect(QPrinter::Inch).height() * dpi));
pageSize.setWidth(qCeil(printerPtr->pageRect(QPrinter::Inch).width() * dpi));
#ifdef USE_WEBENGINE
//FIXME
#else
webView->page()->setViewportSize(pageSize);
webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
#endif
// export border width with at least 1 pixel
// templateOptions->borderwidth = std::max(1, pageSize.width() / 1000);
if (printOptions->type == print_options::DIVELIST) {
webView->setHtml(t.generate());
} else if (printOptions->type == print_options::STATISTICS ) {
webView->setHtml(t.generateStatistics());
}
if (printOptions->color_selected && printerPtr->colorMode()) {
printerPtr->setColorMode(QPrinter::Color);
} else {
printerPtr->setColorMode(QPrinter::GrayScale);
}
// apply user settings
int divesPerPage;
// get number of dives per page from data-numberofdives attribute in the body of the selected template
bool ok;
#ifdef USE_WEBENGINE
// FIX ME
#else
divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok);
if (!ok) {
divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed
//TODO: show warning
}
#endif
int Pages;
if (divesPerPage == 0) {
flowRender();
} else {
Pages = qCeil(getTotalWork(printOptions) / (float)divesPerPage);
render(Pages);
}
}
void Printer::previewOnePage()
{
if (printMode == PREVIEW) {
TemplateLayout t(printOptions, templateOptions);
pageSize.setHeight(paintDevice->height());
pageSize.setWidth(paintDevice->width());
#ifdef USE_WEBENGINE
//FIXME
#else
webView->page()->setViewportSize(pageSize);
#endif
// initialize the border settings
// templateOptions->border_width = std::max(1, pageSize.width() / 1000);
if (printOptions->type == print_options::DIVELIST) {
webView->setHtml(t.generate());
} else if (printOptions->type == print_options::STATISTICS ) {
webView->setHtml(t.generateStatistics());
}
#ifdef USE_WEBENGINE
// FIX ME
render(1);
#else
bool ok;
int divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok);
if (!ok) {
divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed
//TODO: show warning
}
if (divesPerPage == 0) {
flowRender();
} else {
render(1);
}
#endif
}
}
|