Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #51543 and make big forms more fluid by reducing calls to updateFieldDependencies #51836

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/gui/qgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,6 @@ QgsFeature QgsAttributeForm::getUpdatedFeature() const

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

updateValuesDependenciesDefaultValues( originIdx );
updateValuesDependenciesVirtualFields( originIdx );
}
Expand All @@ -557,7 +555,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)
domi4484 marked this conversation as resolved.
Show resolved Hide resolved
if ( mMode != QgsAttributeEditorContext::AddFeatureMode && !eww->field().defaultValueDefinition().applyOnUpdate() )
{
continue;
Expand Down Expand Up @@ -916,6 +918,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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()