Invert args

This commit is contained in:
Alessandro Pasotti 2021-12-17 15:16:48 +01:00
parent e2266aafe0
commit 40947d4266
3 changed files with 30 additions and 34 deletions

View File

@ -18,9 +18,9 @@
}, },
{ {
"variant": "Layer and feature parameters", "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.", "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": "feature", "description": "The feature which should be evaluated." }, { "arg": "layer", "description": "The layer (or its ID or name)." } ], "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_feature, 'atlas_layer')", "returns" : "The representation of the attributes for the specified feature from the specified layer." } ] "examples": [ { "expression": "represent_attributes('atlas_layer', @atlas_feature)", "returns" : "The representation of the attributes for the specified feature from the specified layer." } ]
} }
] ]
} }

View File

@ -1732,35 +1732,25 @@ static QVariant fcnRepresentAttributes( const QVariantList &values, const QgsExp
QgsVectorLayer *layer = nullptr; QgsVectorLayer *layer = nullptr;
QgsFeature feature; QgsFeature feature;
if ( values.size() == 2 ) if ( values.isEmpty() )
{ {
if ( ! values.at( 0 ).isNull() ) feature = context->feature();
{ layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );
feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent ); }
} else if ( values.size() == 1 )
else if ( context && context->hasFeature() ) {
{ layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );
feature = context->feature(); feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
} }
else else if ( values.size() == 2 )
{ {
parent->setEvalErrorString( QObject::tr( "Cannot use represent attributes function in this context: feature is not set" ) ); layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
return QVariant(); feature = QgsExpressionUtils::getFeature( values.at( 1 ), parent );
} }
else
if ( ! values.at( 1 ).isNull() ) {
{ parent->setEvalErrorString( QObject::tr( "Function `represent_attributes` requires no more than two parameters. %1 given." ).arg( values.length() ) );
layer = QgsExpressionUtils::getVectorLayer( values.at( 1 ), parent ); return QVariant();
}
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();
}
} }
if ( !layer ) if ( !layer )
@ -1769,6 +1759,12 @@ static QVariant fcnRepresentAttributes( const QVariantList &values, const QgsExp
return QVariant(); 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(); const QgsFields fields = feature.fields();
QVariantMap result; QVariantMap result;
for ( int fieldIndex = 0; fieldIndex < fields.count(); ++fieldIndex ) 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 ); fcnAttributes, QStringLiteral( "Record and Attributes" ), QString(), false, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES );
attributesFunc->setIsStatic( false ); attributesFunc->setIsStatic( false );
functions << attributesFunc; 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 ); fcnRepresentAttributes, QStringLiteral( "Record and Attributes" ), QString(), false, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES );
representAttributesFunc->setIsStatic( false ); representAttributesFunc->setIsStatic( false );
functions << representAttributesFunc; functions << representAttributesFunc;

View File

@ -566,7 +566,7 @@ class TestQgsExpression: public QObject
QgsProject::instance()->addMapLayer( &layer, false, false ); QgsProject::instance()->addMapLayer( &layer, false, false );
QgsExpressionContext context3; QgsExpressionContext context3;
context3.setFeature( f2 ); context3.setFeature( f2 );
expression = QgsExpression( "represent_attributes($currentfeature, 'test_represent_attributes')" ); expression = QgsExpression( "represent_attributes('test_represent_attributes', $currentfeature)" );
result = expression.evaluate( &context3 ).toMap(); result = expression.evaluate( &context3 ).toMap();
@ -580,7 +580,7 @@ class TestQgsExpression: public QObject
// Test errors // Test errors
QgsProject::instance()->removeMapLayer( layer.id() ); QgsProject::instance()->removeMapLayer( layer.id() );
expression = QgsExpression( "represent_attributes($currentfeature, 'test_represent_attributes')" ); expression = QgsExpression( "represent_attributes('test_represent_attributes', $currentfeature)" );
QgsExpressionContext context4; QgsExpressionContext context4;
result = expression.evaluate( &context4 ).toMap(); result = expression.evaluate( &context4 ).toMap();
QVERIFY( expression.hasEvalError() ); QVERIFY( expression.hasEvalError() );