Skip to content

Commit

Permalink
[FEATURE] Expression function represent_value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m-kuhn committed Sep 26, 2017
1 parent 5ad5930 commit beddd25
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
12 changes: 12 additions & 0 deletions 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"},
]
}
50 changes: 48 additions & 2 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -41,6 +41,8 @@
#include "qgsvectorlayer.h"
#include "qgsrasterbandstats.h"
#include "qgscolorramp.h"
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"

const QString QgsExpressionFunction::helpText() const
{
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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" ) )
Expand Down

0 comments on commit beddd25

Please sign in to comment.