mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-15 00:07:25 -05:00
formatted_attributes() expression function
Funded by: Kanton Solothurn
This commit is contained in:
parent
c1dd106d77
commit
c9c5896a77
15
resources/function_help/json/formatted_attributes
Normal file
15
resources/function_help/json/formatted_attributes
Normal file
@ -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."}] }
|
||||
]
|
||||
}
|
||||
@ -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;
|
||||
@ -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" ),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user