]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/commitdiff
Implement printing.
authorSoren Stoutner <soren@stoutner.com>
Wed, 29 Jun 2022 19:47:21 +0000 (12:47 -0700)
committerSoren Stoutner <soren@stoutner.com>
Wed, 29 Jun 2022 19:47:21 +0000 (12:47 -0700)
src/views/BrowserView.cpp
src/views/BrowserView.h
src/windows/BrowserWindow.cpp

index e7f238fc9beacb174d292a77d6c6e5fb58d6ebe3..b3f593ff93352a1d023a895207cb5832af21e036 100644 (file)
@@ -31,6 +31,9 @@
 
 // Qt framework headers.
 #include <QAction>
+#include <QPrintDialog>
+#include <QPrintPreviewDialog>
+#include <QPrinter>
 
 // 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<bool>`.
+    // 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.
index b256ec253c4d7bff353817bca34a59a2f232090c..e2b52758dbd14fe3cdc30499b8b4c88e32e08144 100644 (file)
@@ -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:
index e84f9574835bdf1ad6c04df0304e518d1ee7c9cb..33aef29f77ad309caf13e4acc3ad3d5c6b1cf30a 100644 (file)
@@ -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);