Skip to content
Permalink
Browse files

Add ability to set expression context variables for map canvas

  • Loading branch information
nyalldawson committed Sep 7, 2015
1 parent c8a6566 commit 9e57e3d7ea832007086982d94669534d0c164d2e
Showing with 43 additions and 1 deletion.
  1. +18 −0 python/gui/qgsmapcanvas.sip
  2. +4 −1 src/gui/qgsmapcanvas.cpp
  3. +21 −0 src/gui/qgsmapcanvas.h
@@ -338,6 +338,24 @@ class QgsMapCanvas : QGraphicsView
*/
void setSnappingUtils( QgsSnappingUtils* utils );

/** Sets an expression context scope for the map canvas. This scope is injected into the expression
* context used for rendering the map, and can be used to apply specific variable overrides for
* expression evaluation for the map canvas render. This method will overwrite the existing expression
* context scope for the canvas.
* @param scope new expression context scope
* @note added in QGIS 2.12
* @see expressionContextScope()
*/
void setExpressionContextScope( const QgsExpressionContextScope& scope );

/** Returns a reference to the expression context scope for the map canvas. This scope is injected
* into the expression context used for rendering the map, and can be used to apply specific variable
* overrides for expression evaluation for the map canvas render.
* @note added in QGIS 2.12
* @see setExpressionContextScope()
*/
QgsExpressionContextScope& expressionContextScope();

public slots:

/** Repaints the canvas map*/
@@ -660,11 +660,14 @@ void QgsMapCanvas::refreshMap()

//update $map variable to canvas
QgsExpression::setSpecialColumn( "$map", tr( "canvas" ) );
mExpressionContextScope.setVariable( "map_id", "canvas" );

//build the expression context
QgsExpressionContext expressionContext;
expressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();
<< QgsExpressionContextUtils::projectScope()
<< new QgsExpressionContextScope( mExpressionContextScope );

mSettings.setExpressionContext( expressionContext );

// create the renderer job
@@ -24,6 +24,7 @@
#include <memory>
#include <deque>

#include "qgsexpressioncontext.h"
#include "qgsrectangle.h"
#include "qgspoint.h"
#include "qgis.h"
@@ -409,6 +410,24 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
void setSnappingUtils( QgsSnappingUtils* utils );

/** Sets an expression context scope for the map canvas. This scope is injected into the expression
* context used for rendering the map, and can be used to apply specific variable overrides for
* expression evaluation for the map canvas render. This method will overwrite the existing expression
* context scope for the canvas.
* @param scope new expression context scope
* @note added in QGIS 2.12
* @see expressionContextScope()
*/
void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }

/** Returns a reference to the expression context scope for the map canvas. This scope is injected
* into the expression context used for rendering the map, and can be used to apply specific variable
* overrides for expression evaluation for the map canvas render.
* @note added in QGIS 2.12
* @see setExpressionContextScope()
*/
QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }

public slots:

/** Repaints the canvas map*/
@@ -698,6 +717,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView

QgsSnappingUtils* mSnappingUtils;

QgsExpressionContextScope mExpressionContextScope;

}; // class QgsMapCanvas
Q_NOWARN_DEPRECATED_POP

0 comments on commit 9e57e3d

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