Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[QgsQuick] Updates default values when an attribute value is updated
  • Loading branch information
vsklencar authored and nyalldawson committed Apr 9, 2021
1 parent 66e9e6f commit 4e688c6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/quickgui/attributes/qgsquickattributemodel.cpp
Expand Up @@ -335,6 +335,56 @@ void QgsQuickAttributeModel::resetAttributes()
endResetModel();
}

void QgsQuickAttributeModel::updateDefaultValuesAttributes( const QgsField &editedField )
{
if ( !mFeatureLayerPair.layer() )
return;

QgsExpressionContext expressionContext = mFeatureLayerPair.layer()->createExpressionContext();
expressionContext.setFeature( mFeatureLayerPair.feature() );
QgsFields fields = mFeatureLayerPair.layer()->fields();

beginResetModel();
for ( int i = 0; i < fields.count(); ++i )
{
QgsDefaultValue defaultDefinition = fields.at( i ).defaultValueDefinition();
if ( !defaultDefinition.expression().isEmpty() && defaultDefinition.applyOnUpdate() )
{
// Skip evaluation for a given (last edited ) field to have same behaviour as it is in QGIS
// This allows to edit value, but eventually it will be overwritten by "on update" default value if defined
// when all attributes are saved and form is closed (as in QGIS)
if ( editedField.name() == fields.at( i ).name() )
continue;

QgsExpression exp( fields.at( i ).defaultValueDefinition().expression() );
exp.prepare( &expressionContext );
if ( exp.hasParserError() )
QgsMessageLog::logMessage( tr( "Default value expression for %1:%2 has parser error: %3" ).arg(
mFeatureLayerPair.layer()->name(),
fields.at( i ).name(),
exp.parserErrorString() ),
QStringLiteral( "QgsQuick" ),
Qgis::Warning );

QVariant value = exp.evaluate( &expressionContext );

if ( exp.hasEvalError() )
QgsMessageLog::logMessage( tr( "Default value expression for %1:%2 has evaluation error: %3" ).arg(
mFeatureLayerPair.layer()->name(),
fields.at( i ).name(),
exp.evalErrorString() ),
QStringLiteral( "QgsQuick" ),
Qgis::Warning );
else
{
QModelIndex index = this->index( i );
setData( index, value, AttributeValue );
}
}
}
endResetModel();
}

void QgsQuickAttributeModel::create()
{
if ( !mFeatureLayerPair.layer() )
Expand Down
7 changes: 7 additions & 0 deletions src/quickgui/attributes/qgsquickattributemodel.h
Expand Up @@ -122,6 +122,13 @@ class QUICK_EXPORT QgsQuickAttributeModel : public QAbstractListModel
//! Resets remembered attributes
Q_INVOKABLE virtual void resetAttributes();

/**
* Updates attributes according their default value definition.
* Only for attributes with defined default value definition and flag `applyOnUpdate`.
* @param editedField QgsField reference of last edited field which triggers update attributes.
*/
Q_INVOKABLE void updateDefaultValuesAttributes( const QgsField &editedField );

//! Gives information whether field with given index is remembered or not
bool isFieldRemembered( const int fieldIndex ) const;

Expand Down
5 changes: 5 additions & 0 deletions src/quickgui/plugin/qgsquickfeatureform.qml
Expand Up @@ -488,7 +488,12 @@ Item {
Connections {
target: attributeEditorLoader.item
onValueChanged: {
var valueChanged = value != AttributeValue
AttributeValue = isNull ? undefined : value
// updates other attributes if a user males a change
if (valueChanged) {
form.model.attributeModel.updateDefaultValuesAttributes(Field)
}
}
}

Expand Down

0 comments on commit 4e688c6

Please sign in to comment.