Add ability to set expression context variables for map canvas

This commit is contained in:
Nyall Dawson 2015-09-07 19:18:34 +10:00
parent c8a65668ed
commit 9e57e3d7ea
3 changed files with 43 additions and 1 deletions

View File

@ -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*/

View File

@ -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

View File

@ -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