Skip to content

Commit

Permalink
Cache snapping results in feature dialog for subsequent recalculation of
Browse files Browse the repository at this point in the history
References #37359
  • Loading branch information
m-kuhn committed Jul 11, 2020
1 parent 2e2acf9 commit 15182ae
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 13 deletions.
8 changes: 8 additions & 0 deletions python/gui/auto_generated/qgsattributedialog.sip.in
Expand Up @@ -73,6 +73,14 @@ Intercept window activate/deactivate events to show/hide the highlighted feature
:param e: The event

:return: The same as the parent QDialog
%End

void setExtraContextScope( QgsExpressionContextScope *extraScope /Transfer/ );
%Docstring
Sets an additional expression context scope to be used
for calculations in this form.

.. versionadded:: 3.16
%End

public slots:
Expand Down
8 changes: 8 additions & 0 deletions python/gui/auto_generated/qgsattributeform.sip.in
Expand Up @@ -167,6 +167,14 @@ In this case it will return a combined expression according to the chosen filter
on all attribute widgets.

.. versionadded:: 3.0
%End

void setExtraContextScope( QgsExpressionContextScope *extraScope /Transfer/ );
%Docstring
Sets an additional expression context scope to be used
for calculations in this form.

.. versionadded:: 3.16
%End

signals:
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsfeatureaction.cpp
Expand Up @@ -65,6 +65,9 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )

QgsAttributeDialog *dialog = new QgsAttributeDialog( mLayer, f, cloneFeature, parentWidget(), true, context );
dialog->setWindowFlags( dialog->windowFlags() | Qt::Tool );
if ( scope )
dialog->setExtraContextScope( new QgsExpressionContextScope( *scope ) );


dialog->setObjectName( QStringLiteral( "featureactiondlg:%1:%2" ).arg( mLayer->id() ).arg( f->id() ) );

Expand Down Expand Up @@ -255,6 +258,8 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap &defaultAttributes, boo
dialog->setAttribute( Qt::WA_DeleteOnClose );
dialog->setMode( QgsAttributeEditorContext::AddFeatureMode );
dialog->setEditCommandMessage( text() );
if ( scope )
dialog->setExtraContextScope( new QgsExpressionContextScope( *scope ) );

connect( dialog->attributeForm(), &QgsAttributeForm::featureSaved, this, &QgsFeatureAction::onFeatureSaved );

Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsattributedialog.cpp
Expand Up @@ -148,3 +148,8 @@ bool QgsAttributeDialog::event( QEvent *e )

return QDialog::event( e );
}

void QgsAttributeDialog::setExtraContextScope( QgsExpressionContextScope *extraScope )
{
mAttributeForm->setExtraContextScope( extraScope );
}
8 changes: 8 additions & 0 deletions src/gui/qgsattributedialog.h
Expand Up @@ -95,6 +95,14 @@ class GUI_EXPORT QgsAttributeDialog : public QDialog
*/
bool event( QEvent *e ) override;

/**
* Sets an additional expression context scope to be used
* for calculations in this form.
*
* \since QGIS 3.16
*/
void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );

public slots:
void accept() override;
void reject() override;
Expand Down
34 changes: 21 additions & 13 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -516,7 +516,8 @@ bool QgsAttributeForm::updateDefaultValues( const int originIdx )
if ( mAlreadyUpdatedFields.contains( eww->fieldIdx() ) )
continue;

QString value = mLayer->defaultValue( eww->fieldIdx(), updatedFeature ).toString();
QgsExpressionContext context = createExpressionContext( updatedFeature );
QString value = mLayer->defaultValue( eww->fieldIdx(), updatedFeature, &context ).toString();
eww->setValue( value );
}
}
Expand Down Expand Up @@ -832,6 +833,17 @@ QString QgsAttributeForm::createFilterExpression() const
return filter;
}

QgsExpressionContext QgsAttributeForm::createExpressionContext( const QgsFeature &feature ) const
{
QgsExpressionContext context;
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
context.appendScope( QgsExpressionContextUtils::formScope( feature, mContext.attributeFormModeString() ) );
if ( mExtraContextScope )
context.appendScope( new QgsExpressionContextScope( *mExtraContextScope.get() ) );
context.setFeature( feature );
return context;
}


void QgsAttributeForm::onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues )
{
Expand Down Expand Up @@ -950,10 +962,7 @@ void QgsAttributeForm::updateConstraints( QgsEditorWidgetWrapper *eww )
// sync OK button status
synchronizeEnabledState();

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

// Recheck visibility for all containers which are controlled by this value
const QVector<ContainerInformation *> infos = mContainerInformationDependency.value( eww->field().name() );
Expand All @@ -966,10 +975,7 @@ void QgsAttributeForm::updateConstraints( QgsEditorWidgetWrapper *eww )

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

const QVector<ContainerInformation *> infos = mContainerVisibilityInformation;

Expand Down Expand Up @@ -1017,10 +1023,7 @@ void QgsAttributeForm::updateLabels()
QgsFeature currentFeature;
if ( currentFormFeature( currentFeature ) )
{
QgsExpressionContext context;
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
context.appendScope( QgsExpressionContextUtils::formScope( currentFeature, mContext.attributeFormModeString() ) );
context.setFeature( currentFeature );
QgsExpressionContext context = createExpressionContext( currentFeature );

for ( auto it = mLabelDataDefinedProperties.constBegin() ; it != mLabelDataDefinedProperties.constEnd(); ++it )
{
Expand Down Expand Up @@ -2380,6 +2383,11 @@ QString QgsAttributeForm::aggregateFilter() const
return filters.join( QStringLiteral( " AND " ) );
}

void QgsAttributeForm::setExtraContextScope( QgsExpressionContextScope *extraScope )
{
mExtraContextScope.reset( extraScope );
}

int QgsAttributeForm::messageTimeout()
{
QgsSettings settings;
Expand Down
11 changes: 11 additions & 0 deletions src/gui/qgsattributeform.h
Expand Up @@ -183,6 +183,14 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
*/
QString aggregateFilter() const;

/**
* Sets an additional expression context scope to be used
* for calculations in this form.
*
* \since QGIS 3.16
*/
void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );

signals:

/**
Expand Down Expand Up @@ -387,6 +395,8 @@ class GUI_EXPORT QgsAttributeForm : public QWidget

QString createFilterExpression() const;

QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;

//! constraints management
void updateAllConstraints();
void updateConstraints( QgsEditorWidgetWrapper *w );
Expand All @@ -407,6 +417,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
QList<QgsWidgetWrapper *> mWidgets;
QgsAttributeEditorContext mContext;
std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
QDialogButtonBox *mButtonBox = nullptr;
QWidget *mSearchButtonBox = nullptr;
QList<QgsAttributeFormInterface *> mInterfaces;
Expand Down

0 comments on commit 15182ae

Please sign in to comment.