Skip to content

Commit 7952692

Browse files
authoredJun 18, 2020
Merge pull request #37156 from m-kuhn/backport-35881-release-3_10
Backport use expression context for conditional visibility
2 parents d56e32a + 6d836a7 commit 7952692

File tree

7 files changed

+41
-19
lines changed

7 files changed

+41
-19
lines changed
 

‎src/app/attributeformconfig/qgsattributeformcontaineredit.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ QgsAttributeFormContainerEdit::QgsAttributeFormContainerEdit( QTreeWidgetItem *i
4848
connect( mShowAsGroupBoxCheckBox, &QCheckBox::stateChanged, mShowLabelCheckBox, &QCheckBox::setEnabled );
4949
}
5050

51+
void QgsAttributeFormContainerEdit::registerExpressionContextGenerator( QgsExpressionContextGenerator *generator )
52+
{
53+
mVisibilityExpressionWidget->registerExpressionContextGenerator( generator );
54+
}
55+
5156
void QgsAttributeFormContainerEdit::updateItemData()
5257
{
5358
QgsAttributesFormProperties::DnDTreeItemData itemData = mTreeItem->data( 0, QgsAttributesFormProperties::DnDTreeRole ).value<QgsAttributesFormProperties::DnDTreeItemData>();

‎src/app/attributeformconfig/qgsattributeformcontaineredit.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ class APP_EXPORT QgsAttributeFormContainerEdit: public QWidget, private Ui_QgsAt
3434
public:
3535
explicit QgsAttributeFormContainerEdit( QTreeWidgetItem *item, QWidget *parent = nullptr );
3636

37+
/**
38+
* Register an expression context generator class that will be used to retrieve
39+
* an expression context for the widget when required.
40+
* \since QGIS 3.14
41+
*/
42+
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
3743

3844
void updateItemData();
3945

40-
4146
private:
42-
QTreeWidgetItem *mTreeItem;
47+
QTreeWidgetItem *mTreeItem = nullptr;
4348
};
4449

4550
#endif // QGSATTRIBUTEFORMCONTAINEREDIT_H

‎src/app/qgsattributesformproperties.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsapplication.h"
2727
#include "qgscolorbutton.h"
2828
#include "qgscodeeditorhtml.h"
29+
#include "qgsexpressioncontextutils.h"
2930

3031

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

185186
mFormSuppressCmbBx->setCurrentIndex( mLayer->editFormConfig().suppress() );
187+
}
186188

187-
189+
QgsExpressionContext QgsAttributesFormProperties::createExpressionContext() const
190+
{
191+
QgsExpressionContext context;
192+
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
193+
return context;
188194
}
195+
189196
void QgsAttributesFormProperties::initLayoutConfig()
190197
{
191198
mEditorLayoutComboBox->setCurrentIndex( mEditorLayoutComboBox->findData( mLayer->editFormConfig().layout() ) );
@@ -428,6 +435,7 @@ void QgsAttributesFormProperties::loadAttributeContainerEdit()
428435

429436
QTreeWidgetItem *currentItem = mFormLayoutTree->selectedItems().at( 0 );
430437
mAttributeContainerEdit = new QgsAttributeFormContainerEdit( currentItem, this );
438+
mAttributeContainerEdit->registerExpressionContextGenerator( this );
431439
mAttributeTypeFrame->layout()->setMargin( 0 );
432440
mAttributeTypeFrame->layout()->addWidget( mAttributeContainerEdit );
433441
}

‎src/app/qgsattributesformproperties.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class QgsAttributeRelationEdit;
5252
class QgsAttributeWidgetEdit;
5353
class DnDTree;
5454

55-
class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAttributesFormProperties
55+
class APP_EXPORT QgsAttributesFormProperties : public QWidget, public QgsExpressionContextGenerator,private Ui_QgsAttributesFormProperties
5656
{
5757
Q_OBJECT
5858

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

211+
QgsExpressionContext createExpressionContext() const override;
212+
211213
protected:
212214
void updateButtons();
213215

‎src/gui/editorwidgets/qgsdatetimeedit.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ void QgsDateTimeEdit::clear()
6262
{
6363
displayCurrentDate();
6464

65-
// Check if it's really changed or crash, see GH #29937
66-
if ( ! dateTime().isNull() )
67-
{
68-
changed( QDateTime() );
69-
}
65+
changed( QDateTime() );
7066

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

193189
void QgsDateTimeEdit::changed( const QDateTime &dateTime )
194190
{
195-
mIsEmpty = false;
196191
bool isNull = dateTime.isNull();
192+
193+
if ( mIsNull == isNull && QgsDateTimeEdit::dateTime() == dateTime )
194+
return;
195+
196+
mIsEmpty = false;
197197
if ( isNull != mIsNull )
198198
{
199199
mIsNull = isNull;
@@ -275,8 +275,7 @@ void QgsDateTimeEdit::setDateTime( const QDateTime &dateTime )
275275
clear();
276276
displayNull();
277277
}
278-
// Check if it's really changed or crash, see GH #29937
279-
else if ( dateTime != QgsDateTimeEdit::dateTime() )
278+
else
280279
{
281280
QDateTimeEdit::setDateTime( dateTime );
282281
changed( dateTime );

‎src/gui/qgsattributeform.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -931,28 +931,32 @@ void QgsAttributeForm::updateConstraints( QgsEditorWidgetWrapper *eww )
931931
// sync OK button status
932932
synchronizeEnabledState();
933933

934-
mExpressionContext.setFeature( ft );
935-
936-
mExpressionContext << QgsExpressionContextUtils::formScope( ft, mContext.attributeFormModeString() );
934+
QgsExpressionContext context;
935+
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
936+
context.appendScope( QgsExpressionContextUtils::formScope( ft, mContext.attributeFormModeString() ) );
937+
context.setFeature( ft );
937938

938939
// Recheck visibility for all containers which are controlled by this value
939940
const QVector<ContainerInformation *> infos = mContainerInformationDependency.value( eww->field().name() );
940941
for ( ContainerInformation *info : infos )
941942
{
942-
info->apply( &mExpressionContext );
943+
info->apply( &context );
943944
}
944945
}
945946
}
946947

947948
void QgsAttributeForm::updateContainersVisibility()
948949
{
949-
mExpressionContext << QgsExpressionContextUtils::formScope( QgsFeature( mFeature ), mContext.attributeFormModeString() );
950+
QgsExpressionContext context;
951+
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
952+
context.appendScope( QgsExpressionContextUtils::formScope( mFeature, mContext.attributeFormModeString() ) );
953+
context.setFeature( mFeature );
950954

951955
const QVector<ContainerInformation *> infos = mContainerVisibilityInformation;
952956

953957
for ( ContainerInformation *info : infos )
954958
{
955-
info->apply( &mExpressionContext );
959+
info->apply( &context );
956960
}
957961

958962
//and update the constraints

‎src/gui/qgsattributeform.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
391391
QList<QgsAttributeFormInterface *> mInterfaces;
392392
QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
393393
QList< QgsAttributeFormWidget *> mFormWidgets;
394-
QgsExpressionContext mExpressionContext;
395394
QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
396395
bool mValuesInitialized = false;
397396
bool mDirty = false;

0 commit comments

Comments
 (0)
Please sign in to comment.