#1401 Ask user to save modified project before project close

This commit is contained in:
Magne Sjaastad 2017-04-20 08:07:47 +02:00
parent 82b6e4b8f2
commit d7d80bc53e
8 changed files with 99 additions and 14 deletions

View File

@ -98,10 +98,12 @@
#include "cafPdmFieldCvfColor.h"
#include "cafPdmFieldCvfMat4d.h"
#include "cafPdmSettings.h"
#include "cafPdmUiModelChangeDetector.h"
#include "cafPdmUiTreeView.h"
#include "cafProgressInfo.h"
#include "cafUiProcess.h"
#include "cafUtils.h"
#include "cvfProgramOptions.h"
#include "cvfqtUtils.h"
@ -716,6 +718,55 @@ bool RiaApplication::saveProjectPromptForFileName()
return bSaveOk;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaApplication::hasValidProjectFileExtension(const QString& fileName)
{
if (fileName.contains(".rsp", Qt::CaseInsensitive) || fileName.contains(".rip", Qt::CaseInsensitive))
{
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaApplication::askUserToSaveModifiedProject()
{
if (caf::PdmUiModelChangeDetector::instance()->isModelChanged())
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Question);
QString questionText;
questionText = QString("The current project is modified.\n\nDo you want to save the changes?");
msgBox.setText(questionText);
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
int ret = msgBox.exec();
if (ret == QMessageBox::Cancel)
{
return false;
}
else if (ret == QMessageBox::Yes)
{
if (!saveProject())
{
return false;
}
}
else
{
caf::PdmUiModelChangeDetector::instance()->reset();
}
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
@ -736,10 +787,11 @@ bool RiaApplication::saveProjectAs(const QString& fileName)
m_recentFileActionProvider->addFileName(fileName);
caf::PdmUiModelChangeDetector::instance()->reset();
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -2022,7 +2074,7 @@ bool RiaApplication::openFile(const QString& fileName)
bool loadingSucceded = false;
if (fileName.contains(".rsp", Qt::CaseInsensitive) || fileName.contains(".rip", Qt::CaseInsensitive))
if (RiaApplication::hasValidProjectFileExtension(fileName))
{
loadingSucceded = loadProject(fileName);
}
@ -2057,6 +2109,11 @@ bool RiaApplication::openFile(const QString& fileName)
}
}
if (loadingSucceded && !RiaApplication::hasValidProjectFileExtension(fileName))
{
caf::PdmUiModelChangeDetector::instance()->setModelChanged();
}
return loadingSucceded;
}

View File

@ -126,7 +126,11 @@ public:
bool saveProject();
bool saveProjectAs(const QString& fileName);
bool saveProjectPromptForFileName();
static bool hasValidProjectFileExtension(const QString& fileName);
bool askUserToSaveModifiedProject();
void closeProject();
void addWellPathsToModel(QList<QString> wellPathFilePaths);
void addWellLogsToModel(const QList<QString>& wellLogFilePaths);

View File

@ -39,6 +39,8 @@ void RicCloseProjectFeature::onActionTriggered(bool isChecked)
{
RiaApplication* app = RiaApplication::instance();
if (!app->askUserToSaveModifiedProject()) return;
app->closeProject();
}

View File

@ -41,6 +41,9 @@ bool RicOpenLastUsedFileFeature::isCommandEnabled()
void RicOpenLastUsedFileFeature::onActionTriggered(bool isChecked)
{
RiaApplication* app = RiaApplication::instance();
if (!app->askUserToSaveModifiedProject()) return;
QString fileName = app->preferences()->lastUsedProjectFileName;
if (app->loadProject(fileName))

View File

@ -42,6 +42,9 @@ bool RicOpenProjectFeature::isCommandEnabled()
void RicOpenProjectFeature::onActionTriggered(bool isChecked)
{
RiaApplication* app = RiaApplication::instance();
if (!app->askUserToSaveModifiedProject()) return;
QString defaultDir = app->lastUsedDialogDirectory("BINARY_GRID");
QString fileName = QFileDialog::getOpenFileName(NULL, "Open ResInsight Project", defaultDir, "ResInsight project (*.rsp *.rip);;All files(*.*)");

View File

@ -137,11 +137,18 @@ void RiuMainPlotWindow::cleanupGuiBeforeProjectClose()
//--------------------------------------------------------------------------------------------------
void RiuMainPlotWindow::closeEvent(QCloseEvent* event)
{
RiaApplication* app = RiaApplication::instance();
if (!app->askUserToSaveModifiedProject())
{
event->ignore();
return;
}
saveWinGeoAndDockToolBarLayout();
if (!RiaApplication::instance()->tryCloseMainWindow()) return;
if (!app->tryCloseMainWindow()) return;
RiaApplication::instance()->closeProject();
app->closeProject();
}
//--------------------------------------------------------------------------------------------------

View File

@ -197,15 +197,18 @@ void RiuMainWindow::cleanupGuiBeforeProjectClose()
//--------------------------------------------------------------------------------------------------
void RiuMainWindow::closeEvent(QCloseEvent* event)
{
saveWinGeoAndDockToolBarLayout();
RiaApplication* app = RiaApplication::instance();
if (!app->askUserToSaveModifiedProject())
{
event->ignore();
return;
}
saveWinGeoAndDockToolBarLayout();
if (!app->tryClosePlotWindow()) return;
RiaApplication::instance()->closeProject();
event->accept();
app->closeProject();
}
//--------------------------------------------------------------------------------------------------

View File

@ -122,18 +122,24 @@ void RiuRecentFileActionProvider::slotOpenRecentFile()
QAction* action = qobject_cast<QAction *>(sender());
if (action)
{
QString filename = action->data().toString();
bool loadingSucceded = RiaApplication::instance()->openFile(filename);
QString fileName = action->data().toString();
RiaApplication* app = RiaApplication::instance();
if (RiaApplication::hasValidProjectFileExtension(fileName))
{
if (!app->askUserToSaveModifiedProject()) return;
}
bool loadingSucceded = RiaApplication::instance()->openFile(fileName);
if (loadingSucceded)
{
addFileName(filename);
addFileName(fileName);
}
else
{
QMessageBox::warning(NULL, "File open", "Failed to import file located at\n" + filename);
QMessageBox::warning(NULL, "File open", "Failed to import file located at\n" + fileName);
removeFileName(filename);
removeFileName(fileName);
}
}
}