From 90d54a3aa48f8b1c050754f866ef0cf013f4f06b Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 30 Sep 2018 18:19:08 +0200 Subject: Ask user to save on new and quit if project is dirty. --- src/mainwindow.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/mainwindow.h | 7 ++++++ src/project.cc | 5 +++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/mainwindow.cc b/src/mainwindow.cc index ad13975..0120eab 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -348,12 +348,64 @@ void MainWindow::updateWindowTitle() setWindowTitle("DGEdit - " + project_string); } -void MainWindow::closeEvent(QCloseEvent*) +void MainWindow::closeEvent(QCloseEvent* event) { + if(!checkDirty()) + { + event->ignore(); + return; + } + saveSettings(); QApplication::quit(); } +bool MainWindow::checkDirty() +{ + if(project_dirty) + { + int ret = + QMessageBox::question(this, tr("Save before closing project?"), + tr("The project has changed. Do you want to save " + "before closing?"), + QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); + switch(ret) + { + case QMessageBox::Yes: + saveProject(); + if(project_dirty) + { + // Project still dirty - it was not saved (user pressed cancel?) + return false; + } + break; + case QMessageBox::No: + // Proceed to quit + break; + case QMessageBox::Cancel: + return false; + default: + break; + } + } + + return true; // not dirty or user chose not to save +} + +void MainWindow::reset() +{ + // Close all tabs + while(tab_widget->count()) + { + tab_widget->removeTab(0); + } + + instrument_list->clear(); + channel_list->clear(); + + project.reset(); +} + void MainWindow::loadSettings() { QByteArray state; @@ -372,6 +424,13 @@ void MainWindow::saveSettings() void MainWindow::newProject() { + if(!checkDirty()) + { + return; + } + + reset(); + ProjectDialog dlg(this, project); dlg.show(); dlg.exec(); @@ -379,6 +438,11 @@ void MainWindow::newProject() void MainWindow::loadProject() { + if(!checkDirty()) + { + return; + } + QString filename = QFileDialog::getOpenFileName(this, tr("Load DGEdit Project"), @@ -401,6 +465,8 @@ void MainWindow::loadProject(QString filename) return; } + reset(); + QString xml(file.readAll()); file.close(); diff --git a/src/mainwindow.h b/src/mainwindow.h index e4c5863..2c12950 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -73,6 +73,13 @@ protected: void closeEvent(QCloseEvent*); private: + //! Check dirty and prompt user to save + //! \returns true to continue closing the project, false to bail out. + bool checkDirty(); + + //! Reset project and reflect in mainwindow (close tabs and lists) + void reset(); + void loadSettings(); void saveSettings(); diff --git a/src/project.cc b/src/project.cc index 04919db..318898b 100644 --- a/src/project.cc +++ b/src/project.cc @@ -584,8 +584,11 @@ QList Project::getChannelList() const void Project::reset() { RAIIBulkUpdate bulkUpdate(*this); - setRawFileRoot(""); setProjectName(""); + setProjectFile(""); + setRawFileRoot(""); + setExportPath(""); + instruments.clear(); channels.clear(); next_id = 0; -- cgit v1.2.3