Skip to content

Commit

Permalink
Change canCommitChanges to allowCommit flag
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Sep 26, 2018
1 parent d2bb3d6 commit cda4950
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 6 deletions.
45 changes: 45 additions & 0 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -2236,6 +2236,44 @@ Test if an edit command is active
%Docstring
Configuration and logic to apply automatically on any edit happening on this layer.

.. versionadded:: 3.4
%End

bool allowCommit()() const;
%Docstring
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 :py:func:`commitChanges`, this flag is checked just after the

.. seealso:: :py:func:`beforeCommitChanges`

.. versionadded:: 3.4
%End

void setAllowCommit()( bool allowCommit() );
%Docstring
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 :py:func:`commitChanges`, this flag is checked just after the

.. seealso:: :py:func:`beforeCommitChanges`

.. versionadded:: 3.4
%End

Expand Down Expand Up @@ -2332,6 +2370,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
22 changes: 16 additions & 6 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2924,15 +2924,11 @@ bool QgsVectorLayer::commitChanges()
return false;
}

bool canCommit = true;

emit canCommitChanges( &canCommit );
emit beforeCommitChanges();

if ( !canCommit )
if ( !mAllowCommit )
return false;

emit beforeCommitChanges();

bool success = mEditBuffer->commitChanges( mCommitErrors );

if ( success )
Expand Down Expand Up @@ -4853,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
45 changes: 45 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -2010,6 +2010,42 @@ 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.
*
* \since QGIS 3.4
*/
bool allowCommit()() 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.
*
* \since QGIS 3.4
*/
void setAllowCommit()( bool allowCommit() );

public slots:

/**
Expand Down Expand Up @@ -2102,6 +2138,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 @@ -2512,6 +2555,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 cda4950

Please sign in to comment.