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

[attribute form] Properly handle default value using current_parent_* variable/function when in editing state #58009

Merged
merged 4 commits into from
Jul 10, 2024
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
50 changes: 50 additions & 0 deletions src/gui/qgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,28 @@ void QgsAttributeForm::updateValuesDependenciesVirtualFields( const int originId
}
}

void QgsAttributeForm::updateValuesDependenciesParent()
{
// create updated Feature
QgsFeature updatedFeature = getUpdatedFeature();
QList<int> updatedFields;

// go through fields dependent to parent feature value(s)
const QSet<QgsEditorWidgetWrapper *> relevantWidgets = mParentDependencies;
for ( QgsEditorWidgetWrapper *eww : relevantWidgets )
{
//do not update when this widget is already updating (avoid recursions)
if ( updatedFields.contains( eww->fieldIdx() ) )
continue;

// Update value
updatedFields << eww->fieldIdx();
QgsExpressionContext context = createExpressionContext( updatedFeature );
const QVariant value = mLayer->defaultValue( eww->fieldIdx(), updatedFeature, &context );
eww->setValue( value );
}
}

void QgsAttributeForm::updateRelatedLayerFields()
{
// Synchronize dependencies
Expand Down Expand Up @@ -1527,6 +1549,15 @@ void QgsAttributeForm::refreshFeature()

void QgsAttributeForm::parentFormValueChanged( const QString &attribute, const QVariant &newValue )
{
if ( mContext.parentFormFeature().isValid() )
{
QgsFeature parentFormFeature = mContext.parentFormFeature();
parentFormFeature.setAttribute( attribute, newValue );
mContext.setParentFormFeature( parentFormFeature );
}

updateValuesDependenciesParent();

for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
{
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
Expand Down Expand Up @@ -3147,6 +3178,7 @@ void QgsAttributeForm::updateFieldDependencies()
mDefaultValueDependencies.clear();
mVirtualFieldsDependencies.clear();
mRelatedLayerFieldsDependencies.clear();
mParentDependencies.clear();

//create defaultValueDependencies
for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
Expand All @@ -3155,6 +3187,7 @@ void QgsAttributeForm::updateFieldDependencies()
if ( ! eww )
continue;

updateFieldDependenciesParent( eww );
updateFieldDependenciesDefaultValue( eww );
updateFieldDependenciesVirtualFields( eww );
updateRelatedLayerFieldsDependencies( eww );
Expand Down Expand Up @@ -3245,6 +3278,23 @@ void QgsAttributeForm::updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrap
}
}

void QgsAttributeForm::updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww )
{
if ( eww && !eww->field().defaultValueDefinition().expression().isEmpty() )
{
const QgsExpression expression( eww->field().defaultValueDefinition().expression() );
const QSet< QString > referencedVariablesAndFunctions = expression.referencedVariables() + expression.referencedFunctions();
for ( const QString &referenced : referencedVariablesAndFunctions )
{
if ( referenced.startsWith( QStringLiteral( "current_parent" ) ) )
{
mParentDependencies.insert( eww );
break;
}
}
}
}

void QgsAttributeForm::setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids )
{
for ( QgsAttributeFormWidget *formWidget : mFormWidgets )
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgsattributeform.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
void updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww );

void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );

Expand Down Expand Up @@ -422,6 +423,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
void updateValuesDependencies( const int originIdx );
void updateValuesDependenciesDefaultValues( const int originIdx );
void updateValuesDependenciesVirtualFields( const int originIdx );
void updateValuesDependenciesParent();
void updateRelatedLayerFields();

void clearMultiEditMessages();
Expand Down Expand Up @@ -555,6 +557,8 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
*/
QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;

QSet<QgsEditorWidgetWrapper *> mParentDependencies;

//! List of updated fields to avoid recursion on the setting of defaultValues
QList<int> mAlreadyUpdatedFields;

Expand Down
33 changes: 33 additions & 0 deletions tests/src/gui/testqgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TestQgsAttributeForm : public QObject
void testAttributeFormInterface();
void testDefaultValueUpdate();
void testDefaultValueUpdateRecursion();
void testParentFeatureUpdate();
void testSameFieldSync();
void testZeroDoubles();
void testMinimumWidth();
Expand Down Expand Up @@ -1111,6 +1112,38 @@ void TestQgsAttributeForm::testDefaultValueUpdateRecursion()
QCOMPARE( ww3->value().toInt(), 43 );
}

void TestQgsAttributeForm::testParentFeatureUpdate()
{
// make a temporary layer to check through
const QString def = QStringLiteral( "Point?field=col0:integer" );
QgsVectorLayer *layer = new QgsVectorLayer( def, QStringLiteral( "test" ), QStringLiteral( "memory" ) );
layer->setDefaultValueDefinition( 0, QgsDefaultValue( QStringLiteral( "current_parent_value('colZero\')" ), true ) );
layer->startEditing();

// initialize parent feature
QgsFields parentFields;
parentFields.append( QgsField( QStringLiteral( "colZero" ), QMetaType::Type::Int ) );

QgsFeature parentFeature( parentFields, 1 );
parentFeature.setAttribute( QStringLiteral( "colZero" ), 10 );

// initialize child feature
QgsFeature feature( layer->dataProvider()->fields(), 1 );

// build a form
QgsAttributeEditorContext context;
context.setParentFormFeature( parentFeature );
QgsAttributeForm form( layer, feature, context );

// get wrappers for each widget
QgsEditorWidgetWrapper *ww0;
ww0 = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[0] );

form.parentFormValueChanged( QStringLiteral( "colZero" ), 20 );

QCOMPARE( ww0->value().toInt(), 20 );
}

void TestQgsAttributeForm::testSameFieldSync()
{
// Check that widget synchronisation works when a form contains the same field several times
Expand Down
Loading