added save file option (keep filename)

This commit is contained in:
Michael Mandl 2018-05-24 11:54:24 +02:00
parent 95eebd4112
commit 3ad5d3bd57
3 changed files with 56 additions and 17 deletions

View file

@ -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;
}

View file

@ -18,6 +18,7 @@ class MainWindow : public QMainWindow
private:
Ui::MainWindow *ui;
std::unique_ptr<DataModel> 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);
};

View file

@ -18,7 +18,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="MetaDataTab">
<attribute name="title">
@ -60,7 +60,7 @@
<x>0</x>
<y>0</y>
<width>905</width>
<height>19</height>
<height>17</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -69,6 +69,7 @@
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
<addaction name="actionSave_as"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
@ -87,7 +88,7 @@
</attribute>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionSave_as"/>
<addaction name="actionSave"/>
</widget>
<action name="actionSave_as">
<property name="icon">
@ -100,9 +101,6 @@
<property name="toolTip">
<string>Save as...</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
<action name="actionOpen">
<property name="icon">
@ -136,6 +134,18 @@
<string>Ctrl+Q</string>
</property>
</action>
<action name="actionSave">
<property name="icon">
<iconset resource="mainwindow.qrc">
<normaloff>:/images/document-save-as.png</normaloff>:/images/document-save-as.png</iconset>
</property>
<property name="text">
<string>Save</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>