Skip to content

Commit dca6642

Browse files
committed
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.
1 parent 3da0d36 commit dca6642

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/MainWindow.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ void MainWindow::resetBrowser()
373373

374374
void MainWindow::fileClose()
375375
{
376-
db.close();
376+
if(!db.close())
377+
return;
378+
377379
setWindowTitle(QApplication::applicationName());
378380
resetBrowser();
379381
populateStructure();
@@ -401,13 +403,17 @@ void MainWindow::fileClose()
401403

402404
void MainWindow::closeEvent( QCloseEvent* event )
403405
{
404-
db.close();
405-
PreferencesDialog::setSettingsValue("MainWindow", "geometry", saveGeometry());
406-
PreferencesDialog::setSettingsValue("MainWindow", "windowState", saveState());
407-
PreferencesDialog::setSettingsValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText());
408-
PreferencesDialog::setSettingsValue("PlotDock", "splitterSize", ui->splitterForPlot->saveState());
409-
clearCompleterModelsFields();
410-
QMainWindow::closeEvent(event);
406+
if(db.close())
407+
{
408+
PreferencesDialog::setSettingsValue("MainWindow", "geometry", saveGeometry());
409+
PreferencesDialog::setSettingsValue("MainWindow", "windowState", saveState());
410+
PreferencesDialog::setSettingsValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText());
411+
PreferencesDialog::setSettingsValue("PlotDock", "splitterSize", ui->splitterForPlot->saveState());
412+
clearCompleterModelsFields();
413+
QMainWindow::closeEvent(event);
414+
} else {
415+
event->ignore();
416+
}
411417
}
412418

413419
void MainWindow::addRecord()

src/sqlitedb.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,24 @@ bool DBBrowserDB::create ( const QString & db)
191191
}
192192

193193

194-
void DBBrowserDB::close (){
195-
if (_db)
194+
bool DBBrowserDB::close()
195+
{
196+
if(_db)
196197
{
197198
if (getDirty())
198199
{
199-
if (QMessageBox::question( 0,
200-
QApplication::applicationName(),
201-
QObject::tr("Do you want to save the changes "
202-
"made to the database file %1?").arg(curDBFilename),
203-
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
200+
QMessageBox::StandardButton reply = QMessageBox::question(0,
201+
QApplication::applicationName(),
202+
QObject::tr("Do you want to save the changes "
203+
"made to the database file %1?").arg(curDBFilename),
204+
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
205+
206+
// If the user clicked the cancel button stop here and return false
207+
if(reply == QMessageBox::Cancel)
208+
return false;
209+
210+
// If he didn't it was either yes or no
211+
if(reply == QMessageBox::Yes)
204212
saveAll();
205213
else
206214
revertAll(); //not really necessary, I think... but will not hurt.
@@ -211,6 +219,9 @@ void DBBrowserDB::close (){
211219
objMap.clear();
212220
savepointList.clear();
213221
if(mainWindow) mainWindow->dbState(getDirty());
222+
223+
// Return true to tell the calling function that the closing wasn't cancelled by the user
224+
return true;
214225
}
215226

216227
bool DBBrowserDB::dump(const QString& filename)

src/sqlitedb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DBBrowserDB
5454
~DBBrowserDB (){}
5555
bool open ( const QString & db);
5656
bool create ( const QString & db);
57-
void close ();
57+
bool close();
5858
bool setRestorePoint(const QString& pointname = "RESTOREPOINT");
5959
bool save (const QString& pointname = "RESTOREPOINT");
6060
bool revert (const QString& pointname = "RESTOREPOINT");

0 commit comments

Comments
 (0)