mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-03 00:02:25 -05:00
add map_layers variable for MapLayoutItem
This commit is contained in:
parent
b6251d235f
commit
5bc848c057
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user