Skip to content
Permalink
Browse files

add map_layers variable for MapLayoutItem

  • Loading branch information
Gustry authored and nyalldawson committed Aug 21, 2018
1 parent b6251d2 commit 5bc848c05746524414575cdcf1637009878b6dcc
Showing with 25 additions and 0 deletions.
  1. +11 −0 src/core/layout/qgslayoutitemmap.cpp
  2. +8 −0 src/core/qgsexpressioncontext.cpp
  3. +6 −0 tests/src/core/testqgslayoutitem.cpp
@@ -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;
}

@@ -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;
@@ -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()

0 comments on commit 5bc848c

Please sign in to comment.
You can’t perform that action at this time.