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->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile())); 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(); newFile();
} }
@ -33,6 +34,8 @@ void MainWindow::newFile()
ui->metaDataWidget->setModel(&m_dataModel->m_metaData); ui->metaDataWidget->setModel(&m_dataModel->m_metaData);
ui->verbEndWidget->setModel(&m_dataModel->m_verbEnd); ui->verbEndWidget->setModel(&m_dataModel->m_verbEnd);
ui->genusWidget->setModel(&m_dataModel->m_genus); ui->genusWidget->setModel(&m_dataModel->m_genus);
m_filename = "";
} }
void MainWindow::openFile() void MainWindow::openFile()
@ -55,7 +58,21 @@ void MainWindow::openFile()
m_dataModel->read(loadDoc.object()); 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() void MainWindow::saveFileAs()
@ -66,13 +83,20 @@ void MainWindow::saveFileAs()
return; return;
} }
QJsonObject saveData; saveFile(filename);
m_dataModel->write(saveData); }
QJsonDocument saveDoc(saveData); void MainWindow::saveFile(const QString &filename)
{
QFile saveFile(filename); QJsonObject saveData;
saveFile.open(QFile::WriteOnly); m_dataModel->write(saveData);
saveFile.write(saveDoc.toJson());
saveFile.close(); 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: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
std::unique_ptr<DataModel> m_dataModel; std::unique_ptr<DataModel> m_dataModel;
QString m_filename;
public: public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
@ -26,5 +27,9 @@ public:
public slots: public slots:
void newFile(); void newFile();
void openFile(); void openFile();
void saveFile();
void saveFileAs(); void saveFileAs();
private:
void saveFile(const QString &filename);
}; };

View file

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