From 4338bcbacf467c0d0d7aec36fbd186ca6b9e02f6 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Tue, 8 Jun 2021 09:55:32 +0200 Subject: Move printing finished callback to signal We can only have one printer active at a time anyway. At the same time restore some lost documentation. Pick-to: 6.2 Task-number: QTBUG-63235 Change-Id: I454ef3fb038712c832e73c661120fbbe77b3a90c Reviewed-by: Peter Varga --- src/webenginewidgets/api/qwebengineview.cpp | 54 ++++++++++++++++++++++++----- src/webenginewidgets/api/qwebengineview.h | 3 +- 2 files changed, 47 insertions(+), 10 deletions(-) (limited to 'src/webenginewidgets/api') diff --git a/src/webenginewidgets/api/qwebengineview.cpp b/src/webenginewidgets/api/qwebengineview.cpp index 8ea0beba1..047c682df 100644 --- a/src/webenginewidgets/api/qwebengineview.cpp +++ b/src/webenginewidgets/api/qwebengineview.cpp @@ -531,9 +531,9 @@ void QWebEngineViewPrivate::didPrintPage(quint64 requestId, QSharedPointerm_documentCopies = currentPrinter->copyCount(); printerWorker->m_collateCopies = currentPrinter->collateCopies(); - QObject::connect(printerWorker, &QtWebEngineCore::PrinterWorker::resultReady, q, [requestId, this](bool success) { + QObject::connect(printerWorker, &QtWebEngineCore::PrinterWorker::resultReady, q, [this, q](bool success) { currentPrinter = nullptr; - page->d_ptr->m_callbacks.invoke(requestId, success); + Q_EMIT q->printFinished(success); }); QObject::connect(printerWorker, &QtWebEngineCore::PrinterWorker::resultReady, printerThread, &QThread::quit); @@ -980,25 +980,61 @@ void QWebEngineView::printToPdf(const QWebEngineCallback &res #endif } -void QWebEngineView::print(QPrinter *printer, const QWebEngineCallback &resultCallback) +/*! + \fn void QWebEngineView::printRequested() + \since 6.2 + + This signal is emitted when the JavaScript \c{window.print()} method is called. + Typically, the signal handler can simply call printToPdf(). + + \sa printToPdf() +*/ + +/*! + \fn void QWebEngineView::printFinished(bool success) + \since 6.2 + + This signal is emitted when printing requested with print() has finished. + + \sa print() +*/ + +/*! + Renders the current content of the page into a temporary PDF document, then prints it using \a printer. + + The settings for creating and printing the PDF document will be retrieved from the \a printer + object. + + When finished the signal printFinished() is emitted with the \c true for success or \c false for failure. + + It is the users responsibility to ensure the \a printer remains valid until printFinished() + has been emitted. + + \note Printing runs on the browser process, which is by default not sandboxed. + + \note This function rasterizes the result when rendering onto \a printer. Please consider raising + the default resolution of \a printer to at least 300 DPI or using printToPdf() to produce + PDF file output more effectively. + + \since 6.2 +*/ +void QWebEngineView::print(QPrinter *printer) { #if QT_CONFIG(webengine_printing_and_pdf) Q_D(QWebEngineView); if (d->currentPrinter) { qWarning("Cannot print page on printer %ls: Already printing on a device.", qUtf16Printable(printer->printerName())); - page()->d_ptr->m_callbacks.invokeDirectly(resultCallback, false); return; } d->currentPrinter = printer; page()->d_ptr->ensureInitialized(); - quint64 requestId = page()->d_ptr->adapter->printToPDFCallbackResult(printer->pageLayout(), - printer->colorMode() == QPrinter::Color, - false); - page()->d_ptr->m_callbacks.registerCallback(requestId, resultCallback); + page()->d_ptr->adapter->printToPDFCallbackResult(printer->pageLayout(), + printer->colorMode() == QPrinter::Color, + false); #else Q_UNUSED(printer); - page()->d_ptr->m_callbacks.invokeDirectly(resultCallback, false); + Q_EMIT printFinished(false); #endif } diff --git a/src/webenginewidgets/api/qwebengineview.h b/src/webenginewidgets/api/qwebengineview.h index 22c115bd1..90a2d4118 100644 --- a/src/webenginewidgets/api/qwebengineview.h +++ b/src/webenginewidgets/api/qwebengineview.h @@ -110,7 +110,7 @@ public: void printToPdf(const QString &filePath, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF())); void printToPdf(const QWebEngineCallback &resultCallback, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF())); - void print(QPrinter *printer, const QWebEngineCallback &resultCallback); + void print(QPrinter *printer); public Q_SLOTS: void stop(); @@ -131,6 +131,7 @@ Q_SIGNALS: int exitCode); void pdfPrintingFinished(const QString &filePath, bool success); void printRequested(); + void printFinished(bool success); protected: virtual QWebEngineView *createWindow(QWebEnginePage::WebWindowType type); -- cgit v1.2.3