QgsExpressionContext QgsExpressionContextUtils::createFeatureBasedContext( const QgsFeature &feature, const QgsFields &fields ) { QgsExpressionContextScope* scope = new QgsExpressionContextScope(); scope->setFeature( feature ); scope->setFields( fields ); return QgsExpressionContext() << scope; }
QgsExpressionContextScope* QgsExpressionContextUtils::atlasScope( const QgsAtlasComposition* atlas ) { QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Atlas" ) ); if ( !atlas ) { //add some dummy atlas variables. This is done so that as in certain contexts we want to show //users that these variables are available even if they have no current value scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_pagename", QString(), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_feature", QVariant::fromValue( QgsFeature() ), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", 0, true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_geometry", QVariant::fromValue( QgsGeometry() ), true ) ); return scope; } //add known atlas variables scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_totalfeatures", atlas->numFeatures(), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featurenumber", atlas->currentFeatureNumber() + 1, true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_filename", atlas->currentFilename(), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_pagename", atlas->currentPageName(), true ) ); if ( atlas->enabled() && atlas->coverageLayer() ) { scope->setFields( atlas->coverageLayer()->fields() ); } if ( atlas->enabled() ) { QgsFeature atlasFeature = atlas->feature(); scope->setFeature( atlasFeature ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_feature", QVariant::fromValue( atlasFeature ), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", atlasFeature.id(), true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_geometry", QVariant::fromValue( *atlasFeature.constGeometry() ), true ) ); } return scope; }