Skip to content

Commit

Permalink
[attribute form] Properly handle default value using current_parent_*…
Browse files Browse the repository at this point in the history
… variable/function when in editing state
  • Loading branch information
nirvn committed Jul 6, 2024
1 parent 1f580a7 commit 667eb85
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
45 changes: 45 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,18 @@ void QgsAttributeForm::updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrap
}
}

void QgsAttributeForm::updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww )
{
if ( eww )
{
QString expression = eww->field().defaultValueDefinition().expression();
if ( expression.contains( QStringLiteral( "current_parent" ) ) )
{
mParentDependencies.insert( eww );
}
}
}

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

0 comments on commit 667eb85

Please sign in to comment.