Simplify the code that checks if it's OK to close the current file

And make sure it gets called whenever it needs to get called - it was
missing from the openRecentFile case.

Fixes #530

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-06-08 19:46:43 -07:00
parent a70a0662f5
commit 6f568fbecd
2 changed files with 25 additions and 11 deletions

View file

@ -175,16 +175,14 @@ void MainWindow::on_actionNew_triggered()
void MainWindow::on_actionOpen_triggered() void MainWindow::on_actionOpen_triggered()
{ {
if (DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING || if (!okToClose(tr("Please save or cancel the current dive edit before opening a new file.")))
ui.InfoWidget->isEditing()) {
QMessageBox::warning(this, tr("Warning"), tr("Please save or cancel the current dive edit before opening a new file."));
return; return;
}
QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), lastUsedDir(), filter()); QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), lastUsedDir(), filter());
if (filename.isEmpty()) if (filename.isEmpty())
return; return;
updateLastUsedDir(QFileInfo(filename).dir().path()); updateLastUsedDir(QFileInfo(filename).dir().path());
on_actionClose_triggered(); closeCurrentFile();
loadFiles(QStringList() << filename); loadFiles(QStringList() << filename);
} }
@ -234,16 +232,21 @@ void MainWindow::setToolButtonsEnabled(bool enabled)
ui.profHR->setEnabled(enabled); ui.profHR->setEnabled(enabled);
} }
void MainWindow::on_actionClose_triggered() bool MainWindow::okToClose(QString message)
{ {
if (DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING || if (DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING ||
ui.InfoWidget->isEditing()) { ui.InfoWidget->isEditing()) {
QMessageBox::warning(this, tr("Warning"), tr("Please save or cancel the current dive edit before closing the file.")); QMessageBox::warning(this, tr("Warning"), message);
return; return false;
} }
if (unsaved_changes() && (askSaveChanges() == false)) if (unsaved_changes() && askSaveChanges() == false)
return; return false;
return true;
}
void MainWindow::closeCurrentFile()
{
ui.newProfile->setEmptyState(); ui.newProfile->setEmptyState();
/* free the dives and trips */ /* free the dives and trips */
clear_git_id(); clear_git_id();
@ -263,6 +266,12 @@ void MainWindow::on_actionClose_triggered()
clear_events(); clear_events();
} }
void MainWindow::on_actionClose_triggered()
{
if (okToClose(tr("Please save or cancel the current dive edit before closing the file.")))
closeCurrentFile();
}
QString MainWindow::lastUsedDir() QString MainWindow::lastUsedDir()
{ {
QSettings settings; QSettings settings;
@ -1001,12 +1010,15 @@ void MainWindow::recentFileTriggered(bool checked)
{ {
Q_UNUSED(checked); Q_UNUSED(checked);
if (!okToClose(tr("Please save or cancel the current dive edit before opening a new file.")))
return;
QAction *actionRecent = (QAction *)sender(); QAction *actionRecent = (QAction *)sender();
const QString &filename = actionRecent->toolTip(); const QString &filename = actionRecent->toolTip();
updateLastUsedDir(QFileInfo(filename).dir().path()); updateLastUsedDir(QFileInfo(filename).dir().path());
on_actionClose_triggered(); closeCurrentFile();
loadFiles(QStringList() << filename); loadFiles(QStringList() << filename);
} }

View file

@ -169,6 +169,8 @@ private:
QString filter(); QString filter();
static MainWindow *m_Instance; static MainWindow *m_Instance;
bool askSaveChanges(); bool askSaveChanges();
bool okToClose(QString message);
void closeCurrentFile();
void writeSettings(); void writeSettings();
int file_save(); int file_save();
int file_save_as(); int file_save_as();