Skip to content

Commit 820c40a

Browse files
committed
[FEATURE] map_layers variable for layout map items
Just like map_layers_ids, but returns direct layer values instead of ids
1 parent 367d0be commit 820c40a

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/core/layout/qgslayoutitemmap.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,18 +1155,27 @@ 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 ) );
1158+
QVariantList layersIds;
1159+
QVariantList layers;
1160+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers_ids" ), layersIds, true ) );
1161+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers" ), layers, true ) );
11601162

11611163
context.appendScope( scope );
11621164

1163-
// The scope map_layers_ids has been added to the context, only now we can call layersToRender
1165+
// The scope map_layers_ids and map_layers variables have been added to the context, only now we can
1166+
// call layersToRender (just in case layersToRender relies on evaluating an expression which uses
1167+
// other variables contained within the map settings scope
11641168
const QList<QgsMapLayer *> layersInMap = layersToRender( &context );
1169+
1170+
layersIds.reserve( layersInMap.count() );
1171+
layers.reserve( layersInMap.count() );
11651172
for ( QgsMapLayer *layer : layersInMap )
11661173
{
1167-
layers_ids << layer->id();
1174+
layersIds << layer->id();
1175+
layers << QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( layer ) );
11681176
}
1169-
scope->setVariable( QStringLiteral( "map_layers_ids" ), layers_ids );
1177+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers_ids" ), layersIds, true ) );
1178+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers" ), layers, true ) );
11701179

11711180
return context;
11721181
}

src/core/qgsexpressioncontext.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,13 +995,18 @@ 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;
998+
QVariantList layersIds;
999+
QVariantList layers;
9991000
const QList<QgsMapLayer *> layersInMap = mapSettings.layers();
1001+
layersIds.reserve( layersInMap.count() );
1002+
layers.reserve( layersInMap.count() );
10001003
for ( QgsMapLayer *layer : layersInMap )
10011004
{
1002-
layers_ids << layer->id();
1005+
layersIds << layer->id();
1006+
layers << QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( layer ) );
10031007
}
1004-
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers_ids" ), layers_ids, true ) );
1008+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers_ids" ), layersIds, true ) );
1009+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers" ), layers, true ) );
10051010

10061011
scope->addFunction( QStringLiteral( "is_layer_visible" ), new GetLayerVisibility( mapSettings.layers() ) );
10071012

tests/src/core/testqgslayoutitem.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,11 +1432,15 @@ void TestQgsLayoutItem::itemVariablesFunction()
14321432
r = e4.evaluate( &c );
14331433
QCOMPARE( r.toString(), QString( "degrees" ) );
14341434

1435-
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
1436-
map->setLayers( QList<QgsMapLayer *>() << layer );
1435+
std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique< QgsVectorLayer >( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
1436+
std::unique_ptr< QgsVectorLayer > layer2 = qgis::make_unique< QgsVectorLayer >( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "B" ), QStringLiteral( "memory" ) );
1437+
map->setLayers( QList<QgsMapLayer *>() << layer.get() << layer2.get() );
14371438
QgsExpression e5( QStringLiteral( "map_get( item_variables( 'Map_id' ), 'map_layers_ids' )" ) );
14381439
r = e5.evaluate( &c );
1439-
QCOMPARE( r.toStringList().join( ',' ), layer->id() );
1440+
QCOMPARE( r.toStringList().join( ',' ), QStringLiteral( "%1,%2" ).arg( layer->id(), layer2->id() ) );
1441+
e5 = QgsExpression( QStringLiteral( "array_foreach(map_get( item_variables( 'Map_id' ), 'map_layers' ), layer_property(@element, 'name'))" ) );
1442+
r = e5.evaluate( &c );
1443+
QCOMPARE( r.toStringList().join( ',' ), QStringLiteral( "A,B" ) );
14401444
}
14411445

14421446
void TestQgsLayoutItem::variables()

0 commit comments

Comments
 (0)