Skip to content

Commit

Permalink
Add QgsVectorLayer::canCommitChanges
Browse files Browse the repository at this point in the history
to control if changes can be saved or not. This signal is emitted before a layer is being saved and if a connected slot marks the canCommit variable as False, the layer will not be saved
  • Loading branch information
m-kuhn committed Sep 26, 2018
1 parent 5157406 commit a56deac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -2352,6 +2352,16 @@ Is emitted, when editing on this layer has started
void editingStopped();
%Docstring
Is emitted, when edited changes successfully have been written to the data provider
%End

void canCommitChanges( bool &canCommit );
%Docstring
Emitted when a layer wants to commit changes to the data provider.
Can be used to prevent the layer from being saved by setting ``canCommit`` to false.

It is the developers responsibility to inform the user about this in a suitable manner.

.. versionadded:: 3.4
%End

void beforeCommitChanges();
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2924,6 +2924,13 @@ bool QgsVectorLayer::commitChanges()
return false;
}

bool canCommit = true;

emit canCommitChanges( canCommit );

if ( !canCommit )
return false;

emit beforeCommitChanges();

bool success = mEditBuffer->commitChanges( mCommitErrors );
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -2114,6 +2114,16 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Is emitted, when edited changes successfully have been written to the data provider
void editingStopped();

/**
* Emitted when a layer wants to commit changes to the data provider.
* Can be used to prevent the layer from being saved by setting \a canCommit to false.
*
* It is the developers responsibility to inform the user about this in a suitable manner.
*
* \since QGIS 3.4
*/
void canCommitChanges( bool &canCommit );

//! Is emitted, before changes are committed to the data provider
void beforeCommitChanges();

Expand Down

0 comments on commit a56deac

Please sign in to comment.