From dca664270f3c9c4072aabb3b2f16727bd5670dea Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Fri, 20 Jun 2014 13:03:23 +0200 Subject: [PATCH] MainWindow: Make it possible to cancel closing of database file When closing the database, either by using the menu item or by closing the window, the user is asked whether the changes he made shall be saved or not. Add a third button to this message box which makes it possible to cancel the action. --- src/MainWindow.cpp | 22 ++++++++++++++-------- src/sqlitedb.cpp | 25 ++++++++++++++++++------- src/sqlitedb.h | 2 +- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index c73ce8da..b0a503ce 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -373,7 +373,9 @@ void MainWindow::resetBrowser() void MainWindow::fileClose() { - db.close(); + if(!db.close()) + return; + setWindowTitle(QApplication::applicationName()); resetBrowser(); populateStructure(); @@ -401,13 +403,17 @@ void MainWindow::fileClose() void MainWindow::closeEvent( QCloseEvent* event ) { - db.close(); - PreferencesDialog::setSettingsValue("MainWindow", "geometry", saveGeometry()); - PreferencesDialog::setSettingsValue("MainWindow", "windowState", saveState()); - PreferencesDialog::setSettingsValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText()); - PreferencesDialog::setSettingsValue("PlotDock", "splitterSize", ui->splitterForPlot->saveState()); - clearCompleterModelsFields(); - QMainWindow::closeEvent(event); + if(db.close()) + { + PreferencesDialog::setSettingsValue("MainWindow", "geometry", saveGeometry()); + PreferencesDialog::setSettingsValue("MainWindow", "windowState", saveState()); + PreferencesDialog::setSettingsValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText()); + PreferencesDialog::setSettingsValue("PlotDock", "splitterSize", ui->splitterForPlot->saveState()); + clearCompleterModelsFields(); + QMainWindow::closeEvent(event); + } else { + event->ignore(); + } } void MainWindow::addRecord() diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index f0cb1cb2..88bc385b 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -191,16 +191,24 @@ bool DBBrowserDB::create ( const QString & db) } -void DBBrowserDB::close (){ - if (_db) +bool DBBrowserDB::close() +{ + if(_db) { if (getDirty()) { - if (QMessageBox::question( 0, - QApplication::applicationName(), - QObject::tr("Do you want to save the changes " - "made to the database file %1?").arg(curDBFilename), - QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) + QMessageBox::StandardButton reply = QMessageBox::question(0, + QApplication::applicationName(), + QObject::tr("Do you want to save the changes " + "made to the database file %1?").arg(curDBFilename), + QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); + + // If the user clicked the cancel button stop here and return false + if(reply == QMessageBox::Cancel) + return false; + + // If he didn't it was either yes or no + if(reply == QMessageBox::Yes) saveAll(); else revertAll(); //not really necessary, I think... but will not hurt. @@ -211,6 +219,9 @@ void DBBrowserDB::close (){ objMap.clear(); savepointList.clear(); if(mainWindow) mainWindow->dbState(getDirty()); + + // Return true to tell the calling function that the closing wasn't cancelled by the user + return true; } bool DBBrowserDB::dump(const QString& filename) diff --git a/src/sqlitedb.h b/src/sqlitedb.h index ea76c660..27cf92ba 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -54,7 +54,7 @@ public: ~DBBrowserDB (){} bool open ( const QString & db); bool create ( const QString & db); - void close (); + bool close(); bool setRestorePoint(const QString& pointname = "RESTOREPOINT"); bool save (const QString& pointname = "RESTOREPOINT"); bool revert (const QString& pointname = "RESTOREPOINT");