summaryrefslogtreecommitdiffstats
path: root/src/webenginewidgets/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/webenginewidgets/api')
-rw-r--r--src/webenginewidgets/api/qwebengineview.cpp126
-rw-r--r--src/webenginewidgets/api/qwebengineview.h7
-rw-r--r--src/webenginewidgets/api/qwebengineview_p.h8
3 files changed, 140 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
{
diff --git a/src/webenginewidgets/api/qwebengineview.h b/src/webenginewidgets/api/qwebengineview.h
index d7f9d6a45..136df0b93 100644
--- a/src/webenginewidgets/api/qwebengineview.h
+++ b/src/webenginewidgets/api/qwebengineview.h
@@ -51,6 +51,7 @@
QT_BEGIN_NAMESPACE
class QContextMenuEvent;
+class QPrinter;
class QUrl;
class QWebEnginePage;
class QWebEngineSettings;
@@ -107,6 +108,10 @@ public:
#endif
QWebEngineContextMenuRequest *lastContextMenuRequest() const;
+ void printToPdf(const QString &filePath, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF()));
+ void printToPdf(const QWebEngineCallback<const QByteArray&> &resultCallback, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF()));
+ void print(QPrinter *printer, const QWebEngineCallback<bool> &resultCallback);
+
public Q_SLOTS:
void stop();
void back();
@@ -124,6 +129,8 @@ Q_SIGNALS:
void iconChanged(const QIcon&);
void renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus,
int exitCode);
+ void pdfPrintingFinished(const QString &filePath, bool success);
+ void printRequested();
protected:
virtual QWebEngineView *createWindow(QWebEnginePage::WebWindowType type);
diff --git a/src/webenginewidgets/api/qwebengineview_p.h b/src/webenginewidgets/api/qwebengineview_p.h
index 40f195522..71e7d04f7 100644
--- a/src/webenginewidgets/api/qwebengineview_p.h
+++ b/src/webenginewidgets/api/qwebengineview_p.h
@@ -58,6 +58,7 @@
#include <QtWidgets/qaccessiblewidget.h>
namespace QtWebEngineCore {
+class QPrinter;
class RenderWidgetHostViewQtDelegateWidget;
class RenderWidgetHostViewQtDelegate;
}
@@ -92,6 +93,10 @@ public:
QWebEngineContextMenuRequest *lastContextMenuRequest() const override;
QWebEnginePage *createPageForWindow(QWebEnginePage::WebWindowType type) override;
QObject *accessibilityParentObject() override;
+ void didPrintPage(quint64 requestId, QSharedPointer<QByteArray> result) override;
+ void didPrintPageToPdf(const QString &filePath, bool success) override;
+ void printRequested() override;
+
QWebEngineViewPrivate();
virtual ~QWebEngineViewPrivate();
static void bindPageAndView(QWebEnginePage *page, QWebEngineView *view);
@@ -108,6 +113,9 @@ public:
bool m_dragEntered;
mutable bool m_ownsPage;
QWebEngineContextMenuRequest *m_contextRequest;
+#if QT_CONFIG(webengine_printing_and_pdf)
+ QPrinter *currentPrinter;
+#endif
};
#ifndef QT_NO_ACCESSIBILITY