[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.
This commit is contained in:
Matthias Kuhn 2017-09-26 10:33:56 +02:00
parent 5ad5930595
commit beddd25074
2 changed files with 60 additions and 2 deletions

View File

@ -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"},
]
}

View File

@ -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<QgsExpressionFunction *> &QgsExpression::Functions()
QString(),
false,
QSet<QString>()
)
);
// **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" ) )