2017-04-27 18:26:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-04-21 16:05:20 +00:00
|
|
|
#include "printer.h"
|
|
|
|
#include "templatelayout.h"
|
2016-04-05 16:40:03 +00:00
|
|
|
#include "core/statistics.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "core/qthelper.h"
|
2018-08-20 09:51:48 +00:00
|
|
|
#include "core/settings/qPrefDisplay.h"
|
2015-04-21 16:05:20 +00:00
|
|
|
|
2015-08-11 20:50:27 +00:00
|
|
|
#include <algorithm>
|
2015-04-21 16:05:20 +00:00
|
|
|
#include <QPainter>
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifdef USE_WEBENGINE
|
|
|
|
#include <QtWebEngineWidgets>
|
|
|
|
#else
|
|
|
|
#include <QtWebKitWidgets>
|
2015-06-10 17:31:19 +00:00
|
|
|
#include <QWebElementCollection>
|
|
|
|
#include <QWebElement>
|
2016-08-08 22:12:12 +00:00
|
|
|
#endif
|
2015-09-03 18:56:37 +00:00
|
|
|
#include "profile-widget/profilewidget2.h"
|
2015-04-21 16:05:20 +00:00
|
|
|
|
2015-07-10 19:30:18 +00:00
|
|
|
Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode)
|
2015-04-21 16:05:20 +00:00
|
|
|
{
|
2015-07-10 18:34:25 +00:00
|
|
|
this->paintDevice = paintDevice;
|
2015-06-14 04:25:35 +00:00
|
|
|
this->printOptions = printOptions;
|
2015-07-02 20:26:31 +00:00
|
|
|
this->templateOptions = templateOptions;
|
2015-07-10 19:30:18 +00:00
|
|
|
this->printMode = printMode;
|
2015-06-22 13:42:02 +00:00
|
|
|
dpi = 0;
|
2015-05-30 13:32:15 +00:00
|
|
|
done = 0;
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifdef USE_WEBENGINE
|
|
|
|
webView = new QWebEngineView();
|
|
|
|
#else
|
2015-07-10 12:59:56 +00:00
|
|
|
webView = new QWebView();
|
2016-08-08 22:12:12 +00:00
|
|
|
#endif
|
2015-07-10 12:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Printer::~Printer()
|
|
|
|
{
|
|
|
|
delete webView;
|
2015-04-21 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:31:19 +00:00
|
|
|
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());
|
2018-05-16 13:25:57 +00:00
|
|
|
profile->plotDive(dive, true, true);
|
2015-07-19 05:12:01 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-06-10 17:31:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 21:23:07 +00:00
|
|
|
void Printer::flowRender()
|
|
|
|
{
|
2015-08-19 21:02:58 +00:00
|
|
|
// add extra padding at the bottom to pages with height not divisible by view port
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifndef USE_WEBENGINE
|
2015-08-19 21:02:58 +00:00
|
|
|
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);
|
|
|
|
|
2015-08-13 21:23:07 +00:00
|
|
|
// 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 {
|
2015-08-14 19:37:12 +00:00
|
|
|
// fill the page with background color
|
|
|
|
QRect fullPage(0, 0, pageSize.width(), pageSize.height());
|
|
|
|
QBrush fillBrush(templateOptions->color_palette.color1);
|
|
|
|
painter.fillRect(fullPage, fillBrush);
|
2015-08-13 21:23:07 +00:00
|
|
|
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
|
2017-03-23 01:13:49 +00:00
|
|
|
emit(progessUpdated(lrint((end * 80.0 / fullPageResolution) + done)));
|
2015-08-21 09:02:10 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2015-08-13 21:23:07 +00:00
|
|
|
start = dontbreakElement.geometry().y();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// render the remianing page
|
2015-08-14 19:37:12 +00:00
|
|
|
QRect fullPage(0, 0, pageSize.width(), pageSize.height());
|
|
|
|
QBrush fillBrush(templateOptions->color_palette.color1);
|
|
|
|
painter.fillRect(fullPage, fillBrush);
|
2015-08-13 21:23:07 +00:00
|
|
|
QRegion reigon(0, 0, pageSize.width(), end - start);
|
|
|
|
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon);
|
|
|
|
|
|
|
|
painter.end();
|
2016-08-08 22:12:12 +00:00
|
|
|
#else
|
|
|
|
// FIX ME
|
|
|
|
#endif
|
2015-08-13 21:23:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 14:20:14 +00:00
|
|
|
void Printer::render(int Pages = 0)
|
2015-04-21 16:05:20 +00:00
|
|
|
{
|
2015-06-10 17:31:19 +00:00
|
|
|
// keep original preferences
|
2018-10-12 13:07:40 +00:00
|
|
|
QPointer<ProfileWidget2> profile = MainWindow::instance()->graphics;
|
2015-06-10 17:31:19 +00:00
|
|
|
int profileFrameStyle = profile->frameStyle();
|
2018-08-20 09:51:48 +00:00
|
|
|
int animationOriginal = qPrefDisplay::animation_speed();
|
2015-06-10 17:31:19 +00:00
|
|
|
double fontScale = profile->getFontPrintScale();
|
2015-07-29 19:24:18 +00:00
|
|
|
double printFontScale = 1.0;
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// apply printing settings to profile
|
|
|
|
profile->setFrameStyle(QFrame::NoFrame);
|
2015-06-14 04:34:58 +00:00
|
|
|
profile->setPrintMode(true, !printOptions->color_selected);
|
2015-06-10 17:31:19 +00:00
|
|
|
profile->setToolTipVisibile(false);
|
2018-08-20 09:51:48 +00:00
|
|
|
qPrefDisplay::set_animation_speed(0);
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// render the Qwebview
|
2015-04-21 16:05:20 +00:00
|
|
|
QPainter painter;
|
2015-06-17 15:05:14 +00:00
|
|
|
QRect viewPort(0, 0, pageSize.width(), pageSize.height());
|
2015-07-10 18:34:25 +00:00
|
|
|
painter.begin(paintDevice);
|
2015-04-21 16:05:20 +00:00
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
2015-06-14 04:34:58 +00:00
|
|
|
|
2015-06-10 17:31:19 +00:00
|
|
|
// get all refereces to diveprofile class in the Html template
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifdef USE_WEBENGINE
|
|
|
|
//FIX ME
|
|
|
|
#else
|
2015-06-10 17:31:19 +00:00
|
|
|
QWebElementCollection collection = webView->page()->mainFrame()->findAllElements(".diveprofile");
|
|
|
|
|
|
|
|
QSize originalSize = profile->size();
|
|
|
|
if (collection.count() > 0) {
|
2015-07-29 19:24:18 +00:00
|
|
|
printFontScale = (double)collection.at(0).geometry().size().height() / (double)profile->size().height();
|
2015-06-10 17:31:19 +00:00
|
|
|
profile->resize(collection.at(0).geometry().size());
|
|
|
|
}
|
2015-07-29 19:24:18 +00:00
|
|
|
profile->setFontPrintScale(printFontScale);
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
int elemNo = 0;
|
2015-04-21 16:05:20 +00:00
|
|
|
for (int i = 0; i < Pages; i++) {
|
2015-06-10 17:31:19 +00:00
|
|
|
// render the base Html template
|
2015-04-21 16:05:20 +00:00
|
|
|
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer);
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// 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
|
2015-07-28 20:35:04 +00:00
|
|
|
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);
|
2015-06-10 17:31:19 +00:00
|
|
|
elemNo++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scroll the webview to the next page
|
2015-06-17 15:05:14 +00:00
|
|
|
webView->page()->mainFrame()->scroll(0, pageSize.height());
|
|
|
|
viewPort.adjust(0, pageSize.height(), 0, pageSize.height());
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// rendering progress is 4/5 of total work
|
2017-03-23 01:13:49 +00:00
|
|
|
emit(progessUpdated(lrint((i * 80.0 / Pages) + done)));
|
2015-07-10 19:30:18 +00:00
|
|
|
if (i < Pages - 1 && printMode == Printer::PRINT)
|
2015-07-10 18:34:25 +00:00
|
|
|
static_cast<QPrinter*>(paintDevice)->newPage();
|
2015-04-21 16:05:20 +00:00
|
|
|
}
|
|
|
|
painter.end();
|
2016-08-08 22:12:12 +00:00
|
|
|
#endif
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// return profle settings
|
|
|
|
profile->setFrameStyle(profileFrameStyle);
|
|
|
|
profile->setPrintMode(false);
|
|
|
|
profile->setFontPrintScale(fontScale);
|
|
|
|
profile->setToolTipVisibile(true);
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifdef USE_WEBENGINE
|
|
|
|
//FIXME
|
|
|
|
#else
|
2015-06-10 17:31:19 +00:00
|
|
|
profile->resize(originalSize);
|
2016-08-08 22:12:12 +00:00
|
|
|
#endif
|
2018-08-20 09:51:48 +00:00
|
|
|
qPrefDisplay::set_animation_speed(animationOriginal);
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
//replot the dive after returning the settings
|
2018-05-16 13:25:57 +00:00
|
|
|
profile->plotDive(0, true, true);
|
2015-04-21 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 13:32:15 +00:00
|
|
|
//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);
|
|
|
|
}
|
|
|
|
|
2015-04-21 16:05:20 +00:00
|
|
|
void Printer::print()
|
|
|
|
{
|
2015-07-10 19:30:18 +00:00
|
|
|
// we can only print if "PRINT" mode is selected
|
|
|
|
if (printMode != Printer::PRINT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-19 21:02:58 +00:00
|
|
|
|
2015-07-10 18:34:25 +00:00
|
|
|
QPrinter *printerPtr;
|
|
|
|
printerPtr = static_cast<QPrinter*>(paintDevice);
|
|
|
|
|
2015-07-02 20:26:31 +00:00
|
|
|
TemplateLayout t(printOptions, templateOptions);
|
2015-06-17 15:05:14 +00:00
|
|
|
connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
|
2015-07-10 18:34:25 +00:00
|
|
|
dpi = printerPtr->resolution();
|
2015-06-17 15:05:14 +00:00
|
|
|
//rendering resolution = selected paper size in inchs * printer dpi
|
2015-08-22 14:05:13 +00:00
|
|
|
pageSize.setHeight(qCeil(printerPtr->pageRect(QPrinter::Inch).height() * dpi));
|
|
|
|
pageSize.setWidth(qCeil(printerPtr->pageRect(QPrinter::Inch).width() * dpi));
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifdef USE_WEBENGINE
|
|
|
|
//FIXME
|
|
|
|
#else
|
2015-06-17 15:05:14 +00:00
|
|
|
webView->page()->setViewportSize(pageSize);
|
2015-08-07 22:55:12 +00:00
|
|
|
webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
|
2016-08-08 22:12:12 +00:00
|
|
|
#endif
|
2015-08-11 20:50:27 +00:00
|
|
|
// export border width with at least 1 pixel
|
2017-03-06 16:41:54 +00:00
|
|
|
// templateOptions->borderwidth = std::max(1, pageSize.width() / 1000);
|
2015-08-21 17:12:33 +00:00
|
|
|
if (printOptions->type == print_options::DIVELIST) {
|
|
|
|
webView->setHtml(t.generate());
|
|
|
|
} else if (printOptions->type == print_options::STATISTICS ) {
|
|
|
|
webView->setHtml(t.generateStatistics());
|
|
|
|
}
|
2015-07-10 18:34:25 +00:00
|
|
|
if (printOptions->color_selected && printerPtr->colorMode()) {
|
|
|
|
printerPtr->setColorMode(QPrinter::Color);
|
2015-07-10 14:20:14 +00:00
|
|
|
} else {
|
2015-07-10 18:34:25 +00:00
|
|
|
printerPtr->setColorMode(QPrinter::GrayScale);
|
2015-07-10 14:20:14 +00:00
|
|
|
}
|
|
|
|
// apply user settings
|
|
|
|
int divesPerPage;
|
|
|
|
|
|
|
|
// get number of dives per page from data-numberofdives attribute in the body of the selected template
|
|
|
|
bool ok;
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifdef USE_WEBENGINE
|
|
|
|
// FIX ME
|
|
|
|
#else
|
2015-07-10 14:20:14 +00:00
|
|
|
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
|
|
|
|
}
|
2016-08-08 22:12:12 +00:00
|
|
|
#endif
|
2015-07-23 13:20:09 +00:00
|
|
|
int Pages;
|
|
|
|
if (divesPerPage == 0) {
|
2015-08-13 21:23:07 +00:00
|
|
|
flowRender();
|
2015-07-23 13:20:09 +00:00
|
|
|
} else {
|
2015-08-22 14:05:13 +00:00
|
|
|
Pages = qCeil(getTotalWork(printOptions) / (float)divesPerPage);
|
2015-08-13 21:23:07 +00:00
|
|
|
render(Pages);
|
2015-07-23 13:20:09 +00:00
|
|
|
}
|
2015-04-21 16:05:20 +00:00
|
|
|
}
|
2015-07-10 19:45:27 +00:00
|
|
|
|
|
|
|
void Printer::previewOnePage()
|
|
|
|
{
|
|
|
|
if (printMode == PREVIEW) {
|
|
|
|
TemplateLayout t(printOptions, templateOptions);
|
|
|
|
|
|
|
|
pageSize.setHeight(paintDevice->height());
|
|
|
|
pageSize.setWidth(paintDevice->width());
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifdef USE_WEBENGINE
|
|
|
|
//FIXME
|
|
|
|
#else
|
2015-07-10 19:45:27 +00:00
|
|
|
webView->page()->setViewportSize(pageSize);
|
2016-08-08 22:12:12 +00:00
|
|
|
#endif
|
2015-08-21 08:56:59 +00:00
|
|
|
// initialize the border settings
|
2017-03-06 16:41:54 +00:00
|
|
|
// templateOptions->border_width = std::max(1, pageSize.width() / 1000);
|
2015-08-21 17:12:33 +00:00
|
|
|
if (printOptions->type == print_options::DIVELIST) {
|
|
|
|
webView->setHtml(t.generate());
|
|
|
|
} else if (printOptions->type == print_options::STATISTICS ) {
|
|
|
|
webView->setHtml(t.generateStatistics());
|
|
|
|
}
|
2016-08-08 22:12:12 +00:00
|
|
|
#ifdef USE_WEBENGINE
|
|
|
|
// FIX ME
|
|
|
|
render(1);
|
|
|
|
#else
|
2015-08-21 09:02:10 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-08-08 22:12:12 +00:00
|
|
|
#endif
|
2015-07-10 19:45:27 +00:00
|
|
|
}
|
|
|
|
}
|