Skip to content

Commit 4b37c6e

Browse files
authoredMar 31, 2023
Merge pull request #52448 from qgis/backport-51836-to-release-3_28
[Backport release-3_28] Fix #51543 and make big forms more fluid by reducing calls to updateFieldDependencies
2 parents 045bf94 + 625a064 commit 4b37c6e

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed
 

‎src/gui/qgsattributeform.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,6 @@ QgsFeature QgsAttributeForm::getUpdatedFeature() const
522522

523523
void QgsAttributeForm::updateValuesDependencies( const int originIdx )
524524
{
525-
updateFieldDependencies();
526-
527525
updateValuesDependenciesDefaultValues( originIdx );
528526
updateValuesDependenciesVirtualFields( originIdx );
529527
}
@@ -547,7 +545,11 @@ void QgsAttributeForm::updateValuesDependenciesDefaultValues( const int originId
547545
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
548546
if ( eww )
549547
{
550-
//do not update when when mMode is not AddFeatureMode and it's not applyOnUpdate
548+
// Update only on form opening (except when applyOnUpdate is activated)
549+
if ( mValuesInitialized && !eww->field().defaultValueDefinition().applyOnUpdate() )
550+
continue;
551+
552+
// Update only when mMode is AddFeatureMode (except when applyOnUpdate is activated)
551553
if ( mMode != QgsAttributeEditorContext::AddFeatureMode && !eww->field().defaultValueDefinition().applyOnUpdate() )
552554
{
553555
continue;
@@ -906,6 +908,23 @@ void QgsAttributeForm::resetValues()
906908
{
907909
ww->setFeature( mFeature );
908910
}
911+
912+
// Prepare value dependencies
913+
updateFieldDependencies();
914+
915+
// Update dependent fields
916+
for ( QgsWidgetWrapper *ww : constMWidgets )
917+
{
918+
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
919+
if ( !eww )
920+
continue;
921+
922+
// Append field index here, so it's not updated recursively
923+
mAlreadyUpdatedFields.append( eww->fieldIdx() );
924+
updateValuesDependencies( eww->fieldIdx() );
925+
mAlreadyUpdatedFields.removeAll( eww->fieldIdx() );
926+
}
927+
909928
mValuesInitialized = true;
910929
mDirty = false;
911930
}

‎tests/src/gui/testqgsattributeform.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,9 @@ void TestQgsAttributeForm::testDefaultValueUpdate()
982982
//col3 - "col2"
983983

984984
// set constraints for each field
985-
layer->setDefaultValueDefinition( 1, QgsDefaultValue( QStringLiteral( "\"col0\"+1" ) ) );
986-
layer->setDefaultValueDefinition( 2, QgsDefaultValue( QStringLiteral( "\"col0\"+\"col1\"" ) ) );
987-
layer->setDefaultValueDefinition( 3, QgsDefaultValue( QStringLiteral( "\"col2\"" ) ) );
985+
layer->setDefaultValueDefinition( 1, QgsDefaultValue( QStringLiteral( "\"col0\"+1" ), true ) );
986+
layer->setDefaultValueDefinition( 2, QgsDefaultValue( QStringLiteral( "\"col0\"+\"col1\"" ), true ) );
987+
layer->setDefaultValueDefinition( 3, QgsDefaultValue( QStringLiteral( "\"col2\"" ), true ) );
988988

989989
layer->startEditing();
990990

@@ -1044,10 +1044,10 @@ void TestQgsAttributeForm::testDefaultValueUpdateRecursion()
10441044
//col3 - COALESCE( 0, "col2"+1)
10451045

10461046
// set constraints for each field
1047-
layer->setDefaultValueDefinition( 0, QgsDefaultValue( QStringLiteral( "\"col3\"+1" ) ) );
1048-
layer->setDefaultValueDefinition( 1, QgsDefaultValue( QStringLiteral( "\"col0\"+1" ) ) );
1049-
layer->setDefaultValueDefinition( 2, QgsDefaultValue( QStringLiteral( "\"col1\"+1" ) ) );
1050-
layer->setDefaultValueDefinition( 3, QgsDefaultValue( QStringLiteral( "\"col2\"+1" ) ) );
1047+
layer->setDefaultValueDefinition( 0, QgsDefaultValue( QStringLiteral( "\"col3\"+1" ), true ) );
1048+
layer->setDefaultValueDefinition( 1, QgsDefaultValue( QStringLiteral( "\"col0\"+1" ), true ) );
1049+
layer->setDefaultValueDefinition( 2, QgsDefaultValue( QStringLiteral( "\"col1\"+1" ), true ) );
1050+
layer->setDefaultValueDefinition( 3, QgsDefaultValue( QStringLiteral( "\"col2\"+1" ), true ) );
10511051

10521052
layer->startEditing();
10531053

‎tests/src/python/test_qgsattributeform.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,38 @@ def test_on_update(self):
178178
form.changeAttribute('age', 7)
179179
self.assertEqual(form.currentFormFeature()['numbers'], [1, 7])
180180

181+
def test_default_value_always_updated(self):
182+
"""Test that default values are not updated on every edit operation
183+
when containing an 'attribute' expression"""
184+
185+
layer = QgsVectorLayer("Point?field=age:int&field=number:int", "vl", "memory")
186+
187+
layer.setEditorWidgetSetup(0, QgsEditorWidgetSetup('Range', {}))
188+
189+
# set default value for numbers to attribute("age"), it will depend on the field age and should not update
190+
layer.setDefaultValueDefinition(1, QgsDefaultValue("attribute(@feature, 'age')", False))
191+
layer.setEditorWidgetSetup(1, QgsEditorWidgetSetup('Range', {}))
192+
193+
layer.startEditing()
194+
195+
feature = QgsFeature(layer.fields())
196+
feature.setAttribute('age', 15)
197+
198+
form = QgsAttributeForm(layer)
199+
form.setMode(QgsAttributeEditorContext.AddFeatureMode)
200+
form.setFeature(feature)
201+
202+
QGISAPP.processEvents()
203+
204+
self.assertEqual(form.currentFormFeature()['age'], 15)
205+
self.assertEqual(form.currentFormFeature()['number'], 15)
206+
# return
207+
form.changeAttribute('number', 12)
208+
form.changeAttribute('age', 1)
209+
self.assertEqual(form.currentFormFeature()['number'], 12)
210+
form.changeAttribute('age', 7)
211+
self.assertEqual(form.currentFormFeature()['number'], 12)
212+
181213

182214
if __name__ == '__main__':
183215
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.