diff --git a/desktop-widgets/printdialog.cpp b/desktop-widgets/printdialog.cpp index 5c290398f..207ae4f00 100644 --- a/desktop-widgets/printdialog.cpp +++ b/desktop-widgets/printdialog.cpp @@ -117,8 +117,11 @@ PrintDialog::PrintDialog(bool inPlanner, QWidget *parent) : QPushButton *printButton = new QPushButton(tr("P&rint")); connect(printButton, SIGNAL(clicked(bool)), this, SLOT(printClicked())); + // To do: Port preview to WebEngine. For the time being: Use OS print preview +#ifndef USE_WEBENGINE QPushButton *previewButton = new QPushButton(tr("&Preview")); connect(previewButton, SIGNAL(clicked(bool)), this, SLOT(previewClicked())); +#endif QPushButton *exportHtmlButton = new QPushButton(tr("Export Html")); connect(exportHtmlButton, SIGNAL(clicked(bool)), this, SLOT(exportHtmlClicked())); @@ -126,7 +129,9 @@ PrintDialog::PrintDialog(bool inPlanner, QWidget *parent) : QDialogButtonBox *buttonBox = new QDialogButtonBox; buttonBox->addButton(QDialogButtonBox::Cancel); buttonBox->addButton(printButton, QDialogButtonBox::AcceptRole); +#ifndef USE_WEBENGINE buttonBox->addButton(previewButton, QDialogButtonBox::ActionRole); +#endif buttonBox->addButton(exportHtmlButton, QDialogButtonBox::AcceptRole); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); @@ -185,7 +190,7 @@ void PrintDialog::createPrinterObj() qprinter = new QPrinter; qprinter->setResolution(printOptions.resolution); qprinter->setOrientation((QPrinter::Orientation)printOptions.landscape); - printer = new Printer(qprinter, printOptions, templateOptions, Printer::PRINT, inPlanner); + printer = new Printer(qprinter, printOptions, templateOptions, Printer::PRINT, inPlanner, nullptr); } } diff --git a/desktop-widgets/printer.cpp b/desktop-widgets/printer.cpp index 77c6459fb..b54ff38ac 100644 --- a/desktop-widgets/printer.cpp +++ b/desktop-widgets/printer.cpp @@ -14,7 +14,7 @@ #include #include "profile-widget/profilewidget2.h" -Printer::Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, bool inPlanner) : +Printer::Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, bool inPlanner, QWidget *parent = nullptr) : paintDevice(paintDevice), webView(new QWebView), printOptions(printOptions), @@ -267,8 +267,6 @@ void Printer::previewOnePage() pageSize.setHeight(paintDevice->height()); pageSize.setWidth(paintDevice->width()); webView->page()->setViewportSize(pageSize); - // 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 ) diff --git a/desktop-widgets/printer.h b/desktop-widgets/printer.h index c841da2c9..84c9c4f0c 100644 --- a/desktop-widgets/printer.h +++ b/desktop-widgets/printer.h @@ -38,7 +38,7 @@ private slots: void templateProgessUpdated(int value); public: - Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, bool inPlanner); + Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, bool inPlanner, QWidget *parent); ~Printer(); void print(); void previewOnePage(); diff --git a/desktop-widgets/printerwebengine.cpp b/desktop-widgets/printerwebengine.cpp index 7f2d54fd8..6d7f6767a 100644 --- a/desktop-widgets/printerwebengine.cpp +++ b/desktop-widgets/printerwebengine.cpp @@ -14,8 +14,7 @@ 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), +Printer::Printer(QPaintDevice *paintDevice, print_options &printOptions, template_options &templateOptions, PrintMode printMode, bool inPlanner, QWidget *parent = nullptr) : paintDevice(paintDevice), printOptions(printOptions), templateOptions(templateOptions), @@ -23,8 +22,10 @@ Printer::Printer(QPaintDevice *paintDevice, const print_options &printOptions, c inPlanner(inPlanner), done(0) { + webView = new QWebEngineView(parent); connect(webView, &QWebEngineView::loadFinished, this, &Printer::onLoadFinished); - connect(this, &Printer::profilesInserted, this, &Printer::printing); + if (printMode == PRINT) + connect(this, &Printer::profilesInserted, this, &Printer::printing); profilesMissing = true; } @@ -55,9 +56,8 @@ void Printer::onLoadFinished() void Printer::printing() { QPrintDialog printDialog(&printer, (QWidget *) nullptr); - if (printDialog.exec() == QDialog::Accepted) { + if (printDialog.exec() == QDialog::Accepted) webView->page()->print(&printer, [this](bool ok){ if (ok) emit jobDone(); }); - } printDialog.close(); } @@ -84,12 +84,28 @@ QString Printer::exportHtml() return html; } +void Printer::updateOptions(print_options &poptions, template_options &toptions) +{ + templateOptions = toptions; + printOptions = poptions; + profilesMissing = true; +} + +QString Printer::writeTmpTemplate(const QString templtext) +{ + QFile fd(printDir.filePath("ssrftmptemplate.html")); + + + fd.open(QIODevice::WriteOnly | QIODevice::Text); + QTextStream out(&fd); + out << templtext; + fd.close(); + return fd.fileName(); +} + void Printer::print() { // we can only print if "PRINT" mode is selected - if (printMode != Printer::PRINT) { - return; - } int i; struct dive *dive; QString fn; @@ -106,6 +122,7 @@ void Printer::print() emit(progessUpdated(done + lrint(i * 80.0 / dives_to_print))); } + profilesMissing = true; TemplateLayout t(printOptions, templateOptions); connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int))); int dpi = printer.resolution(); @@ -128,18 +145,3 @@ void Printer::print() 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()); - } -} diff --git a/desktop-widgets/printerwebengine.h b/desktop-widgets/printerwebengine.h index b159a020e..7419f1ba0 100644 --- a/desktop-widgets/printerwebengine.h +++ b/desktop-widgets/printerwebengine.h @@ -3,7 +3,7 @@ #define PRINTER_H #include "printoptions.h" -#include "templateedit.h" + #include #include @@ -18,18 +18,18 @@ class Printer : public QObject { Q_OBJECT public: - QWebEngineView *webView; enum PrintMode { PRINT, PREVIEW }; + QWebEngineView *webView; private: QPaintDevice *paintDevice; QPrinter printer; QTemporaryDir printDir; - const print_options &printOptions; - const template_options &templateOptions; + print_options &printOptions; + template_options &templateOptions; QSize pageSize; PrintMode printMode; bool inPlanner; @@ -42,11 +42,14 @@ private slots: void printing(); public: - Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, bool inPlanner); + Printer(QPaintDevice *paintDevice, print_options &printOptions, template_options &templateOptions, PrintMode printMode, bool inPlanner, QWidget *parent); ~Printer(); void print(); void previewOnePage(); + QString writeTmpTemplate(const QString templtext); QString exportHtml(); + void updateOptions(print_options &poptions, template_options &toptions); + signals: void progessUpdated(int value); diff --git a/desktop-widgets/templateedit.cpp b/desktop-widgets/templateedit.cpp index db8a92007..2dc181522 100644 --- a/desktop-widgets/templateedit.cpp +++ b/desktop-widgets/templateedit.cpp @@ -1,15 +1,22 @@ // SPDX-License-Identifier: GPL-2.0 #include "templateedit.h" #include "templatelayout.h" +#ifdef USE_WEBENGINE +#include "printerwebengine.h" +#else #include "printer.h" +#endif #include "ui_templateedit.h" #include #include #include #include +#ifdef USE_WEBENGINE +#include +#endif -TemplateEdit::TemplateEdit(QWidget *parent, const print_options &printOptions, template_options &templateOptions) : +TemplateEdit::TemplateEdit(QWidget *parent, print_options &printOptions, template_options &templateOptions) : QDialog(parent), ui(new Ui::TemplateEdit), printOptions(printOptions), @@ -43,7 +50,20 @@ TemplateEdit::TemplateEdit(QWidget *parent, const print_options &printOptions, t ui->plainTextEdit->setPlainText(grantlee_template); editingCustomColors = false; +#ifdef USE_WEBENGINE + QWebEngineView *view; + preview = new Printer(nullptr, printOptions, newTemplateOptions, Printer::PREVIEW, false, this); + view = preview->webView; + int w = ui->label->width(); + int h = ui->label->height(); + view->resize(w, h); + preview->print(); + view->setZoomFactor(0.25); + view->show(); + ui->verticalLayout_3->replaceWidget(ui->label, view); +#else updatePreview(); +#endif } TemplateEdit::~TemplateEdit() @@ -54,14 +74,16 @@ TemplateEdit::~TemplateEdit() void TemplateEdit::updatePreview() { +#ifndef USE_WEBENGINE // update Qpixmap preview int width = ui->label->width(); int height = ui->label->height(); QPixmap map(width * 2, height * 2); map.fill(QColor::fromRgb(255, 255, 255)); - Printer printer(&map, printOptions, newTemplateOptions, Printer::PREVIEW, false); + Printer printer(&map, printOptions, newTemplateOptions, Printer::PREVIEW, false, nullptr); printer.previewOnePage(); ui->label->setPixmap(map.scaled(width, height, Qt::IgnoreAspectRatio)); +#endif // update colors tab ui->colorLable1->setStyleSheet("QLabel { background-color : \"" + newTemplateOptions.color_palette.color1.name() + "\";}"); @@ -165,6 +187,7 @@ void TemplateEdit::saveSettings() void TemplateEdit::on_buttonBox_clicked(QAbstractButton *button) { QDialogButtonBox::StandardButton standardButton = ui->buttonBox->standardButton(button); + QString template_file = printOptions.p_template; switch (standardButton) { case QDialogButtonBox::Ok: saveSettings(); @@ -172,7 +195,14 @@ void TemplateEdit::on_buttonBox_clicked(QAbstractButton *button) case QDialogButtonBox::Cancel: break; case QDialogButtonBox::Apply: +#ifdef USE_WEBENGINE + printOptions.p_template = preview->writeTmpTemplate(ui->plainTextEdit->toPlainText()); + preview->updateOptions(printOptions, templateOptions); + preview->print(); + printOptions.p_template = template_file; +#else saveSettings(); +#endif updatePreview(); break; default: diff --git a/desktop-widgets/templateedit.h b/desktop-widgets/templateedit.h index c4ebe7969..0b2f7bbab 100644 --- a/desktop-widgets/templateedit.h +++ b/desktop-widgets/templateedit.h @@ -4,6 +4,9 @@ #include #include "printoptions.h" +#ifdef USE_WEBENGINE +#include "printerwebengine.h" +#endif namespace Ui { class TemplateEdit; @@ -14,7 +17,7 @@ class TemplateEdit : public QDialog Q_OBJECT public: - explicit TemplateEdit(QWidget *parent, const print_options &printOptions, template_options &templateOptions); + explicit TemplateEdit(QWidget *parent, print_options &printOptions, template_options &templateOptions); ~TemplateEdit(); private slots: void on_fontsize_valueChanged(int font_size); @@ -29,13 +32,15 @@ private: Ui::TemplateEdit *ui; QButtonGroup *btnGroup; bool editingCustomColors; - const print_options &printOptions; + print_options &printOptions; template_options &templateOptions; struct template_options newTemplateOptions; QString grantlee_template; void saveSettings(); void updatePreview(); - +#ifdef USE_WEBENGINE + Printer *preview; +#endif }; #endif // TEMPLATEEDIT_H diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp index ec895687b..7b58ba6e1 100644 --- a/desktop-widgets/templatelayout.cpp +++ b/desktop-widgets/templatelayout.cpp @@ -152,7 +152,13 @@ QString TemplateLayout::generateStatistics() QString TemplateLayout::readTemplate(QString template_name) { - QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name); + QString filename; + // are we using a temporary file while editing? + if (template_name.contains("ssrftmptemplate.html")) + filename = template_name; + else + filename = getPrintingTemplatePathUser() + QDir::separator() + template_name; + QFile qfile(filename); if (qfile.open(QFile::ReadOnly | QFile::Text)) { QTextStream in(&qfile); return in.readAll();