From 273588ac3f6077b62c1fb2b55b9cdc6b5e200fad Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 29 Jun 2022 12:47:21 -0700 Subject: [PATCH] Implement printing. --- src/views/BrowserView.cpp | 57 +++++++++++++++++++++++++++++++++++ src/views/BrowserView.h | 3 ++ src/windows/BrowserWindow.cpp | 2 ++ 3 files changed, 62 insertions(+) diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index e7f238f..b3f593f 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -31,6 +31,9 @@ // Qt framework headers. #include +#include +#include +#include // Initialize the public static variables. QString BrowserView::webEngineDefaultUserAgent = QStringLiteral(""); @@ -577,6 +580,60 @@ void BrowserView::pageLinkHovered(const QString &linkUrl) const emit linkHovered(linkUrl); } +void BrowserView::print() const +{ + // Create a printer. + QPrinter printer; + + // Set the resolution to be 300 dpi. + printer.setResolution(300); + + // Create a printer dialog. + QPrintDialog printDialog(&printer, webEngineViewPointer); + + // Display the dialog and print the page if instructed. + if (printDialog.exec() == QDialog::Accepted) + printWebpage(&printer); +} + +void BrowserView::printPreview() const +{ + // Create a printer. + QPrinter printer; + + // Set the resolution to be 300 dpi. + printer.setResolution(300); + + // Create a print preview dialog. + QPrintPreviewDialog printPreviewDialog(&printer, webEngineViewPointer); + + // Generate the print preview. + connect(&printPreviewDialog, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printWebpage(QPrinter *))); + + // Display the dialog. + printPreviewDialog.exec(); +} + +void BrowserView::printWebpage(QPrinter *printerPointer) const +{ + // Create an event loop. For some reason, the print preview doesn't produce any output unless it is run inside an event loop. + QEventLoop eventLoop; + + // Print the webpage, converting the callback above into a `QWebEngineCallback`. + // Printing requires that the printer be a pointer, not a reference, or it will crash with much cursing. + webEnginePagePointer->print(printerPointer, [&eventLoop](bool printSuccess) + { + // Instruct the compiler to ignore the unused parameter. + (void) printSuccess; + + // Quit the loop. + eventLoop.quit(); + }); + + // Execute the loop. + eventLoop.exec(); +} + void BrowserView::refresh() const { // Reload the website. diff --git a/src/views/BrowserView.h b/src/views/BrowserView.h index b256ec2..e2b5275 100644 --- a/src/views/BrowserView.h +++ b/src/views/BrowserView.h @@ -92,6 +92,8 @@ public Q_SLOTS: void loadUrlFromLineEdit(QString url) const; void mouseBack() const; void mouseForward() const; + void print() const; + void printPreview() const; void refresh() const; private Q_SLOTS: @@ -103,6 +105,7 @@ private Q_SLOTS: void loadProgress(const int &progress) const; void loadStarted() const; void pageLinkHovered(const QString &linkUrl) const; + void printWebpage(QPrinter *printerPointer) const; void updateUrl(const QUrl &url) const; private: diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index e84f957..33aef29 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -58,6 +58,8 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Add the standard actions. KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer); + KStandardAction::print(browserViewPointer, SLOT(print()), actionCollectionPointer); + KStandardAction::printPreview(browserViewPointer, SLOT(printPreview()), actionCollectionPointer); KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer); KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer); fullScreenActionPointer = KStandardAction::fullScreen(this, SLOT(toggleFullScreen()), this, actionCollectionPointer); -- 2.43.0