From a0057c06fba6a5667b6fefa23e89cc1fe829e8af Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sun, 28 Nov 2021 17:26:01 -0800 Subject: [PATCH] printing: convert to two stage approach The double connect to onLoadFinished clearly was incorrect, but even with that removed we still seemed to call the printer before the modified page finished loading. Moving the profilesMissing = false setting into the callback also seems obviously correct, but also wasn't enough to make this work. So in the end I went the brute force route. We run the Javascript code to insert the profile images (which is done in JS to stay compatible with existing templates - yes, this could be reimplemented differently using Qt primitives - but that would result in parsing HTML code and that really is what WebEngine is for and the JS code is known to work...). And then we write the new HTML out to a file and load that file. And once THAT file finishes loading (now with all the profiles - if there were any in the template in the first place), send that to be printed. This seems convoluted and silly - but I simply couldn't get a single step flow to work on openSUSE (which is where this was tested and implemented). Signed-off-by: Dirk Hohndel --- desktop-widgets/printerwebengine.cpp | 33 +++++++++++++++++++++++----- desktop-widgets/printerwebengine.h | 1 + 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/desktop-widgets/printerwebengine.cpp b/desktop-widgets/printerwebengine.cpp index 9bb5d3272..2ba3880be 100644 --- a/desktop-widgets/printerwebengine.cpp +++ b/desktop-widgets/printerwebengine.cpp @@ -25,7 +25,7 @@ Printer::Printer(QPaintDevice *paintDevice, print_options &printOptions, templat webView = new QWebEngineView(parent); connect(webView, &QWebEngineView::loadFinished, this, &Printer::onLoadFinished); if (printMode == PRINT) { - connect(this, &Printer::profilesInserted, this, &Printer::printing); + connect(this, &Printer::profilesInserted, this, &Printer::reopen); connect(this, &Printer::jobDone, this, &Printer::printFinished); } profilesMissing = true; @@ -56,18 +56,42 @@ void Printer::onLoadFinished() jsText.replace("TMPPATH", filePath); webView->page()->runJavaScript(jsText, [this](const QVariant &v) { qDebug() << "JS finished"; + QFile fd(printDir.filePath("finalprint.html")); + fd.open(QIODevice::WriteOnly | QIODevice::Text); + QTextStream out(&fd); + out << v.toString(); + fd.close(); + qDebug() << "finalprint.html written"; + profilesMissing = false; + emit(progessUpdated(80)); emit profilesInserted(); }); + } else { + qDebug() << "load of" << webView->url().toString() << "finished - open printing dialog"; + emit(progessUpdated(100)); + printing(); } - profilesMissing = false; - emit(progessUpdated(100)); +} + +// the Javascript code injecting the profiles has completed, let's reload that page +void Printer::reopen() +{ + qDebug() << "opening the finalprint.html file"; + webView->load(QUrl::fromLocalFile(printDir.filePath("finalprint.html"))); } void Printer::printing() { QPrintDialog printDialog(&printer, (QWidget *) nullptr); if (printDialog.exec() == QDialog::Accepted) - webView->page()->print(&printer, [this](bool ok){ if (ok) emit jobDone(); }); + webView->page()->print(&printer, [this](bool ok){ + if (ok) + emit jobDone(); + qDebug() << "printing done with status " << ok ; + qDebug() << "content of" << printDir.path(); + qDebug() << QDir(printDir.path()).entryList(); + }); + qDebug() << "closing the dialog now"; printDialog.close(); } @@ -136,7 +160,6 @@ void Printer::print() 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")); diff --git a/desktop-widgets/printerwebengine.h b/desktop-widgets/printerwebengine.h index 25d4d7941..db8597327 100644 --- a/desktop-widgets/printerwebengine.h +++ b/desktop-widgets/printerwebengine.h @@ -41,6 +41,7 @@ private slots: void templateProgessUpdated(int value); void printing(); void printFinished(); + void reopen(); public: Printer(QPaintDevice *paintDevice, print_options &printOptions, template_options &templateOptions, PrintMode printMode, bool inPlanner, QWidget *parent);