Skip to content

Commit

Permalink
Correctly utilise expression contexts for attribute form container vi…
Browse files Browse the repository at this point in the history
…sibility

- we shouldn't use a single member instance of the context here, because continually
adding new scopes to that single instance will eventually cause the context to
become massive and slow
- correctly populate the context with the global/project/layer scopes

Fixes #35558
  • Loading branch information
nyalldawson committed Apr 20, 2020
1 parent ea9613e commit 4ca931d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -306,7 +306,6 @@ void QgsAttributeForm::setFeature( const QgsFeature &feature )
}
}
mIsSettingFeature = false;
mExpressionContext.setFeature( feature );
}

bool QgsAttributeForm::saveEdits()
Expand Down Expand Up @@ -951,28 +950,32 @@ void QgsAttributeForm::updateConstraints( QgsEditorWidgetWrapper *eww )
// sync OK button status
synchronizeEnabledState();

mExpressionContext.setFeature( ft );

mExpressionContext << QgsExpressionContextUtils::formScope( ft, mContext.attributeFormModeString() );
QgsExpressionContext context;
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
context.appendScope( QgsExpressionContextUtils::formScope( ft, mContext.attributeFormModeString() ) );
context.setFeature( ft );

// Recheck visibility for all containers which are controlled by this value
const QVector<ContainerInformation *> infos = mContainerInformationDependency.value( eww->field().name() );
for ( ContainerInformation *info : infos )
{
info->apply( &mExpressionContext );
info->apply( &context );
}
}
}

void QgsAttributeForm::updateContainersVisibility()
{
mExpressionContext << QgsExpressionContextUtils::formScope( QgsFeature( mFeature ), mContext.attributeFormModeString() );
QgsExpressionContext context;
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
context.appendScope( QgsExpressionContextUtils::formScope( mFeature, mContext.attributeFormModeString() ) );
context.setFeature( mFeature );

const QVector<ContainerInformation *> infos = mContainerVisibilityInformation;

for ( ContainerInformation *info : infos )
{
info->apply( &mExpressionContext );
info->apply( &context );
}

//and update the constraints
Expand Down Expand Up @@ -1014,13 +1017,16 @@ void QgsAttributeForm::updateLabels()
QgsFeature currentFeature;
if ( currentFormFeature( currentFeature ) )
{
mExpressionContext << QgsExpressionContextUtils::formScope( currentFeature, mContext.attributeFormModeString() );
mExpressionContext.setFields( mLayer->fields() );
QgsExpressionContext context;
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
context.appendScope( QgsExpressionContextUtils::formScope( currentFeature, mContext.attributeFormModeString() ) );
context.setFeature( currentFeature );

for ( auto it = mLabelDataDefinedProperties.constBegin() ; it != mLabelDataDefinedProperties.constEnd(); ++it )
{
QLabel *label { it.key() };
bool ok;
const QString value { it->valueAsString( mExpressionContext, QString(), &ok ) };
const QString value { it->valueAsString( context, QString(), &ok ) };
if ( ok && ! value.isEmpty() )
{
label->setText( value );
Expand Down
1 change: 0 additions & 1 deletion src/gui/qgsattributeform.h
Expand Up @@ -412,7 +412,6 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
QList<QgsAttributeFormInterface *> mInterfaces;
QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
QList< QgsAttributeFormWidget *> mFormWidgets;
QgsExpressionContext mExpressionContext;
QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
bool mValuesInitialized = false;
Expand Down

0 comments on commit 4ca931d

Please sign in to comment.