Skip to content

Commit

Permalink
add map_layers variable for MapLayoutItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and nyalldawson committed Aug 21, 2018
1 parent b6251d2 commit 5bc848c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgslayoutitem.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit 5bc848c

Please sign in to comment.