Skip to content

Commit

Permalink
Merge pull request #30185 from m-kuhn/update_on_split
Browse files Browse the repository at this point in the history
Update default values with "apply on update" on split
  • Loading branch information
m-kuhn committed Jun 13, 2019
2 parents 1c5ebe8 + 492ce0c commit 586d216
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/core/qgsvectorlayereditutils.cpp
Expand Up @@ -352,9 +352,10 @@ QgsGeometry::OperationResult QgsVectorLayerEditUtils::splitFeatures( const QVect
mLayer->changeGeometry( feat.id(), featureGeom );

//insert new features
for ( int i = 0; i < newGeometries.size(); ++i )
QgsAttributeMap attributeMap = feat.attributes().toMap();
for ( const QgsGeometry &geom : qgis::as_const( newGeometries ) )
{
QgsFeature f = QgsVectorLayerUtils::createFeature( mLayer, newGeometries.at( i ), feat.attributes().toMap() );
QgsFeature f = QgsVectorLayerUtils::createFeature( mLayer, geom, attributeMap );
mLayer->addFeature( f );
}

Expand Down
6 changes: 4 additions & 2 deletions src/core/qgsvectorlayerutils.cpp
Expand Up @@ -507,9 +507,11 @@ QgsFeatureList QgsVectorLayerUtils::createFeatures( const QgsVectorLayer *layer,

// 2. client side default expression
// note - deliberately not using else if!
QgsDefaultValue defaultValueDefinition = layer->defaultValueDefinition( idx );
if ( ( v.isNull() || ( hasUniqueConstraint
&& uniqueValueCaches[ idx ].contains( v ) ) )
&& layer->defaultValueDefinition( idx ).isValid() )
&& uniqueValueCaches[ idx ].contains( v ) )
|| defaultValueDefinition.applyOnUpdate() )
&& defaultValueDefinition.isValid() )
{
// client side default expression set - takes precedence over all. Why? Well, this is the only default
// which QGIS users have control over, so we assume that they're deliberately overriding any
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsvectorlayerutils.py
Expand Up @@ -263,6 +263,10 @@ def testCreateFeature(self):
# we do not expect the default value expression to take precedence over the attribute map
f = QgsVectorLayerUtils.createFeature(layer, attributes={0: 'a', 2: 6.0})
self.assertEqual(f.attributes(), ['a', NULL, 6.0])
# default value takes precedence if it's apply on update
layer.setDefaultValueDefinition(2, QgsDefaultValue('3*4', True))
f = QgsVectorLayerUtils.createFeature(layer, attributes={0: 'a', 2: 6.0})
self.assertEqual(f.attributes(), ['a', NULL, 12.0])
# layer with default value expression based on geometry
layer.setDefaultValueDefinition(2, QgsDefaultValue('3*$x'))
f = QgsVectorLayerUtils.createFeature(layer, g)
Expand Down

0 comments on commit 586d216

Please sign in to comment.