Fix #11: Ask to save modfications when opening a new file

This commit is contained in:
Michael Mandl 2018-09-30 11:38:30 +02:00
parent 0a71985876
commit 35e33581cc
2 changed files with 25 additions and 33 deletions

View file

@ -35,6 +35,8 @@ MainWindow::~MainWindow()
void MainWindow::newFile() void MainWindow::newFile()
{ {
closeFile();
m_dataModel = std::make_unique<DataModel>(this); m_dataModel = std::make_unique<DataModel>(this);
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);
@ -66,6 +68,8 @@ void MainWindow::openFile()
return; return;
} }
closeFile();
QFile loadFile(filename); QFile loadFile(filename);
if (!loadFile.open(QFile::ReadOnly)) if (!loadFile.open(QFile::ReadOnly))
{ {
@ -110,47 +114,32 @@ void MainWindow::saveFileAs()
saveFile(filename); saveFile(filename);
} }
void MainWindow::closeFile()
{
if (m_saveOnClose == true)
{
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
msgBox.setDefaultButton(QMessageBox::Save);
if (msgBox.exec() == QMessageBox::Save)
{
saveFile();
}
}
}
void MainWindow::dataModelChanged() void MainWindow::dataModelChanged()
{ {
qDebug() << "data model changed";
m_saveOnClose = true; m_saveOnClose = true;
setWindowModified(true); setWindowModified(true);
} }
void MainWindow::closeEvent(QCloseEvent *event) void MainWindow::closeEvent(QCloseEvent *event)
{ {
if (m_saveOnClose == false) closeFile();
{
event->accept();
return;
}
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Save:
saveFile();
if (m_saveOnClose == true)
{
event->ignore();
break;
}
case QMessageBox::Discard:
event->accept();
break;
case QMessageBox::Cancel:
default:
event->ignore();
break;
}
} }
void MainWindow::saveFile(const QString &filename) void MainWindow::saveFile(const QString &filename)
@ -165,6 +154,8 @@ void MainWindow::saveFile(const QString &filename)
saveFile.write(saveDoc.toJson()); saveFile.write(saveDoc.toJson());
saveFile.close(); saveFile.close();
qDebug() << "Wrote" << filename;
setWindowTitle(filename + "[*]"); setWindowTitle(filename + "[*]");
setWindowModified(false); setWindowModified(false);
m_filename = filename; m_filename = filename;

View file

@ -30,6 +30,7 @@ public slots:
void openFile(); void openFile();
void saveFile(); void saveFile();
void saveFileAs(); void saveFileAs();
void closeFile();
void dataModelChanged(); void dataModelChanged();
protected: protected: