Skip to content
Permalink
Browse files

Merge pull request #37156 from m-kuhn/backport-35881-release-3_10

Backport use expression context for conditional visibility
  • Loading branch information
elpaso committed Jun 18, 2020
2 parents d56e32a + 6d836a7 commit 7952692f79c727fd78797d888935c4c2146ec092
@@ -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>();
@@ -34,12 +34,17 @@ class APP_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,6 +26,7 @@
#include "qgsapplication.h"
#include "qgscolorbutton.h"
#include "qgscodeeditorhtml.h"
#include "qgsexpressioncontextutils.h"


QgsAttributesFormProperties::QgsAttributesFormProperties( QgsVectorLayer *layer, QWidget *parent )
@@ -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() ) );
@@ -428,6 +435,7 @@ void QgsAttributesFormProperties::loadAttributeContainerEdit()

QTreeWidgetItem *currentItem = mFormLayoutTree->selectedItems().at( 0 );
mAttributeContainerEdit = new QgsAttributeFormContainerEdit( currentItem, this );
mAttributeContainerEdit->registerExpressionContextGenerator( this );
mAttributeTypeFrame->layout()->setMargin( 0 );
mAttributeTypeFrame->layout()->addWidget( mAttributeContainerEdit );
}
@@ -52,7 +52,7 @@ class QgsAttributeRelationEdit;
class QgsAttributeWidgetEdit;
class DnDTree;

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

@@ -208,6 +208,8 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
void initInitPython();
void initSuppressCombo();

QgsExpressionContext createExpressionContext() const override;

protected:
void updateButtons();

@@ -62,11 +62,7 @@ void QgsDateTimeEdit::clear()
{
displayCurrentDate();

// Check if it's really changed or crash, see GH #29937
if ( ! dateTime().isNull() )
{
changed( QDateTime() );
}
changed( QDateTime() );

// emit signal of QDateTime::dateTimeChanged with an invalid date
// anyway, using parent's signal should be avoided
@@ -192,8 +188,12 @@ void QgsDateTimeEdit::showEvent( QShowEvent *event )

void QgsDateTimeEdit::changed( const QDateTime &dateTime )
{
mIsEmpty = false;
bool isNull = dateTime.isNull();

if ( mIsNull == isNull && QgsDateTimeEdit::dateTime() == dateTime )
return;

mIsEmpty = false;
if ( isNull != mIsNull )
{
mIsNull = isNull;
@@ -275,8 +275,7 @@ void QgsDateTimeEdit::setDateTime( const QDateTime &dateTime )
clear();
displayNull();
}
// Check if it's really changed or crash, see GH #29937
else if ( dateTime != QgsDateTimeEdit::dateTime() )
else
{
QDateTimeEdit::setDateTime( dateTime );
changed( dateTime );
@@ -931,28 +931,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
@@ -391,7 +391,6 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
QList<QgsAttributeFormInterface *> mInterfaces;
QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
QList< QgsAttributeFormWidget *> mFormWidgets;
QgsExpressionContext mExpressionContext;
QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
bool mValuesInitialized = false;
bool mDirty = false;

0 comments on commit 7952692

Please sign in to comment.
You can’t perform that action at this time.