From a66bdb1bf5b8f69e4262f1d331ff211b8228caa1 Mon Sep 17 00:00:00 2001 From: Michael Keller Date: Thu, 16 May 2024 14:39:43 +1200 Subject: [PATCH] Planner: Improve Exit Warning. Improve the warning shown to the user when closing the application wile in the planner. We now allow the user to directly discard the planned dive, save it into the dive log, or cancel the operation altogether. If they save into the dive log, or if they modified the dive log before starting the planner, a second warning about the unsaved dive log changes will be shown. Signed-off-by: Michael Keller --- desktop-widgets/mainwindow.cpp | 37 +++++++++++++++++++++++++--------- desktop-widgets/mainwindow.h | 1 + 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 57dd6615e..6f0c96d86 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -609,7 +609,7 @@ void MainWindow::on_actionPreferences_triggered() void MainWindow::on_actionQuit_triggered() { - if (!okToClose(tr("Please save or cancel the current dive edit before quiting the application."))) + if (!okToClose(tr("Please save or cancel the current dive edit before quitting the application."))) return; writeSettings(); @@ -986,14 +986,10 @@ QString MainWindow::filter_import_dive_sites() return f; } -bool MainWindow::askSaveChanges() +int MainWindow::saveChangesConfirmationBox(QString message) { QMessageBox response(this); - QString message = !existing_filename.empty() ? - tr("Do you want to save the changes that you made in the file %1?").arg(displayedFilename(existing_filename)) : - tr("Do you want to save the changes that you made in the data file?"); - response.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); response.setDefaultButton(QMessageBox::Save); response.setText(message); @@ -1001,8 +997,17 @@ bool MainWindow::askSaveChanges() response.setInformativeText(tr("Changes will be lost if you don't save them.")); response.setIcon(QMessageBox::Warning); response.setWindowModality(Qt::WindowModal); - int ret = response.exec(); + return response.exec(); +} + +bool MainWindow::askSaveChanges() +{ + QString message = !existing_filename.empty() ? + tr("Do you want to save the changes that you made in the file %1?").arg(displayedFilename(existing_filename)) : + tr("Do you want to save the changes that you made in the data file?"); + + int ret = saveChangesConfirmationBox(message); switch (ret) { case QMessageBox::Save: file_save(); @@ -1057,9 +1062,21 @@ void MainWindow::writeSettings() void MainWindow::closeEvent(QCloseEvent *event) { if (inPlanner()) { - on_actionQuit_triggered(); - event->ignore(); - return; + int ret = saveChangesConfirmationBox("Do you want to save the changes that you made in the planner into your dive log?"); + switch (ret) { + case QMessageBox::Save: + DivePlannerPointsModel::instance()->savePlan(); + + break; + case QMessageBox::Cancel: + event->ignore(); + + return; + case QMessageBox::Discard: + DivePlannerPointsModel::instance()->cancelPlan(); + + break; + } } if (!Command::isClean() && (askSaveChanges() == false)) { diff --git a/desktop-widgets/mainwindow.h b/desktop-widgets/mainwindow.h index d156fbfd9..12587347b 100644 --- a/desktop-widgets/mainwindow.h +++ b/desktop-widgets/mainwindow.h @@ -178,6 +178,7 @@ private: QString filter_import_dive_sites(); static MainWindow *m_Instance; QString displayedFilename(const std::string &fullFilename); + int saveChangesConfirmationBox(QString message); bool askSaveChanges(); bool okToClose(QString message); void closeCurrentFile();