From 15f1492d96b979d848b506f69350881125280d2d Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Thu, 26 Jul 2018 13:45:34 +0200 Subject: [PATCH] Connect PrintAction. This display a dialog to select the printer and its options. The dialog is awful because it reuses the application stylesheet but it works. --- src/kiwixapp.cpp | 22 +++++++++++++++++++++- src/kiwixapp.h | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index edf1c10..4365d2b 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include KiwixApp::KiwixApp(int& argc, char *argv[]) : QApplication(argc, argv) @@ -97,6 +99,23 @@ void KiwixApp::openZimFile(const QString &zimfile) openUrl(QUrl("zim://"+zimId+"/")); } +void KiwixApp::printPage() +{ + QPrinter* printer = new QPrinter(); + QPrintDialog printDialog(printer, mp_mainWindow); + printDialog.setStyle(nullptr); + printDialog.setStyleSheet(""); + if (printDialog.exec() == QDialog::Accepted) { + auto webview = mp_tabWidget->currentWidget(); + webview->page()->print(printer, [=](bool success) { + if (!success) { + showMessage("An error has occured while printing."); + } + delete printer; + }); + } +} + void KiwixApp::openUrl(const QUrl &url, bool newTab) { mp_tabWidget->openUrl(url, newTab); } @@ -131,7 +150,8 @@ void KiwixApp::createAction() CREATE_ACTION_ICON(PrintAction, "print", "Print"); SET_SHORTCUT(PrintAction, QKeySequence::Print); - DISABLE_ACTION(PrintAction); + connect(mpa_actions[PrintAction], &QAction::triggered, + this, &KiwixApp::printPage); CREATE_ACTION(NewTabAction, "New tab"); SET_SHORTCUT(NewTabAction, QKeySequence::AddTab); diff --git a/src/kiwixapp.h b/src/kiwixapp.h index a4214b9..4a5e16a 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -65,6 +65,7 @@ public: public slots: void openZimFile(const QString& zimfile=""); + void printPage(); protected: void createAction();