Skip to content

Commit 5bc848c

Browse files
Gustrynyalldawson
authored andcommittedAug 21, 2018
add map_layers variable for MapLayoutItem
1 parent b6251d2 commit 5bc848c

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed
 

‎src/core/layout/qgslayoutitemmap.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,8 +1155,19 @@ QgsExpressionContext QgsLayoutItemMap::createExpressionContext() const
11551155
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapCrs.toProj4(), true ) );
11561156
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapCrs.mapUnits() ), true ) );
11571157

1158+
QVariantList layers_ids;
1159+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers_ids" ), layers_ids, true ) );
1160+
11581161
context.appendScope( scope );
11591162

1163+
// The scope map_layers_ids has been added to the context, only now we can call layersToRender
1164+
const QList<QgsMapLayer *> layersInMap = layersToRender( &context );
1165+
for ( QgsMapLayer *layer : layersInMap )
1166+
{
1167+
layers_ids << layer->id();
1168+
}
1169+
scope->setVariable( QStringLiteral( "map_layers_ids" ), layers_ids );
1170+
11601171
return context;
11611172
}
11621173

‎src/core/qgsexpressioncontext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,14 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapSettingsScope( const Qg
995995
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapSettings.destinationCrs().toProj4(), true ) );
996996
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapSettings.mapUnits() ), true ) );
997997

998+
QVariantList layers_ids;
999+
const QList<QgsMapLayer *> layersInMap = mapSettings.layers();
1000+
for ( QgsMapLayer *layer : layersInMap )
1001+
{
1002+
layers_ids << layer->id();
1003+
}
1004+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers_ids" ), layers_ids, true ) );
1005+
9981006
scope->addFunction( QStringLiteral( "is_layer_visible" ), new GetLayerVisibility( mapSettings.layers() ) );
9991007

10001008
return scope;

‎tests/src/core/testqgslayoutitem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,12 @@ void TestQgsLayoutItem::itemVariablesFunction()
14311431
QgsExpression e4( QStringLiteral( "map_get( item_variables( 'Map_id' ), 'map_units' )" ) );
14321432
r = e4.evaluate( &c );
14331433
QCOMPARE( r.toString(), QString( "degrees" ) );
1434+
1435+
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
1436+
map->setLayers( QList<QgsMapLayer *>() << layer );
1437+
QgsExpression e5( QStringLiteral( "map_get( item_variables( 'Map_id' ), 'map_layers_ids' )" ) );
1438+
r = e5.evaluate( &c );
1439+
QCOMPARE( r.toStringList().join( ',' ), layer->id() );
14341440
}
14351441

14361442
void TestQgsLayoutItem::variables()

0 commit comments

Comments
 (0)
Please sign in to comment.