Added pdf export

This commit is contained in:
Michael Mandl 2018-12-09 11:11:55 +01:00
parent f38b9194fd
commit bdf9706e58
3 changed files with 41 additions and 0 deletions

View file

@ -56,6 +56,7 @@ void MainWindow::setupUi()
connect(ui->actionSave, &QAction::triggered, this, qOverload<>(&MainWindow::saveFile)); connect(ui->actionSave, &QAction::triggered, this, qOverload<>(&MainWindow::saveFile));
connect(ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs); connect(ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs);
connect(ui->actionPrint, &QAction::triggered, this, &MainWindow::print); 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); connect(&m_dataModel, &DataModel::modelChanged, this, &MainWindow::dataModelChanged);
} }
@ -172,6 +173,22 @@ void MainWindow::dataModelChanged()
setWindowModified(true); 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) void MainWindow::closeEvent(QCloseEvent *event)
{ {
closeFile(); closeFile();
@ -190,3 +207,16 @@ void MainWindow::saveFile(const QString &filename)
m_filename = filename; m_filename = filename;
m_saveOnClose = false; 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);
}

View file

@ -37,6 +37,7 @@ public slots:
void closeFile(); void closeFile();
void print() const; void print() const;
void dataModelChanged(); void dataModelChanged();
void savePdf();
protected: protected:
void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
@ -44,4 +45,5 @@ protected:
private: private:
void setupUi(); void setupUi();
void saveFile(const QString &filename); void saveFile(const QString &filename);
void savePdf(const QString &filename);
}; };

View file

@ -133,6 +133,7 @@
<addaction name="actionSave_as"/> <addaction name="actionSave_as"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionPrint"/> <addaction name="actionPrint"/>
<addaction name="actionExport_PDF"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionQuit"/> <addaction name="actionQuit"/>
</widget> </widget>
@ -228,6 +229,14 @@
<string>Ctrl+P</string> <string>Ctrl+P</string>
</property> </property>
</action> </action>
<action name="actionExport_PDF">
<property name="text">
<string>Export PDF</string>
</property>
<property name="toolTip">
<string>Export as PDF file</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>