Skip to content

Commit

Permalink
Expose map settings (rotation, scale) through expression contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 7, 2015
1 parent 9e57e3d commit 829ff50
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/core/composer/qgscomposermap.sip
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ class QgsComposerMap : QgsComposerItem
* @note added in 2.6 */
void requestedExtent( QgsRectangle& extent ) const;

virtual QgsExpressionContext* createExpressionContext() const;

signals:
void extentChanged();

Expand Down
5 changes: 5 additions & 0 deletions python/core/qgsexpressioncontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) );
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsexpressioncontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class QgsMapLayer;
class QgsComposition;
class QgsComposerItem;
class QgsAtlasComposition;
class QgsMapSettings;

/** \ingroup core
* \class QgsScopedExpressionFunction
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ void QgsMapCanvas::refreshMap()
QgsExpressionContext expressionContext;
expressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::mapSettingsScope( mSettings )
<< new QgsExpressionContextScope( mExpressionContextScope );

mSettings.setExpressionContext( expressionContext );
Expand Down

0 comments on commit 829ff50

Please sign in to comment.