diff --git a/python/core/composer/qgscomposermap.sip b/python/core/composer/qgscomposermap.sip index 4b5b4460fcf..c471917b7bf 100644 --- a/python/core/composer/qgscomposermap.sip +++ b/python/core/composer/qgscomposermap.sip @@ -746,6 +746,8 @@ class QgsComposerMap : QgsComposerItem * @note added in 2.6 */ void requestedExtent( QgsRectangle& extent ) const; + virtual QgsExpressionContext* createExpressionContext() const; + signals: void extentChanged(); diff --git a/python/core/qgsexpressioncontext.sip b/python/core/qgsexpressioncontext.sip index 50ddfa34885..7282e94aa97 100644 --- a/python/core/qgsexpressioncontext.sip +++ b/python/core/qgsexpressioncontext.sip @@ -460,6 +460,11 @@ class QgsExpressionContextUtils */ static void setLayerVariables( QgsMapLayer* layer, const QgsStringMap variables ); + /** Creates a new scope which contains variables and functions relating to a QgsMapSettings object. + * For instance, map scale and rotation. + */ + static QgsExpressionContextScope* mapSettingsScope( const QgsMapSettings &mapSettings ) /Factory/; + /** Creates a new scope which contains variables and functions relating to a QgsComposition. * For instance, number of pages and page sizes. * @param composition source composition diff --git a/src/core/composer/qgscomposermap.cpp b/src/core/composer/qgscomposermap.cpp index 2f0ecb4c6d1..1d7ab700130 100644 --- a/src/core/composer/qgscomposermap.cpp +++ b/src/core/composer/qgscomposermap.cpp @@ -2120,6 +2120,22 @@ void QgsComposerMap::requestedExtent( QgsRectangle& extent ) const } } +QgsExpressionContext* QgsComposerMap::createExpressionContext() const +{ + QgsExpressionContext* context = QgsComposerItem::createExpressionContext(); + + //Can't utilise QgsExpressionContextUtils::mapSettingsScope as we don't always + //have a QgsMapSettings object available when the context is required, so we manually + //add the same variables here + QgsExpressionContextScope* scope = new QgsExpressionContextScope( tr( "Map Settings" ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_id", QgsComposerItem::id(), true ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_rotation", mMapRotation, true ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_scale", scale(), true ) ); + context->appendScope( scope ); + + return context; +} + double QgsComposerMap::mapUnitsToMM() const { double extentWidth = currentMapExtent()->width(); diff --git a/src/core/composer/qgscomposermap.h b/src/core/composer/qgscomposermap.h index ca6e8515641..78b64dad634 100644 --- a/src/core/composer/qgscomposermap.h +++ b/src/core/composer/qgscomposermap.h @@ -785,6 +785,8 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem * @note added in 2.6 */ void requestedExtent( QgsRectangle& extent ) const; + virtual QgsExpressionContext* createExpressionContext() const override; + signals: void extentChanged(); diff --git a/src/core/qgsexpressioncontext.cpp b/src/core/qgsexpressioncontext.cpp index d22be886544..3a9edef6977 100644 --- a/src/core/qgsexpressioncontext.cpp +++ b/src/core/qgsexpressioncontext.cpp @@ -631,6 +631,17 @@ void QgsExpressionContextUtils::setLayerVariables( QgsMapLayer* layer, const Qgs layer->setCustomProperty( "variableValues", variableValues ); } +QgsExpressionContextScope* QgsExpressionContextUtils::mapSettingsScope( const QgsMapSettings& mapSettings ) +{ + QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) ); + + //add known map settings context variables + scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_rotation", mapSettings.rotation(), true ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_scale", mapSettings.scale(), true ) ); + + return scope; +} + QgsExpressionContextScope *QgsExpressionContextUtils::compositionScope( const QgsComposition *composition ) { QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composition" ) ); diff --git a/src/core/qgsexpressioncontext.h b/src/core/qgsexpressioncontext.h index 48be6a3ab21..68bb2578900 100644 --- a/src/core/qgsexpressioncontext.h +++ b/src/core/qgsexpressioncontext.h @@ -27,6 +27,7 @@ class QgsMapLayer; class QgsComposition; class QgsComposerItem; class QgsAtlasComposition; +class QgsMapSettings; /** \ingroup core * \class QgsScopedExpressionFunction @@ -491,6 +492,11 @@ class CORE_EXPORT QgsExpressionContextUtils */ static void setLayerVariables( QgsMapLayer* layer, const QgsStringMap variables ); + /** Creates a new scope which contains variables and functions relating to a QgsMapSettings object. + * For instance, map scale and rotation. + */ + static QgsExpressionContextScope* mapSettingsScope( const QgsMapSettings &mapSettings ); + /** Creates a new scope which contains variables and functions relating to a QgsComposition. * For instance, number of pages and page sizes. * @param composition source composition diff --git a/src/gui/qgsmapcanvas.cpp b/src/gui/qgsmapcanvas.cpp index a8705ede696..7b081f38eef 100644 --- a/src/gui/qgsmapcanvas.cpp +++ b/src/gui/qgsmapcanvas.cpp @@ -666,6 +666,7 @@ void QgsMapCanvas::refreshMap() QgsExpressionContext expressionContext; expressionContext << QgsExpressionContextUtils::globalScope() << QgsExpressionContextUtils::projectScope() + << QgsExpressionContextUtils::mapSettingsScope( mSettings ) << new QgsExpressionContextScope( mExpressionContextScope ); mSettings.setExpressionContext( expressionContext );