This reverts commit 643f4a5726 and finishes the task of adding WebEngine as an alternate backend to be used in printing. The ultimate goal is to be able to build without QtWebKit (as that is no longer supported in Qt6). WebKit was used in two places: The user manual and printing. This patch makes printing work with WebEngine. The main obstacle is that WebEngine no longer allows accessing HTML elements from C++ code and rendering the page to a QPainter. The old version used this to figure out dimensions and page breaks for the pages and then in the QPainter placed the profile images. With WebEninge, you need to access the elements using JavaScript which is now used to place the profile in the html proplerly as an <img> tag. To this end, both html and profile images are written to a temporary directory on disk. This image replacement by JavaScript is only necessary to make old templates still work. It could be replaced by actually putting <img> tags in the templates (but this would break user edited templates). In my experiments, the page breaking was done great by html/css, so the additional magic seems superflous. What remains to be done: * remove empty page at the end of printout * make preview great again (in particlar needed for template editing) Note: since QtWebEngine currently cannot be built with our toolchain on Windows, this patch keeps QtWebKit support around by making the QtWebEngine compile-time conditional via #ifdefs. [Dirk Hohndel: merged a few commits to make this more logical - the resulting commit is fairly big, but IMHO preferrable] Signed-off-by: Robert C. Helling <helling@atdotde.de> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
146 lines
4.5 KiB
C++
146 lines
4.5 KiB
C++
// SPDX-License-Identifier: GPL-2.0
|
|
#include "printerwebengine.h"
|
|
#include "mainwindow.h"
|
|
#include "templatelayout.h"
|
|
#include "core/statistics.h"
|
|
#include "core/qthelper.h"
|
|
#include "core/settings/qPrefDisplay.h"
|
|
#include "profile-widget/profilewidget2.h"
|
|
|
|
#include <algorithm>
|
|
#include <QPainter>
|
|
#include <QPrinter>
|
|
#include <QtWebEngineWidgets>
|
|
|
|
extern void exportProfile(const struct dive *dive, const QString filename);
|
|
|
|
Printer::Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, bool inPlanner) :
|
|
webView(new QWebEngineView),
|
|
paintDevice(paintDevice),
|
|
printOptions(printOptions),
|
|
templateOptions(templateOptions),
|
|
printMode(printMode),
|
|
inPlanner(inPlanner),
|
|
done(0)
|
|
{
|
|
connect(webView, &QWebEngineView::loadFinished, this, &Printer::onLoadFinished);
|
|
connect(this, &Printer::profilesInserted, this, &Printer::printing);
|
|
profilesMissing = true;
|
|
}
|
|
|
|
Printer::~Printer()
|
|
{
|
|
delete webView;
|
|
}
|
|
|
|
void Printer::onLoadFinished()
|
|
{
|
|
if (profilesMissing) {
|
|
webView->page()->runJavaScript(" var profiles = document.getElementsByClassName(\"diveProfile\");\
|
|
for (let profile of profiles) { \
|
|
var id = profile.attributes.getNamedItem(\"Id\").value; \
|
|
var img = document.createElement(\"img\"); \
|
|
img.src = id + \".png\"; \
|
|
img.style.height = \"100%\"; \
|
|
img.style.width = \"100%\"; \
|
|
profile.appendChild(img); \
|
|
} \
|
|
", [this](const QVariant &v) { emit profilesInserted(); });
|
|
|
|
}
|
|
profilesMissing = false;
|
|
emit(progessUpdated(100));
|
|
}
|
|
|
|
void Printer::printing()
|
|
{
|
|
QPrintDialog printDialog(&printer, (QWidget *) nullptr);
|
|
if (printDialog.exec() == QDialog::Accepted) {
|
|
webView->page()->print(&printer, [this](bool ok){ if (ok) emit jobDone(); });
|
|
}
|
|
printDialog.close();
|
|
}
|
|
|
|
//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);
|
|
}
|
|
|
|
QString Printer::exportHtml()
|
|
{
|
|
// Does anybody actually use this? It will not contian profile images!!!
|
|
TemplateLayout t(printOptions, templateOptions);
|
|
connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
|
|
QString html;
|
|
|
|
if (printOptions.type == print_options::DIVELIST)
|
|
html = t.generate(inPlanner);
|
|
else if (printOptions.type == print_options::STATISTICS )
|
|
html = t.generateStatistics();
|
|
|
|
// TODO: write html to file
|
|
return html;
|
|
}
|
|
|
|
void Printer::print()
|
|
{
|
|
// we can only print if "PRINT" mode is selected
|
|
if (printMode != Printer::PRINT) {
|
|
return;
|
|
}
|
|
int i;
|
|
struct dive *dive;
|
|
QString fn;
|
|
int dives_to_print = 0;
|
|
for_each_dive(i, dive)
|
|
if (dive->selected || !printOptions.print_selected)
|
|
++dives_to_print;
|
|
|
|
for_each_dive (i, dive) {
|
|
//TODO check for exporting selected dives only
|
|
if (!dive->selected && printOptions.print_selected)
|
|
continue;
|
|
exportProfile(dive, printDir.filePath(QString("dive_%1.png").arg(dive->id)));
|
|
emit(progessUpdated(done + lrint(i * 80.0 / dives_to_print)));
|
|
}
|
|
|
|
TemplateLayout t(printOptions, templateOptions);
|
|
connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
|
|
int dpi = printer.resolution();
|
|
webView->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
|
|
connect(webView, &QWebEngineView::loadFinished, this, &Printer::onLoadFinished);
|
|
|
|
if (printOptions.type == print_options::DIVELIST) {
|
|
QFile printFile(printDir.filePath("print.html"));
|
|
printFile.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
QTextStream out(&printFile);
|
|
out << t.generate(inPlanner);
|
|
printFile.close();
|
|
webView->load(QUrl::fromLocalFile(printDir.filePath("print.html")));
|
|
} else if (printOptions.type == print_options::STATISTICS )
|
|
webView->setHtml(t.generateStatistics());
|
|
if (printOptions.color_selected && printer.colorMode())
|
|
printer.setColorMode(QPrinter::Color);
|
|
else
|
|
printer.setColorMode(QPrinter::GrayScale);
|
|
printer.setResolution(dpi);
|
|
}
|
|
|
|
void Printer::previewOnePage()
|
|
{
|
|
if (printMode == PREVIEW) {
|
|
TemplateLayout t(printOptions, templateOptions);
|
|
|
|
pageSize.setHeight(paintDevice->height());
|
|
pageSize.setWidth(paintDevice->width());
|
|
// initialize the border settings
|
|
// templateOptions.border_width = std::max(1, pageSize.width() / 1000);
|
|
if (printOptions.type == print_options::DIVELIST)
|
|
webView->setHtml(t.generate(inPlanner));
|
|
else if (printOptions.type == print_options::STATISTICS )
|
|
webView->setHtml(t.generateStatistics());
|
|
}
|
|
}
|