From beddd2507465e67119a02df5bd32fc6c38f77581 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Tue, 26 Sep 2017 10:33:56 +0200 Subject: [PATCH] [FEATURE] Expression function represent_value This will lookup the representation value given the widget configuration. This is helpful to get nicely formatted messages for value maps, value relations and others in expressions. --- resources/function_help/json/represent_value | 12 +++++ src/core/expression/qgsexpressionfunction.cpp | 50 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 resources/function_help/json/represent_value diff --git a/resources/function_help/json/represent_value b/resources/function_help/json/represent_value new file mode 100644 index 000000000000..e559d950d922 --- /dev/null +++ b/resources/function_help/json/represent_value @@ -0,0 +1,12 @@ +{ + "name": "represent_value", + "type": "function", + "description": "Returns the configured representation value for a field value. It depends on the configured widget type. Often, this is useful for 'Value Map' widgets.", + "arguments": [ + {"arg":"fieldName", "description": "The field name for which the widget configuration should be loaded."}, + {"arg":"value", "description": "The value which should be resolved. Most likely a field." }, + ], + "examples": [ + { "expression":"represent_value('field_with_value_map', \"field_with_value_map\")", "returns":"Description for value"}, + ] +} diff --git a/src/core/expression/qgsexpressionfunction.cpp b/src/core/expression/qgsexpressionfunction.cpp index c8781f16fddd..9b2647cc55c6 100644 --- a/src/core/expression/qgsexpressionfunction.cpp +++ b/src/core/expression/qgsexpressionfunction.cpp @@ -41,6 +41,8 @@ #include "qgsvectorlayer.h" #include "qgsrasterbandstats.h" #include "qgscolorramp.h" +#include "qgsfieldformatterregistry.h" +#include "qgsfieldformatter.h" const QString QgsExpressionFunction::helpText() const { @@ -3428,6 +3430,46 @@ static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionCo return QVariant(); } +static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent ) +{ + QVariant result; + QString fieldName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent ); + QVariant value = values.at( 1 ); + + QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( context->variable( "layer" ), parent ); + + if ( layer ) + { + const QgsFields fields = layer->fields(); + int index = fields.lookupField( fieldName ); + + if ( index == -1 ) + { + parent->setEvalErrorString( QStringLiteral( "%1: Field not found %2" ).arg( QStringLiteral( "represent_value" ), fieldName ) ); + } + else + { + QgsEditorWidgetSetup setup = layer->editorWidgetSetup( index ); + QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() ); + + QString cacheKey = QStringLiteral( "repvalfcn:%1:%2" ).arg( layer->id(), fieldName ); + + QVariant cache; + if ( !context->hasCachedValue( cacheKey ) ) + { + cache = formatter->createCache( layer, index, setup.config() ); + context->setCachedValue( cacheKey, cache ); + } + else + cache = context->cachedValue( cacheKey ); + + result = formatter->representValue( layer, index, setup.config(), cache, value ); + } + } + + return result; +} + static QVariant fcnGetLayerProperty( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent ) { QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent ); @@ -4280,10 +4322,14 @@ const QList &QgsExpression::Functions() QString(), false, QSet() - ) + ); - // **General** functions + // **Fields and Values** functions + sFunctions + << new QgsStaticExpressionFunction( QStringLiteral( "represent_value" ), 2, fcnRepresentValue, QStringLiteral( "Fields and Values" ) ); + // **General** functions + sFunctions << new QgsStaticExpressionFunction( QStringLiteral( "layer_property" ), 2, fcnGetLayerProperty, QStringLiteral( "General" ) ) << new QgsStaticExpressionFunction( QStringLiteral( "raster_statistic" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "band" ) )