Skip to content

Commit

Permalink
Fix #51543 and make big forms more fluid
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 authored and github-actions[bot] committed Mar 30, 2023
1 parent 045bf94 commit 0a21144
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 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
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_alway_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 0a21144

Please sign in to comment.