Skip to content

Commit

Permalink
Support for editable states and alias expressions for fields (#3645)
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ authored Jan 14, 2025
1 parent 7892d92 commit df0d199
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 7 deletions.
85 changes: 83 additions & 2 deletions app/attributes/attributecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,34 @@ void AttributeController::flatten(
QStringList expressions;
QString expression = field.constraints().constraintExpression();

if ( !expression.isEmpty() )
QgsEditFormConfig editFormConfig = layer->editFormConfig();
QString fieldName = field.name();
QgsPropertyCollection fieldProperties = editFormConfig.dataDefinedFieldProperties( fieldName );

// Retrieving field name expression
QgsProperty nameProperty = fieldProperties.property( QgsEditFormConfig::DataDefinedProperty::Alias );
QgsExpression nameExpression; // empty if users set to hide the field label

if ( editorField->showLabel() )
{
expressions << field.constraints().constraintExpression();
nameExpression = QgsExpression( nameProperty.expressionString() );
}

// Retrieving field editability expression
QgsProperty editableProperty = fieldProperties.property( QgsEditFormConfig::DataDefinedProperty::Editable );
bool isReadOnly = ( layer->editFormConfig().readOnly( fieldIndex ) ) ||
( !field.defaultValueDefinition().expression().isEmpty() && field.defaultValueDefinition().applyOnUpdate() );
QgsExpression isEditableExpression; // empty if the field is read-only

if ( !isReadOnly )
{
isEditableExpression = QgsExpression( editableProperty.expressionString() );
}

if ( !expression.isEmpty() )
{
expressions << field.constraints().constraintExpression();
}

const QString groupName = container->isGroupBox() ? container->name() : QString();
std::shared_ptr<FormItem> formItemData =
Expand All @@ -249,8 +270,10 @@ void AttributeController::flatten(
groupName,
parentTabRow,
layer->attributeDisplayName( fieldIndex ),
nameExpression,
editorField->showLabel(),
!isReadOnly,
isEditableExpression,
getEditorWidgetSetup( layer, fieldIndex ),
fieldIndex,
parentVisibilityExpressions // field doesn't have visibility expression itself
Expand Down Expand Up @@ -904,6 +927,64 @@ void AttributeController::recalculateDerivedItems( bool isFormValueChange, bool
}
}

// Evaluate if form items are editable
{
QMap<QUuid, std::shared_ptr<FormItem>>::iterator formItemsIterator = mFormItems.begin();
while ( formItemsIterator != mFormItems.end() )
{
std::shared_ptr<FormItem> 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<QUuid, std::shared_ptr<FormItem>>::iterator formItemsIterator = mFormItems.begin();
while ( formItemsIterator != mFormItems.end() )
{
std::shared_ptr<FormItem> 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;
Expand Down
36 changes: 34 additions & 2 deletions app/attributes/attributedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 )
Expand All @@ -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,
Expand All @@ -63,8 +67,10 @@ FormItem *FormItem::createFieldItem( const QUuid &id,
parentTabId,
FormItem::Field,
name,
nameExpression,
showName,
isEditable,
editableExpression,
editorWidgetSetup,
fieldIndex,
visibilityExpression,
Expand All @@ -88,8 +94,10 @@ FormItem *FormItem::createRelationItem( const QUuid &id,
parentTabId,
FormItem::Relation,
name,
QgsExpression(),
showName,
true,
QgsExpression(),
QgsEditorWidgetSetup(),
-1,
visibilityExpression,
Expand Down Expand Up @@ -119,8 +127,10 @@ FormItem *FormItem::createSpacerItem(
parentTabId,
FormItem::Spacer,
name,
QgsExpression(),
false, // label is never shown for spacer
false,
QgsExpression(),
config,
-1,
visibilityExpression,
Expand Down Expand Up @@ -153,8 +163,10 @@ FormItem *FormItem::createRichTextItem(
parentTabId,
FormItem::RichText,
name,
QgsExpression(),
showName,
false,
QgsExpression(),
config,
-1,
visibilityExpression,
Expand All @@ -175,6 +187,11 @@ QString FormItem::name() const
return mName;
}

void FormItem::setName( QString name )
{
mName = name;
}

bool FormItem::isEditable() const
{
return mIsEditable;
Expand Down Expand Up @@ -228,6 +245,11 @@ void FormItem::setVisible( bool visible )
mVisible = visible;
}

void FormItem::setIsEditable( bool editable )
{
mIsEditable = editable;
}

QUuid FormItem::id() const
{
return mId;
Expand All @@ -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;
Expand Down
19 changes: 16 additions & 3 deletions app/attributes/attributedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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 );

Expand All @@ -129,6 +138,8 @@ class FormItem
int parentTabId() const;

QgsExpression visibilityExpression() const;
QgsExpression nameExpression() const;
QgsExpression editableExpression() const;

bool visible() const;

Expand All @@ -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;
Expand Down

1 comment on commit df0d199

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 25.1.698311 just submitted!

Please sign in to comment.