Skip to content

Commit

Permalink
formatted_attributes() expression function
Browse files Browse the repository at this point in the history
Funded by: Kanton Solothurn
  • Loading branch information
elpaso committed Nov 25, 2021
1 parent c1dd106 commit c9c5896
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions resources/function_help/json/formatted_attributes
@@ -0,0 +1,15 @@
{
"name": "formatted_attributes",
"type": "function",
"groups": ["Record and Attributes"],
"description": "Returns a map containing all attributes from a feature, with field names as map keys and formatted attributes as string values. Attributes are formatted as defined in the 'Attributes Form' section of the layer properties.",
"variants": [
{ "variant": "Variant 1",
"variant_description": "Returns a map of all attributes from the current feature formatted as defined in the 'Attributes Form' section of the layer properties..",
"examples": [ { "expression":"formatted_attributes()['type']", "returns":"value stored in 'type' attribute for the current feature formatted as defined in the 'Attributes Form' section of the layer properties."}] },
{ "variant": "Variant 2",
"variant_description": "Allows the target feature and the layer to be specified.",
"arguments": [ {"arg":"feature","description":"a feature"}, {"arg":"layer","description":"a vector layer"}],
"examples": [ { "expression":"formatted_attributes( feature, 'regions' )['name']", "returns":"value stored in 'name' attribute for the 'feature' belonging to layer 'regions', formatted as defined in the 'Attributes Form' section of the layer properties."}] }
]
}
49 changes: 49 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -1727,6 +1727,51 @@ static QVariant fcnAttributes( const QVariantList &values, const QgsExpressionCo
return result;
}

static QVariant fcnFormattedAttributes( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsVectorLayer *vl = nullptr;
QgsFeature feature;
if ( values.size() == 0 || values.at( 0 ).isNull() )
{
feature = context->feature();
// first step - find current layer
vl = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );
}
else if ( values.size() == 2 && ! values.at( 1 ).isNull() )
{
feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
vl = QgsExpressionUtils::getVectorLayer( values.at( 1 ), parent );
}
else
{
parent->setEvalErrorString( QObject::tr( "Layer is not set" ) );
return QVariant();
}

if ( !vl )
{
parent->setEvalErrorString( QObject::tr( "Cannot use formatted attributes function in this context" ) );
return QVariant();
}

const QgsFields fields = feature.fields();
QVariantMap result;
for ( int idx = 0; idx < fields.count(); ++idx )
{
QVariant attributeVal { feature.attribute( idx ) };
const QgsEditorWidgetSetup setup = vl->editorWidgetSetup( idx );
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
QString value( fieldFormatter->representValue( vl, idx, setup.config(), QVariant(), attributeVal ) );

if ( setup.config().value( QStringLiteral( "AllowMulti" ) ).toBool() && value.startsWith( QLatin1Char( '{' ) ) && value.endsWith( QLatin1Char( '}' ) ) )
{
value = value.mid( 1, value.size() - 2 );
}
result.insert( fields.at( idx ).name(), value );
}
return result;
}

static QVariant fcnCoreFeatureMaptipDisplay( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const bool isMaptip )
{
QgsVectorLayer *layer = nullptr;
Expand Down Expand Up @@ -7663,6 +7708,10 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
fcnAttributes, QStringLiteral( "Record and Attributes" ), QString(), false, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES );
attributesFunc->setIsStatic( false );
functions << attributesFunc;
QgsStaticExpressionFunction *formattedAttributesFunc = new QgsStaticExpressionFunction( QStringLiteral( "formatted_attributes" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "feature" ), true ) << QgsExpressionFunction::Parameter( QStringLiteral( "layer" ), true ),
fcnFormattedAttributes, QStringLiteral( "Record and Attributes" ), QString(), false, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES );
formattedAttributesFunc->setIsStatic( false );
functions << formattedAttributesFunc;

QgsStaticExpressionFunction *maptipFunc = new QgsStaticExpressionFunction(
QStringLiteral( "maptip" ),
Expand Down

0 comments on commit c9c5896

Please sign in to comment.