Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #35881 from nyalldawson/fix_35558
Correctly utilise expression contexts for attribute form container visibility
  • Loading branch information
elpaso committed Apr 21, 2020
2 parents 5c587c0 + f56327a commit 4059305
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/gui/attributeformconfig/qgsattributeformcontaineredit.cpp
Expand Up @@ -48,6 +48,11 @@ QgsAttributeFormContainerEdit::QgsAttributeFormContainerEdit( QTreeWidgetItem *i
connect( mShowAsGroupBoxCheckBox, &QCheckBox::stateChanged, mShowLabelCheckBox, &QCheckBox::setEnabled );
}

void QgsAttributeFormContainerEdit::registerExpressionContextGenerator( QgsExpressionContextGenerator *generator )
{
mVisibilityExpressionWidget->registerExpressionContextGenerator( generator );
}

void QgsAttributeFormContainerEdit::updateItemData()
{
QgsAttributesFormProperties::DnDTreeItemData itemData = mTreeItem->data( 0, QgsAttributesFormProperties::DnDTreeRole ).value<QgsAttributesFormProperties::DnDTreeItemData>();
Expand Down
9 changes: 7 additions & 2 deletions src/gui/attributeformconfig/qgsattributeformcontaineredit.h
Expand Up @@ -38,12 +38,17 @@ class GUI_EXPORT QgsAttributeFormContainerEdit: public QWidget, private Ui_QgsAt
public:
explicit QgsAttributeFormContainerEdit( QTreeWidgetItem *item, QWidget *parent = nullptr );

/**
* Register an expression context generator class that will be used to retrieve
* an expression context for the widget when required.
* \since QGIS 3.14
*/
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );

void updateItemData();


private:
QTreeWidgetItem *mTreeItem;
QTreeWidgetItem *mTreeItem = nullptr;
};

#endif // QGSATTRIBUTEFORMCONTAINEREDIT_H
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
10 changes: 9 additions & 1 deletion src/gui/vector/qgsattributesformproperties.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsapplication.h"
#include "qgscolorbutton.h"
#include "qgscodeeditorhtml.h"
#include "qgsexpressioncontextutils.h"


QgsAttributesFormProperties::QgsAttributesFormProperties( QgsVectorLayer *layer, QWidget *parent )
Expand Down Expand Up @@ -183,9 +184,15 @@ void QgsAttributesFormProperties::initSuppressCombo()
mFormSuppressCmbBx->addItem( tr( "Show form on add feature" ) );

mFormSuppressCmbBx->setCurrentIndex( mLayer->editFormConfig().suppress() );
}


QgsExpressionContext QgsAttributesFormProperties::createExpressionContext() const
{
QgsExpressionContext context;
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
return context;
}

void QgsAttributesFormProperties::initLayoutConfig()
{
mEditorLayoutComboBox->setCurrentIndex( mEditorLayoutComboBox->findData( mLayer->editFormConfig().layout() ) );
Expand Down Expand Up @@ -432,6 +439,7 @@ void QgsAttributesFormProperties::loadAttributeContainerEdit()

QTreeWidgetItem *currentItem = mFormLayoutTree->selectedItems().at( 0 );
mAttributeContainerEdit = new QgsAttributeFormContainerEdit( currentItem, this );
mAttributeContainerEdit->registerExpressionContextGenerator( this );
mAttributeContainerEdit->layout()->setContentsMargins( 0, 0, 0, 0 );
mAttributeTypeFrame->layout()->setContentsMargins( 0, 0, 0, 0 );
mAttributeTypeFrame->layout()->addWidget( mAttributeContainerEdit );
Expand Down
4 changes: 3 additions & 1 deletion src/gui/vector/qgsattributesformproperties.h
Expand Up @@ -53,7 +53,7 @@ class QgsAttributeTypeDialog;
class QgsAttributeRelationEdit;
class QgsAttributeWidgetEdit;

class GUI_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAttributesFormProperties
class GUI_EXPORT QgsAttributesFormProperties : public QWidget, public QgsExpressionContextGenerator, private Ui_QgsAttributesFormProperties
{
Q_OBJECT

Expand Down Expand Up @@ -215,6 +215,8 @@ class GUI_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
void initInitPython();
void initSuppressCombo();

QgsExpressionContext createExpressionContext() const override;

protected:
void updateButtons();

Expand Down

0 comments on commit 4059305

Please sign in to comment.