Skip to content

Commit

Permalink
[atribute form] Add parent context when editing a child feature throu…
Browse files Browse the repository at this point in the history
…gh the relation editor widget
  • Loading branch information
nirvn committed Apr 28, 2024
1 parent 5739343 commit 0c108b3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/gui/attributeformconfig/qgsattributetypedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ QgsExpressionContext QgsAttributeTypeDialog::createExpressionContext() const
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::layerScope( mLayer )
<< QgsExpressionContextUtils::formScope( )
<< QgsExpressionContextUtils::parentFormScope( QgsFeature() )
<< QgsExpressionContextUtils::mapToolCaptureScope( QList<QgsPointLocator::Match>() );

return context;
Expand Down
53 changes: 52 additions & 1 deletion src/gui/qgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,13 @@ bool QgsAttributeForm::saveEdits( QString *error )
// An add dialog should perform an action by default
// and not only if attributes have "changed"
if ( mMode == QgsAttributeEditorContext::AddFeatureMode || mMode == QgsAttributeEditorContext::FixAttributeMode )
{
doUpdate = true;
}
else
{
updateDefaultValues();
}

QgsAttributes src = mFeature.attributes();
QgsAttributes dst = mFeature.attributes();
Expand Down Expand Up @@ -462,7 +468,7 @@ bool QgsAttributeForm::saveEdits( QString *error )
n++;
}

success = mLayer->changeAttributeValues( mFeature.id(), newValues, oldValues );
success = mLayer->changeAttributeValues( mFeature.id(), newValues, oldValues, true );

if ( success && n > 0 )
{
Expand Down Expand Up @@ -530,6 +536,44 @@ QgsFeature QgsAttributeForm::getUpdatedFeature() const
return updatedFeature;
}

void QgsAttributeForm::updateDefaultValues()
{
if ( !mFeature.isValid()
&& mMode != QgsAttributeEditorContext::AddFeatureMode )
return;

// create updated Feature
QgsFeature updatedFeature = getUpdatedFeature();

// go through depending fields and update the fields with defaultexpression
for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
{
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
if ( eww )
{
// 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;
}

//do not update when this widget is already updating (avoid recursions)
if ( mAlreadyUpdatedFields.contains( eww->fieldIdx() ) )
continue;

QgsExpressionContext context = createExpressionContext( updatedFeature );

const QVariant value = mLayer->defaultValue( eww->fieldIdx(), updatedFeature, &context );
eww->setValue( value );
mCurrentFormFeature.setAttribute( eww->field().name(), value );
}
}
}

void QgsAttributeForm::updateValuesDependencies( const int originIdx )
{
updateValuesDependenciesDefaultValues( originIdx );
Expand Down Expand Up @@ -570,6 +614,7 @@ void QgsAttributeForm::updateValuesDependenciesDefaultValues( const int originId
continue;

QgsExpressionContext context = createExpressionContext( updatedFeature );

const QVariant value = mLayer->defaultValue( eww->fieldIdx(), updatedFeature, &context );
eww->setValue( value );
mCurrentFormFeature.setAttribute( eww->field().name(), value );
Expand Down Expand Up @@ -984,7 +1029,13 @@ QgsExpressionContext QgsAttributeForm::createExpressionContext( const QgsFeature
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
context.appendScope( QgsExpressionContextUtils::formScope( feature, mContext.attributeFormModeString() ) );
if ( mExtraContextScope )
{
context.appendScope( new QgsExpressionContextScope( *mExtraContextScope.get() ) );
}
if ( mContext.parentFormFeature().isValid() )
{
context.appendScope( QgsExpressionContextUtils::parentFormScope( mContext.parentFormFeature() ) );
}
context.setFeature( feature );
return context;
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsattributeform.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
void updateValuesDependenciesVirtualFields( const int originIdx );
void updateRelatedLayerFields();

void updateDefaultValues();

void clearMultiEditMessages();
void pushSelectedFeaturesMessage();
void runSearchSelect( Qgis::SelectBehavior behavior );
Expand Down

0 comments on commit 0c108b3

Please sign in to comment.