diff --git a/app/attributes/attributecontroller.cpp b/app/attributes/attributecontroller.cpp index ad008c6d2..84f3d0f12 100644 --- a/app/attributes/attributecontroller.cpp +++ b/app/attributes/attributecontroller.cpp @@ -232,6 +232,27 @@ void AttributeController::flatten( QStringList expressions; QString expression = field.constraints().constraintExpression(); + QgsEditFormConfig editFormConfig = layer->editFormConfig(); + QString fieldName = field.name(); + QgsPropertyCollection fieldProperties = editFormConfig.dataDefinedFieldProperties( fieldName ); + + // Retrieving field name expression + QgsProperty nameProperty = fieldProperties.property( QgsEditFormConfig::DataDefinedProperty::Alias ); + QString nameExpressionString = nameProperty.expressionString(); + QgsExpression nameExpression( nameExpressionString ); + + // Retrieving field editability expression + QgsProperty editableProperty = fieldProperties.property( QgsEditFormConfig::DataDefinedProperty::Editable ); + QString editableExpressionString; + QgsExpression editableExpression; + + // Bypass reading the editable expression if Apply default value on update is enabled + if ( !field.defaultValueDefinition().applyOnUpdate() ) + { + editableExpressionString = editableProperty.expressionString(); + editableExpression = QgsExpression( editableExpressionString ); + } + if ( !expression.isEmpty() ) { expressions << field.constraints().constraintExpression(); @@ -249,8 +270,10 @@ void AttributeController::flatten( groupName, parentTabRow, layer->attributeDisplayName( fieldIndex ), + nameExpression, editorField->showLabel(), !isReadOnly, + editableExpression, getEditorWidgetSetup( layer, fieldIndex ), fieldIndex, parentVisibilityExpressions // field doesn't have visibility expression itself @@ -904,6 +927,64 @@ void AttributeController::recalculateDerivedItems( bool isFormValueChange, bool } } + // Evaluate if form items are editable + { + QMap>::iterator formItemsIterator = mFormItems.begin(); + while ( formItemsIterator != mFormItems.end() ) + { + std::shared_ptr item = formItemsIterator.value(); + QgsExpression exp = item->editableExpression(); + + if ( !exp.expression().isEmpty() ) + { + bool editable = item->isEditable(); + exp.prepare( &expressionContext ); + + if ( exp.isValid() ) + { + editable = exp.evaluate( &expressionContext ).toBool(); + } + + if ( item->isEditable() != editable ) + { + item->setIsEditable( editable ); + changedFormItems << item->id(); + } + } + + ++formItemsIterator; + } + } + + // Evaluate form items name + { + QMap>::iterator formItemsIterator = mFormItems.begin(); + while ( formItemsIterator != mFormItems.end() ) + { + std::shared_ptr item = formItemsIterator.value(); + QgsExpression exp = item->nameExpression(); + + if ( !exp.expression().isEmpty() ) + { + QString name = item->name(); + exp.prepare( &expressionContext ); + + if ( exp.isValid() ) + { + name = exp.evaluate( &expressionContext ).toString(); + } + + if ( item->name() != name ) + { + item->setName( name ); + changedFormItems << item->id(); + } + } + + ++formItemsIterator; + } + } + // Evaluate form items value state - hard/soft constraints, value validity { bool containsValidationError = false; diff --git a/app/attributes/attributedata.cpp b/app/attributes/attributedata.cpp index b76fa17c8..a9e761cda 100644 --- a/app/attributes/attributedata.cpp +++ b/app/attributes/attributedata.cpp @@ -22,8 +22,10 @@ FormItem::FormItem( const QUuid &id, const int parentTabId, FormItem::FormItemType type, const QString &name, + const QgsExpression &nameExpression, bool showName, bool isEditable, + const QgsExpression &editableExpression, const QgsEditorWidgetSetup &editorWidgetSetup, int fieldIndex, const QgsExpression &visibilityExpression, @@ -35,8 +37,10 @@ FormItem::FormItem( const QUuid &id, , mParentTabId( parentTabId ) , mType( type ) , mName( name ) + , mNameExpression( nameExpression ) , mShowName( showName ) , mIsEditable( isEditable ) + , mEditableExpression( editableExpression ) , mEditorWidgetSetup( editorWidgetSetup ) , mFieldIndex( fieldIndex ) , mVisibilityExpression( visibilityExpression ) @@ -48,8 +52,8 @@ FormItem *FormItem::createFieldItem( const QUuid &id, const QgsField &field, const QString &groupName, int parentTabId, - const QString &name, bool showName, - bool isEditable, + const QString &name, const QgsExpression &nameExpression, bool showName, + bool isEditable, const QgsExpression &editableExpression, const QgsEditorWidgetSetup &editorWidgetSetup, int fieldIndex, @@ -63,8 +67,10 @@ FormItem *FormItem::createFieldItem( const QUuid &id, parentTabId, FormItem::Field, name, + nameExpression, showName, isEditable, + editableExpression, editorWidgetSetup, fieldIndex, visibilityExpression, @@ -88,8 +94,10 @@ FormItem *FormItem::createRelationItem( const QUuid &id, parentTabId, FormItem::Relation, name, + QgsExpression(), showName, true, + QgsExpression(), QgsEditorWidgetSetup(), -1, visibilityExpression, @@ -119,8 +127,10 @@ FormItem *FormItem::createSpacerItem( parentTabId, FormItem::Spacer, name, + QgsExpression(), false, // label is never shown for spacer false, + QgsExpression(), config, -1, visibilityExpression, @@ -153,8 +163,10 @@ FormItem *FormItem::createRichTextItem( parentTabId, FormItem::RichText, name, + QgsExpression(), showName, false, + QgsExpression(), config, -1, visibilityExpression, @@ -175,6 +187,11 @@ QString FormItem::name() const return mName; } +void FormItem::setName( QString name ) +{ + mName = name; +} + bool FormItem::isEditable() const { return mIsEditable; @@ -228,6 +245,11 @@ void FormItem::setVisible( bool visible ) mVisible = visible; } +void FormItem::setIsEditable( bool editable ) +{ + mIsEditable = editable; +} + QUuid FormItem::id() const { return mId; @@ -243,6 +265,16 @@ QgsExpression FormItem::visibilityExpression() const return mVisibilityExpression; } +QgsExpression FormItem::nameExpression() const +{ + return mNameExpression; +} + +QgsExpression FormItem::editableExpression() const +{ + return mEditableExpression; +} + bool FormItem::visible() const { return mVisible; diff --git a/app/attributes/attributedata.h b/app/attributes/attributedata.h index 133a2e9b3..d24375429 100644 --- a/app/attributes/attributedata.h +++ b/app/attributes/attributedata.h @@ -57,8 +57,10 @@ class FormItem int parentTabId, FormItem::FormItemType type, const QString &name, + const QgsExpression &nameExpression, bool showName, bool isEditable, + const QgsExpression &editableExpression, const QgsEditorWidgetSetup &editorWidgetSetup, int fieldIndex, const QgsExpression &visibilityExpression, @@ -71,8 +73,10 @@ class FormItem const QString &groupName, int parentTabId, const QString &name, + const QgsExpression &nameExpression, bool showName, bool isEditable, + const QgsExpression &editableExpression, const QgsEditorWidgetSetup &editorWidgetSetup, int fieldIndex, const QgsExpression &visibilityExpression @@ -109,8 +113,10 @@ class FormItem ); FormItem::FormItemType type() const; + QString name() const; - bool isEditable() const; + void setName( QString name ); + QString editorWidgetType() const; QVariantMap editorWidgetConfig() const; int fieldIndex() const; @@ -121,6 +127,9 @@ class FormItem FieldValidator::ValidationStatus validationStatus() const; void setValidationStatus( FieldValidator::ValidationStatus status ); + bool isEditable() const; + void setIsEditable( bool editable ); + bool isVisible() const; void setVisible( bool visible ); @@ -129,6 +138,8 @@ class FormItem int parentTabId() const; QgsExpression visibilityExpression() const; + QgsExpression nameExpression() const; + QgsExpression editableExpression() const; bool visible() const; @@ -154,15 +165,17 @@ class FormItem const QString mGroupName; //empty for no group, group/tab name if widget is in container const int mParentTabId; const FormItem::FormItemType mType; - const QString mName; const bool mShowName = true; // "Show label" in Widget Display group in QGIS widget settings - const bool mIsEditable; const QgsEditorWidgetSetup mEditorWidgetSetup; const int mFieldIndex; const QgsExpression mVisibilityExpression; + const QgsExpression mNameExpression; // Expression to define field’s display name (alias) + const QgsExpression mEditableExpression; // Expression to determine whether the field is editable + QString mName; QString mValidationMessage; FieldValidator::ValidationStatus mValidationStatus = FieldValidator::Valid; + bool mIsEditable = true; bool mVisible = false; QVariant mOriginalValue; // original unmodified value QVariant mRawValue;