Merge branch 'develop' into feature/print-as-qtextdocument

This commit is contained in:
mandlm 2018-12-09 11:13:16 +01:00
commit e89f157bac
3 changed files with 42 additions and 2 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);
} }
@ -162,8 +163,6 @@ void MainWindow::print() const
m_dataModel.printTo(printCursor); m_dataModel.printTo(printCursor);
printDoc.print(&printer); printDoc.print(&printer);
printDoc.print(&printer);
} }
void MainWindow::dataModelChanged() void MainWindow::dataModelChanged()
@ -172,6 +171,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 +205,17 @@ 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;
QTextCursor printCursor(&printDoc);
m_dataModel.printTo(printCursor);
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>