Skip to content
Permalink
Browse files

Expose map settings (rotation, scale) through expression contexts

  • Loading branch information
nyalldawson committed Sep 7, 2015
1 parent 9e57e3d commit 829ff502003226f3da774d168c2603dc25179135
@@ -746,6 +746,8 @@ class QgsComposerMap : QgsComposerItem
* @note added in 2.6 */
void requestedExtent( QgsRectangle& extent ) const;

virtual QgsExpressionContext* createExpressionContext() const;

signals:
void extentChanged();

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

@@ -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" ) );
@@ -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
@@ -666,6 +666,7 @@ void QgsMapCanvas::refreshMap()
QgsExpressionContext expressionContext;
expressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::mapSettingsScope( mSettings )
<< new QgsExpressionContextScope( mExpressionContextScope );

mSettings.setExpressionContext( expressionContext );

0 comments on commit 829ff50

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