add map_layers variable for MapLayoutItem

This commit is contained in:
Etienne Trimaille 2018-08-20 20:50:59 -04:00 committed by Nyall Dawson
parent b6251d235f
commit 5bc848c057
3 changed files with 25 additions and 0 deletions

View File

@ -1155,8 +1155,19 @@ QgsExpressionContext QgsLayoutItemMap::createExpressionContext() const
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapCrs.toProj4(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapCrs.mapUnits() ), true ) );
QVariantList layers_ids;
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers_ids" ), layers_ids, true ) );
context.appendScope( scope );
// The scope map_layers_ids has been added to the context, only now we can call layersToRender
const QList<QgsMapLayer *> layersInMap = layersToRender( &context );
for ( QgsMapLayer *layer : layersInMap )
{
layers_ids << layer->id();
}
scope->setVariable( QStringLiteral( "map_layers_ids" ), layers_ids );
return context;
}

View File

@ -995,6 +995,14 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapSettingsScope( const Qg
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapSettings.destinationCrs().toProj4(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapSettings.mapUnits() ), true ) );
QVariantList layers_ids;
const QList<QgsMapLayer *> layersInMap = mapSettings.layers();
for ( QgsMapLayer *layer : layersInMap )
{
layers_ids << layer->id();
}
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers_ids" ), layers_ids, true ) );
scope->addFunction( QStringLiteral( "is_layer_visible" ), new GetLayerVisibility( mapSettings.layers() ) );
return scope;

View File

@ -1431,6 +1431,12 @@ void TestQgsLayoutItem::itemVariablesFunction()
QgsExpression e4( QStringLiteral( "map_get( item_variables( 'Map_id' ), 'map_units' )" ) );
r = e4.evaluate( &c );
QCOMPARE( r.toString(), QString( "degrees" ) );
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
map->setLayers( QList<QgsMapLayer *>() << layer );
QgsExpression e5( QStringLiteral( "map_get( item_variables( 'Map_id' ), 'map_layers_ids' )" ) );
r = e5.evaluate( &c );
QCOMPARE( r.toStringList().join( ',' ), layer->id() );
}
void TestQgsLayoutItem::variables()