From bdf9706e58a82c301f0013567f6cee359169e492 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 9 Dec 2018 11:11:55 +0100 Subject: [PATCH] Added pdf export --- source/mainwindow.cpp | 30 ++++++++++++++++++++++++++++++ source/mainwindow.h | 2 ++ source/mainwindow.ui | 9 +++++++++ 3 files changed, 41 insertions(+) diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index cf307db..e91549a 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -56,6 +56,7 @@ void MainWindow::setupUi() connect(ui->actionSave, &QAction::triggered, this, qOverload<>(&MainWindow::saveFile)); connect(ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs); connect(ui->actionPrint, &QAction::triggered, this, &MainWindow::print); + connect(ui->actionExport_PDF, &QAction::triggered, this, qOverload<>(&MainWindow::savePdf)); connect(&m_dataModel, &DataModel::modelChanged, this, &MainWindow::dataModelChanged); } @@ -172,6 +173,22 @@ void MainWindow::dataModelChanged() setWindowModified(true); } +void MainWindow::savePdf() +{ + QFileDialog saveFilenameDialog(this); + saveFilenameDialog.setDefaultSuffix("pdf"); + saveFilenameDialog.setFileMode(QFileDialog::AnyFile); + saveFilenameDialog.setNameFilter("PDF File (*.pdf)"); + saveFilenameDialog.setWindowTitle("Save file"); + + if (!saveFilenameDialog.exec()) + { + return; + } + + savePdf(saveFilenameDialog.selectedFiles().first()); +} + void MainWindow::closeEvent(QCloseEvent *event) { closeFile(); @@ -190,3 +207,16 @@ void MainWindow::saveFile(const QString &filename) m_filename = filename; m_saveOnClose = false; } + +void MainWindow::savePdf(const QString &filename) +{ + QPrinter printer; + printer.setOutputFormat(QPrinter::PdfFormat); + printer.setPaperSize(QPrinter::A4); + printer.setOutputFileName(filename); + + QTextDocument printDoc; + printDoc.setHtml(QString::fromStdString(m_dataModel.toHtml())); + + printDoc.print(&printer); +} diff --git a/source/mainwindow.h b/source/mainwindow.h index c2cf130..9675fb7 100644 --- a/source/mainwindow.h +++ b/source/mainwindow.h @@ -37,6 +37,7 @@ public slots: void closeFile(); void print() const; void dataModelChanged(); + void savePdf(); protected: void closeEvent(QCloseEvent *event) override; @@ -44,4 +45,5 @@ protected: private: void setupUi(); void saveFile(const QString &filename); + void savePdf(const QString &filename); }; diff --git a/source/mainwindow.ui b/source/mainwindow.ui index 86d0199..9622ce3 100644 --- a/source/mainwindow.ui +++ b/source/mainwindow.ui @@ -133,6 +133,7 @@ + @@ -228,6 +229,14 @@ Ctrl+P + + + Export PDF + + + Export as PDF file + +