Skip to content
Permalink
Browse files

Also warn for unsaved changes when opening a model through the model …

…designer
  • Loading branch information
nyalldawson committed Mar 9, 2020
1 parent e9c594d commit 4dd598fbb58af1ac8292083e5170f3d9cfc9a6db
@@ -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

};
@@ -38,8 +38,6 @@
QgsApplication,
QgsProcessing,
QgsProject,
QgsMessageLog,
QgsProcessingModelAlgorithm,
QgsProcessingModelParameter,
QgsSettings
)
@@ -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],
@@ -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 )
@@ -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 );
@@ -522,7 +527,7 @@ void QgsModelDesignerDialog::updateWindowTitle()

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

void QgsModelDesignerDialog::fillInputsTree()
@@ -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();

0 comments on commit 4dd598f

Please sign in to comment.
You can’t perform that action at this time.