Avoid recursion in apply on update default values

This commit is contained in:
Matthias Kuhn 2017-09-29 16:39:47 +02:00
parent cf77ffe85b
commit 2fbb6f09cc
No known key found for this signature in database
GPG Key ID: A0E766808764D73F
4 changed files with 22 additions and 18 deletions

View File

@ -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

View File

@ -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:

View File

@ -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 );
}
}
}
@ -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;
@ -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;

View File

@ -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