Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
signedav committed Mar 31, 2023
2 parents 045bf94 + 625a064 commit 4b37c6e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
25 changes: 22 additions & 3 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -522,8 +522,6 @@ QgsFeature QgsAttributeForm::getUpdatedFeature() const

void QgsAttributeForm::updateValuesDependencies( const int originIdx )
{
updateFieldDependencies();

updateValuesDependenciesDefaultValues( originIdx );
updateValuesDependenciesVirtualFields( originIdx );
}
Expand All @@ -547,7 +545,11 @@ void QgsAttributeForm::updateValuesDependenciesDefaultValues( const int originId
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
if ( eww )
{
//do not update when when mMode is not AddFeatureMode and it's not applyOnUpdate
// Update only on form opening (except when applyOnUpdate is activated)
if ( mValuesInitialized && !eww->field().defaultValueDefinition().applyOnUpdate() )
continue;

// Update only when mMode is AddFeatureMode (except when applyOnUpdate is activated)
if ( mMode != QgsAttributeEditorContext::AddFeatureMode && !eww->field().defaultValueDefinition().applyOnUpdate() )
{
continue;
Expand Down Expand Up @@ -906,6 +908,23 @@ void QgsAttributeForm::resetValues()
{
ww->setFeature( mFeature );
}

// Prepare value dependencies
updateFieldDependencies();

// Update dependent fields
for ( QgsWidgetWrapper *ww : constMWidgets )
{
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
if ( !eww )
continue;

// Append field index here, so it's not updated recursively
mAlreadyUpdatedFields.append( eww->fieldIdx() );
updateValuesDependencies( eww->fieldIdx() );
mAlreadyUpdatedFields.removeAll( eww->fieldIdx() );
}

mValuesInitialized = true;
mDirty = false;
}
Expand Down
14 changes: 7 additions & 7 deletions tests/src/gui/testqgsattributeform.cpp
Expand Up @@ -982,9 +982,9 @@ void TestQgsAttributeForm::testDefaultValueUpdate()
//col3 - "col2"

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

layer->startEditing();

Expand Down Expand Up @@ -1044,10 +1044,10 @@ void TestQgsAttributeForm::testDefaultValueUpdateRecursion()
//col3 - COALESCE( 0, "col2"+1)

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

layer->startEditing();

Expand Down
32 changes: 32 additions & 0 deletions tests/src/python/test_qgsattributeform.py
Expand Up @@ -178,6 +178,38 @@ def test_on_update(self):
form.changeAttribute('age', 7)
self.assertEqual(form.currentFormFeature()['numbers'], [1, 7])

def test_default_value_always_updated(self):
"""Test that default values are not updated on every edit operation
when containing an 'attribute' expression"""

layer = QgsVectorLayer("Point?field=age:int&field=number:int", "vl", "memory")

layer.setEditorWidgetSetup(0, QgsEditorWidgetSetup('Range', {}))

# set default value for numbers to attribute("age"), it will depend on the field age and should not update
layer.setDefaultValueDefinition(1, QgsDefaultValue("attribute(@feature, 'age')", False))
layer.setEditorWidgetSetup(1, QgsEditorWidgetSetup('Range', {}))

layer.startEditing()

feature = QgsFeature(layer.fields())
feature.setAttribute('age', 15)

form = QgsAttributeForm(layer)
form.setMode(QgsAttributeEditorContext.AddFeatureMode)
form.setFeature(feature)

QGISAPP.processEvents()

self.assertEqual(form.currentFormFeature()['age'], 15)
self.assertEqual(form.currentFormFeature()['number'], 15)
# return
form.changeAttribute('number', 12)
form.changeAttribute('age', 1)
self.assertEqual(form.currentFormFeature()['number'], 12)
form.changeAttribute('age', 7)
self.assertEqual(form.currentFormFeature()['number'], 12)


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

0 comments on commit 4b37c6e

Please sign in to comment.