Skip to content

Commit

Permalink
Merge pull request #8034 from m-kuhn/canCommitChanges
Browse files Browse the repository at this point in the history
Add QgsVectorLayer::allowCommit property
  • Loading branch information
m-kuhn authored Oct 5, 2018
2 parents e86693a + 3522518 commit e25d634
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,8 @@ Configuration and logic to apply automatically on any edit happening on this lay
.. versionadded:: 3.4
%End



public slots:

void select( QgsFeatureId featureId );
Expand Down Expand Up @@ -2332,6 +2334,13 @@ This signal is emitted when selection was changed
void layerModified();
%Docstring
This signal is emitted when modifications has been done on layer
%End

void allowCommitChanged();
%Docstring
Emitted whenever the allowCommitChanged() property of this layer changes.

.. versionadded:: 3.4
%End

void beforeModifiedCheck() const;
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2926,6 +2926,9 @@ bool QgsVectorLayer::commitChanges()

emit beforeCommitChanges();

if ( !mAllowCommit )
return false;

bool success = mEditBuffer->commitChanges( mCommitErrors );

if ( success )
Expand Down Expand Up @@ -4846,6 +4849,20 @@ QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties
return labeling;
}

bool QgsVectorLayer::allowCommit() const
{
return mAllowCommit;
}

void QgsVectorLayer::setAllowCommit( bool allowCommit )
{
if ( mAllowCommit == allowCommit )
return;

mAllowCommit = allowCommit;
emit allowCommitChanged();
}

QgsGeometryOptions *QgsVectorLayer::geometryOptions() const
{
return mGeometryOptions.get();
Expand Down
49 changes: 49 additions & 0 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,46 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
QgsGeometryOptions *geometryOptions() const;

/**
* Controls, if the layer is allowed to commit changes. If this is set to false
* it will not be possible to commit changes on this layer. This can be used to
* define checks on a layer that need to be pass before the layer can be saved.
* If you use this API, make sure that:
*
* - the user is visibly informed that his changes were not saved and what he needs
* to do in order to be able to save the changes.
*
* - to set the property back to true, once the user has fixed his data.
*
* When calling \see commitChanges(), this flag is checked just after the
* \see beforeCommitChanges() signal is emitted, so it's possible to adjust it from there.
*
* \note Not available in Python bindings
*
* \since QGIS 3.4
*/
bool allowCommit() const SIP_SKIP;

/**
* Controls, if the layer is allowed to commit changes. If this is set to false
* it will not be possible to commit changes on this layer. This can be used to
* define checks on a layer that need to be pass before the layer can be saved.
* If you use this API, make sure that:
*
* - the user is visibly informed that his changes were not saved and what he needs
* to do in order to be able to save the changes.
*
* - to set the property back to true, once the user has fixed his data.
*
* When calling \see commitChanges(), this flag is checked just after the
* \see beforeCommitChanges() signal is emitted, so it's possible to adjust it from there.
*
* \note Not available in Python bindings
*
* \since QGIS 3.4
*/
void setAllowCommit( bool allowCommit ) SIP_SKIP;

public slots:

/**
Expand Down Expand Up @@ -2102,6 +2142,13 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! This signal is emitted when modifications has been done on layer
void layerModified();

/**
* Emitted whenever the allowCommitChanged() property of this layer changes.
*
* \since QGIS 3.4
*/
void allowCommitChanged();

//! Is emitted, when layer is checked for modifications. Use for last-minute additions
void beforeModifiedCheck() const;

Expand Down Expand Up @@ -2502,6 +2549,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

std::unique_ptr<QgsGeometryOptions> mGeometryOptions;

bool mAllowCommit = true;

friend class QgsVectorLayerFeatureSource;
};

Expand Down

0 comments on commit e25d634

Please sign in to comment.