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 <dirk@hohndel.org>
This commit is contained in:
parent
e5915ca4f2
commit
a0057c06fb
@ -25,7 +25,7 @@ Printer::Printer(QPaintDevice *paintDevice, print_options &printOptions, templat
|
|||||||
webView = new QWebEngineView(parent);
|
webView = new QWebEngineView(parent);
|
||||||
connect(webView, &QWebEngineView::loadFinished, this, &Printer::onLoadFinished);
|
connect(webView, &QWebEngineView::loadFinished, this, &Printer::onLoadFinished);
|
||||||
if (printMode == PRINT) {
|
if (printMode == PRINT) {
|
||||||
connect(this, &Printer::profilesInserted, this, &Printer::printing);
|
connect(this, &Printer::profilesInserted, this, &Printer::reopen);
|
||||||
connect(this, &Printer::jobDone, this, &Printer::printFinished);
|
connect(this, &Printer::jobDone, this, &Printer::printFinished);
|
||||||
}
|
}
|
||||||
profilesMissing = true;
|
profilesMissing = true;
|
||||||
@ -56,18 +56,42 @@ void Printer::onLoadFinished()
|
|||||||
jsText.replace("TMPPATH", filePath);
|
jsText.replace("TMPPATH", filePath);
|
||||||
webView->page()->runJavaScript(jsText, [this](const QVariant &v) {
|
webView->page()->runJavaScript(jsText, [this](const QVariant &v) {
|
||||||
qDebug() << "JS finished";
|
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();
|
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()
|
void Printer::printing()
|
||||||
{
|
{
|
||||||
QPrintDialog printDialog(&printer, (QWidget *) nullptr);
|
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(); });
|
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();
|
printDialog.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +160,6 @@ void Printer::print()
|
|||||||
connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
|
connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
|
||||||
int dpi = printer.resolution();
|
int dpi = printer.resolution();
|
||||||
webView->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
|
webView->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
|
||||||
connect(webView, &QWebEngineView::loadFinished, this, &Printer::onLoadFinished);
|
|
||||||
|
|
||||||
if (printOptions.type == print_options::DIVELIST) {
|
if (printOptions.type == print_options::DIVELIST) {
|
||||||
QFile printFile(printDir.filePath("print.html"));
|
QFile printFile(printDir.filePath("print.html"));
|
||||||
|
|||||||
@ -41,6 +41,7 @@ private slots:
|
|||||||
void templateProgessUpdated(int value);
|
void templateProgessUpdated(int value);
|
||||||
void printing();
|
void printing();
|
||||||
void printFinished();
|
void printFinished();
|
||||||
|
void reopen();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Printer(QPaintDevice *paintDevice, print_options &printOptions, template_options &templateOptions, PrintMode printMode, bool inPlanner, QWidget *parent);
|
Printer(QPaintDevice *paintDevice, print_options &printOptions, template_options &templateOptions, PrintMode printMode, bool inPlanner, QWidget *parent);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user