summaryrefslogtreecommitdiffstats
path: root/src/webenginewidgets/api/qwebengineview.cpp
diff options
context:
space:
mode:
authorSzabolcs David <davidsz@inf.u-szeged.hu>2021-06-03 01:37:37 +0200
committerSzabolcs David <davidsz@inf.u-szeged.hu>2021-06-03 14:05:22 +0200
commit4943d9801ccad59aef073374644fc991e49987e3 (patch)
tree4a4495c6a3391aa1bc4ad42d1548ff2f1bc9e46e /src/webenginewidgets/api/qwebengineview.cpp
parent57140466f277a074b288a515b2bdfd26c8a3cbe3 (diff)
Drop printsupport dependency from core
Prevent linkage of core to widgets by moving printing API from QWebEnginePage to View and using QPagedPaintDevice (the QtGui ancestor of QPrinter) where it's needed. Change-Id: I6ea96edb495b0dcaaa584bbe72632fda025c18d3 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/webenginewidgets/api/qwebengineview.cpp')
-rw-r--r--src/webenginewidgets/api/qwebengineview.cpp126
1 files changed, 125 insertions, 1 deletions
diff --git a/src/webenginewidgets/api/qwebengineview.cpp b/src/webenginewidgets/api/qwebengineview.cpp
index 9ce954e06..a34c841e3 100644
--- a/src/webenginewidgets/api/qwebengineview.cpp
+++ b/src/webenginewidgets/api/qwebengineview.cpp
@@ -80,6 +80,13 @@
#include <QStyle>
#include <QGuiApplication>
+#if QT_CONFIG(webengine_printing_and_pdf)
+#include "printing/printer_worker.h"
+
+#include <QPrinter>
+#include <QThread>
+#endif
+
QT_BEGIN_NAMESPACE
void QWebEngineViewPrivate::pageChanged(QWebEnginePage *oldPage, QWebEnginePage *newPage)
@@ -344,7 +351,13 @@ static QAccessibleInterface *webAccessibleFactory(const QString &, QObject *obje
#endif // QT_NO_ACCESSIBILITY
QWebEngineViewPrivate::QWebEngineViewPrivate()
- : page(0), m_dragEntered(false), m_ownsPage(false), m_contextRequest(nullptr)
+ : page(0)
+ , m_dragEntered(false)
+ , m_ownsPage(false)
+ , m_contextRequest(nullptr)
+#if QT_CONFIG(webengine_printing_and_pdf)
+ , currentPrinter(nullptr)
+#endif
{
#ifndef QT_NO_ACCESSIBILITY
QAccessible::installFactory(&webAccessibleFactory);
@@ -495,6 +508,61 @@ QObject *QWebEngineViewPrivate::accessibilityParentObject()
return q;
}
+void QWebEngineViewPrivate::didPrintPage(quint64 requestId, QSharedPointer<QByteArray> result)
+{
+#if QT_CONFIG(webengine_printing_and_pdf)
+ Q_Q(QWebEngineView);
+
+ // If no currentPrinter is set that means that were printing to PDF only.
+ if (!currentPrinter) {
+ if (!result.data())
+ return;
+ page->d_ptr->m_callbacks.invoke(requestId, *(result.data()));
+ return;
+ }
+
+ QThread *printerThread = new QThread;
+ QObject::connect(printerThread, &QThread::finished, printerThread, &QThread::deleteLater);
+ printerThread->start();
+
+ QtWebEngineCore::PrinterWorker *printerWorker = new QtWebEngineCore::PrinterWorker(result, currentPrinter);
+ printerWorker->m_deviceResolution = currentPrinter->resolution();
+ printerWorker->m_firstPageFirst = currentPrinter->pageOrder() == QPrinter::FirstPageFirst;
+ printerWorker->m_documentCopies = currentPrinter->copyCount();
+ printerWorker->m_collateCopies = currentPrinter->collateCopies();
+
+ QObject::connect(printerWorker, &QtWebEngineCore::PrinterWorker::resultReady, q, [requestId, this](bool success) {
+ currentPrinter = nullptr;
+ page->d_ptr->m_callbacks.invoke(requestId, success);
+ });
+
+ QObject::connect(printerWorker, &QtWebEngineCore::PrinterWorker::resultReady, printerThread, &QThread::quit);
+ QObject::connect(printerThread, &QThread::finished, printerWorker, &QtWebEngineCore::PrinterWorker::deleteLater);
+
+ printerWorker->moveToThread(printerThread);
+ QMetaObject::invokeMethod(printerWorker, "print");
+
+#else
+ // we should never enter this branch, but just for safe-keeping...
+ Q_UNUSED(result);
+ page->d_ptr->m_callbacks.invoke(requestId, QByteArray());
+#endif
+}
+
+void QWebEngineViewPrivate::didPrintPageToPdf(const QString &filePath, bool success)
+{
+ Q_Q(QWebEngineView);
+ Q_EMIT q->pdfPrintingFinished(filePath, success);
+}
+
+void QWebEngineViewPrivate::printRequested()
+{
+ Q_Q(QWebEngineView);
+ QTimer::singleShot(0, q, [q]() {
+ Q_EMIT q->printRequested();
+ });
+}
+
bool QWebEngineViewPrivate::isVisible() const
{
Q_Q(const QWebEngineView);
@@ -878,6 +946,62 @@ QWebEngineContextMenuRequest *QWebEngineView::lastContextMenuRequest() const
return d->m_contextRequest;
}
+void QWebEngineView::printToPdf(const QString &filePath, const QPageLayout &layout)
+{
+#if QT_CONFIG(webengine_printing_and_pdf)
+ Q_D(QWebEngineView);
+ if (d->currentPrinter) {
+ qWarning("Cannot print to PDF while printing at the same time.");
+ return;
+ }
+ page()->d_ptr->ensureInitialized();
+ page()->d_ptr->adapter->printToPDF(layout, filePath);
+#else
+ Q_UNUSED(filePath);
+ Q_UNUSED(layout);
+#endif
+}
+
+void QWebEngineView::printToPdf(const QWebEngineCallback<const QByteArray&> &resultCallback, const QPageLayout &layout)
+{
+#if QT_CONFIG(webengine_printing_and_pdf)
+ Q_D(QWebEngineView);
+ if (d->currentPrinter) {
+ qWarning("Cannot print to PDF while printing at the same time.");
+ page()->d_ptr->m_callbacks.invokeEmpty(resultCallback);
+ return;
+ }
+ page()->d_ptr->ensureInitialized();
+ quint64 requestId = page()->d_ptr->adapter->printToPDFCallbackResult(layout);
+ page()->d_ptr->m_callbacks.registerCallback(requestId, resultCallback);
+#else
+ Q_UNUSED(layout);
+ page()->d_ptr->m_callbacks.invokeEmpty(resultCallback);
+#endif
+}
+
+void QWebEngineView::print(QPrinter *printer, const QWebEngineCallback<bool> &resultCallback)
+{
+#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);
+#else
+ Q_UNUSED(printer);
+ page()->d_ptr->m_callbacks.invokeDirectly(resultCallback, false);
+#endif
+}
+
#ifndef QT_NO_ACCESSIBILITY
bool QWebEngineViewAccessible::isValid() const
{