Skip to content

Commit

Permalink
Avoid recursion in apply on update default values
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Sep 29, 2017
1 parent cf77ffe commit 2fbb6f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
13 changes: 8 additions & 5 deletions python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -925,13 +925,16 @@ Return the provider type for this layer
virtual bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = 0 );


bool updateFeature( const QgsFeature &f );
bool updateFeature( const QgsFeature &feature, bool skipDefaultValues = false );
%Docstring
Updates an existing feature. This method needs to query the datasource
on every call. Consider using changeAttributeValue() or
changeGeometry() instead.
\param f Feature to update
:return: True in case of success and False in case of error
on every call. Consider using changeAttributeValue() or
changeGeometry() instead. The id of the feature will be used to match
an existing feature.

\param feature Feature with changed geometry or attributes.
\param skipDefaultValues Default values will not be updated if this is true. False by default.
:return: True in case of success and False in case of error
:rtype: bool
%End

Expand Down
4 changes: 1 addition & 3 deletions src/app/qgsattributetypedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,10 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
*/
void setDefaultValueExpression( const QString &expression );



QgsExpressionContext createExpressionContext() const override;

bool applyDefaultValueOnUpdate() const;
void setApplyDefaultValueOnUpdate(bool applyDefaultValueOnUpdate);
void setApplyDefaultValueOnUpdate( bool applyDefaultValueOnUpdate );

private slots:

Expand Down
7 changes: 3 additions & 4 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,7 @@ void QgsVectorLayer::updateDefaultValues( QgsFeatureId fid, QgsFeature feature )
continue;

feature.setAttribute( idx, defaultValue( idx, feature ) );
// TODO catch recursion!!
updateFeature( feature );
updateFeature( feature, true );
}
}
}
Expand Down Expand Up @@ -961,7 +960,7 @@ bool QgsVectorLayer::addFeature( QgsFeature &feature, Flags )
return success;
}

bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature )
bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipDefaultValues )
{
bool hasChanged = false;
bool hasError = false;
Expand Down Expand Up @@ -1003,7 +1002,7 @@ bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature )
}
}

if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() )
if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )
updateDefaultValues( updatedFeature.id(), updatedFeature );

return !hasError;
Expand Down
16 changes: 10 additions & 6 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,17 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = 0 ) override;

/** Updates an existing feature. This method needs to query the datasource
on every call. Consider using changeAttributeValue() or
changeGeometry() instead.
\param f Feature to update
\returns True in case of success and False in case of error
/**
* Updates an existing feature. This method needs to query the datasource
* on every call. Consider using changeAttributeValue() or
* changeGeometry() instead. The id of the feature will be used to match
* an existing feature.
*
* \param feature Feature with changed geometry or attributes.
* \param skipDefaultValues Default values will not be updated if this is true. False by default.
* \returns True in case of success and False in case of error
*/
bool updateFeature( const QgsFeature &f );
bool updateFeature( const QgsFeature &feature, bool skipDefaultValues = false );

/** Insert a new vertex before the given vertex number,
* in the given ring, item (first number is index 0), and feature
Expand Down

0 comments on commit 2fbb6f0

Please sign in to comment.