From 3ad5d3bd576b0ff8e5465c2e0e17f60ebe0751a8 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 24 May 2018 11:54:24 +0200 Subject: [PATCH] added save file option (keep filename) --- source/mainwindow.cpp | 46 ++++++++++++++++++++++++++++++++----------- source/mainwindow.h | 5 +++++ source/mainwindow.ui | 22 +++++++++++++++------ 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index ec9393f..e6c2d84 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -17,7 +17,8 @@ MainWindow::MainWindow(QWidget *parent) connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile())); connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile())); - connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveFileAs())); + connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(saveFile())); + connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveFileAs())); newFile(); } @@ -33,6 +34,8 @@ void MainWindow::newFile() ui->metaDataWidget->setModel(&m_dataModel->m_metaData); ui->verbEndWidget->setModel(&m_dataModel->m_verbEnd); ui->genusWidget->setModel(&m_dataModel->m_genus); + + m_filename = ""; } void MainWindow::openFile() @@ -55,7 +58,21 @@ void MainWindow::openFile() m_dataModel->read(loadDoc.object()); - ui->metaDataWidget->toFirst(); + m_filename = filename; + + ui->metaDataWidget->toFirst(); +} + +void MainWindow::saveFile() +{ + if (m_filename.isEmpty()) + { + saveFileAs(); + } + else + { + saveFile(m_filename); + } } void MainWindow::saveFileAs() @@ -66,13 +83,20 @@ void MainWindow::saveFileAs() return; } - QJsonObject saveData; - m_dataModel->write(saveData); - - QJsonDocument saveDoc(saveData); - - QFile saveFile(filename); - saveFile.open(QFile::WriteOnly); - saveFile.write(saveDoc.toJson()); - saveFile.close(); + saveFile(filename); +} + +void MainWindow::saveFile(const QString &filename) +{ + QJsonObject saveData; + m_dataModel->write(saveData); + + QJsonDocument saveDoc(saveData); + + QFile saveFile(filename); + saveFile.open(QFile::WriteOnly); + saveFile.write(saveDoc.toJson()); + saveFile.close(); + + m_filename = filename; } diff --git a/source/mainwindow.h b/source/mainwindow.h index 96c572b..8d04176 100644 --- a/source/mainwindow.h +++ b/source/mainwindow.h @@ -18,6 +18,7 @@ class MainWindow : public QMainWindow private: Ui::MainWindow *ui; std::unique_ptr m_dataModel; + QString m_filename; public: MainWindow(QWidget *parent = nullptr); @@ -26,5 +27,9 @@ public: public slots: void newFile(); void openFile(); + void saveFile(); void saveFileAs(); + +private: + void saveFile(const QString &filename); }; diff --git a/source/mainwindow.ui b/source/mainwindow.ui index 730c748..3c7b8a9 100644 --- a/source/mainwindow.ui +++ b/source/mainwindow.ui @@ -18,7 +18,7 @@ - 1 + 0 @@ -60,7 +60,7 @@ 0 0 905 - 19 + 17 @@ -69,6 +69,7 @@ + @@ -87,7 +88,7 @@ - + @@ -100,9 +101,6 @@ Save as... - - Ctrl+S - @@ -136,6 +134,18 @@ Ctrl+Q + + + + :/images/document-save-as.png:/images/document-save-as.png + + + Save + + + Ctrl+S + +