Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for editable states and alias expressions for fields #3645

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions app/attributes/attributecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@ 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 = editableProperty.expressionString();
QgsExpression editableExpression( editableExpressionString );

if ( !expression.isEmpty() )
{
expressions << field.constraints().constraintExpression();
Expand All @@ -249,8 +263,10 @@ void AttributeController::flatten(
groupName,
parentTabRow,
layer->attributeDisplayName( fieldIndex ),
nameExpression,
editorField->showLabel(),
!isReadOnly,
editableExpression,
tomasMizera marked this conversation as resolved.
Show resolved Hide resolved
VitorVieiraZ marked this conversation as resolved.
Show resolved Hide resolved
getEditorWidgetSetup( layer, fieldIndex ),
fieldIndex,
parentVisibilityExpressions // field doesn't have visibility expression itself
Expand Down Expand Up @@ -904,6 +920,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
tomasMizera marked this conversation as resolved.
Show resolved Hide resolved
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;
VitorVieiraZ marked this conversation as resolved.
Show resolved Hide resolved

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
Loading