Skip to content

Commit

Permalink
Also warn for unsaved changes when opening a model through the model …
Browse files Browse the repository at this point in the history
…designer
  • Loading branch information
nyalldawson committed Mar 10, 2020
1 parent e9c594d commit 4dd598f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
Expand Up @@ -83,6 +83,13 @@ Loads a model into the designer from the specified file ``path``.
bool validateSave();
%Docstring
Checks if the model can current be saved, and returns ``True`` if it can.
%End

bool checkForUnsavedChanges();
%Docstring
Checks if there are unsaved changes in the model, and if so, prompts the user to save them.

Returns ``False`` if the cancel option was selected
%End

};
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -38,8 +38,6 @@
QgsApplication,
QgsProcessing,
QgsProject,
QgsMessageLog,
QgsProcessingModelAlgorithm,
QgsProcessingModelParameter,
QgsSettings
)
Expand Down Expand Up @@ -185,6 +183,9 @@ def saveModel(self, saveAs):
self.setDirty(False)

def openModel(self):
if not self.checkForUnsavedChanges():
return

filename, selected_filter = QFileDialog.getOpenFileName(self,
self.tr('Open Model'),
ModelerUtils.modelsFolders()[0],
Expand Down
55 changes: 30 additions & 25 deletions src/gui/processing/models/qgsmodeldesignerdialog.cpp
Expand Up @@ -239,31 +239,10 @@ QgsModelDesignerDialog::~QgsModelDesignerDialog()

void QgsModelDesignerDialog::closeEvent( QCloseEvent *event )
{
if ( isDirty() )
{
QMessageBox::StandardButton ret = QMessageBox::question( this, tr( "Save Model?" ),
tr( "There are unsaved changes in this model. Do you want to keep those?" ),
QMessageBox::Save | QMessageBox::Cancel | QMessageBox::Discard, QMessageBox::Cancel );
switch ( ret )
{
case QMessageBox::Save:
saveModel( false );
event->accept();
break;

case QMessageBox::Discard:
event->accept();
break;

default:
event->ignore();
break;
}
}
else
{
if ( checkForUnsavedChanges() )
event->accept();
}
else
event->ignore();
}

void QgsModelDesignerDialog::beginUndoCommand( const QString &text, int id )
Expand Down Expand Up @@ -359,6 +338,32 @@ bool QgsModelDesignerDialog::validateSave()
return true;
}

bool QgsModelDesignerDialog::checkForUnsavedChanges()
{
if ( isDirty() )
{
QMessageBox::StandardButton ret = QMessageBox::question( this, tr( "Save Model?" ),
tr( "There are unsaved changes in this model. Do you want to keep those?" ),
QMessageBox::Save | QMessageBox::Cancel | QMessageBox::Discard, QMessageBox::Cancel );
switch ( ret )
{
case QMessageBox::Save:
saveModel( false );
return true;

case QMessageBox::Discard:
return true;

default:
return false;
}
}
else
{
return true;
}
}

void QgsModelDesignerDialog::zoomIn()
{
mView->setTransformationAnchor( QGraphicsView::NoAnchor );
Expand Down Expand Up @@ -522,7 +527,7 @@ void QgsModelDesignerDialog::updateWindowTitle()

bool QgsModelDesignerDialog::isDirty() const
{
return mHasChanged && mUndoStack->index() == -1;
return mHasChanged && mUndoStack->index() != -1;
}

void QgsModelDesignerDialog::fillInputsTree()
Expand Down
7 changes: 7 additions & 0 deletions src/gui/processing/models/qgsmodeldesignerdialog.h
Expand Up @@ -107,6 +107,13 @@ class GUI_EXPORT QgsModelDesignerDialog : public QMainWindow, public Ui::QgsMode
*/
bool validateSave();

/**
* Checks if there are unsaved changes in the model, and if so, prompts the user to save them.
*
* Returns FALSE if the cancel option was selected
*/
bool checkForUnsavedChanges();

private slots:
void zoomIn();
void zoomOut();
Expand Down

0 comments on commit 4dd598f

Please sign in to comment.