Skip to content
Permalink
Browse files
Invert args
  • Loading branch information
elpaso committed Dec 17, 2021
1 parent e2266aa commit 40947d42660339a9f3b87cd03e57facdbcaa459d
Showing with 30 additions and 34 deletions.
  1. +3 −3 resources/function_help/json/represent_attributes
  2. +25 −29 src/core/expression/qgsexpressionfunction.cpp
  3. +2 −2 tests/src/core/testqgsexpression.cpp
@@ -18,9 +18,9 @@
},
{
"variant": "Layer and feature parameters",
"variant_description": "If called with a 'feature' and a 'layer' parameter, the function will return the representation of the attributes of the specified feature from the specified layer.",
"arguments": [ { "arg": "feature", "description": "The feature which should be evaluated." }, { "arg": "layer", "description": "The layer (or its ID or name)." } ],
"examples": [ { "expression": "represent_attributes(@atlas_feature, 'atlas_layer')", "returns" : "The representation of the attributes for the specified feature from the specified layer." } ]
"variant_description": "If called with a 'layer' and a 'feature' parameter, the function will return the representation of the attributes of the specified feature from the specified layer.",
"arguments": [ { "arg": "layer", "description": "The layer (or its ID or name)." }, { "arg": "feature", "description": "The feature which should be evaluated." } ],
"examples": [ { "expression": "represent_attributes('atlas_layer', @atlas_feature)", "returns" : "The representation of the attributes for the specified feature from the specified layer." } ]
}
]
}
@@ -1732,35 +1732,25 @@ static QVariant fcnRepresentAttributes( const QVariantList &values, const QgsExp
QgsVectorLayer *layer = nullptr;
QgsFeature feature;

if ( values.size() == 2 )
if ( values.isEmpty() )
{
if ( ! values.at( 0 ).isNull() )
{
feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
}
else if ( context && context->hasFeature() )
{
feature = context->feature();
}
else
{
parent->setEvalErrorString( QObject::tr( "Cannot use represent attributes function in this context: feature is not set" ) );
return QVariant();
}

if ( ! values.at( 1 ).isNull() )
{
layer = QgsExpressionUtils::getVectorLayer( values.at( 1 ), parent );
}
else if ( context )
{
layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );
}
else
{
parent->setEvalErrorString( QObject::tr( "Cannot use represent attributes function: layer is not set" ) );
return QVariant();
}
feature = context->feature();
layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );
}
else if ( values.size() == 1 )
{
layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );
feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
}
else if ( values.size() == 2 )
{
layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
feature = QgsExpressionUtils::getFeature( values.at( 1 ), parent );
}
else
{
parent->setEvalErrorString( QObject::tr( "Function `represent_attributes` requires no more than two parameters. %1 given." ).arg( values.length() ) );
return QVariant();
}

if ( !layer )
@@ -1769,6 +1759,12 @@ static QVariant fcnRepresentAttributes( const QVariantList &values, const QgsExp
return QVariant();
}

if ( !feature.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot use represent attributes function: feature could not be resolved." ) );
return QVariant();
}

const QgsFields fields = feature.fields();
QVariantMap result;
for ( int fieldIndex = 0; fieldIndex < fields.count(); ++fieldIndex )
@@ -7754,7 +7750,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
fcnAttributes, QStringLiteral( "Record and Attributes" ), QString(), false, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES );
attributesFunc->setIsStatic( false );
functions << attributesFunc;
QgsStaticExpressionFunction *representAttributesFunc = new QgsStaticExpressionFunction( QStringLiteral( "represent_attributes" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "feature" ), true ) << QgsExpressionFunction::Parameter( QStringLiteral( "layer" ), true ),
QgsStaticExpressionFunction *representAttributesFunc = new QgsStaticExpressionFunction( QStringLiteral( "represent_attributes" ), -1,
fcnRepresentAttributes, QStringLiteral( "Record and Attributes" ), QString(), false, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES );
representAttributesFunc->setIsStatic( false );
functions << representAttributesFunc;
@@ -566,7 +566,7 @@ class TestQgsExpression: public QObject
QgsProject::instance()->addMapLayer( &layer, false, false );
QgsExpressionContext context3;
context3.setFeature( f2 );
expression = QgsExpression( "represent_attributes($currentfeature, 'test_represent_attributes')" );
expression = QgsExpression( "represent_attributes('test_represent_attributes', $currentfeature)" );

result = expression.evaluate( &context3 ).toMap();

@@ -580,7 +580,7 @@ class TestQgsExpression: public QObject

// Test errors
QgsProject::instance()->removeMapLayer( layer.id() );
expression = QgsExpression( "represent_attributes($currentfeature, 'test_represent_attributes')" );
expression = QgsExpression( "represent_attributes('test_represent_attributes', $currentfeature)" );
QgsExpressionContext context4;
result = expression.evaluate( &context4 ).toMap();
QVERIFY( expression.hasEvalError() );

0 comments on commit 40947d4

Please sign in to comment.