Skip to content

Commit 9e57e3d

Browse files
committed
Add ability to set expression context variables for map canvas
1 parent c8a6566 commit 9e57e3d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

python/gui/qgsmapcanvas.sip

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,24 @@ class QgsMapCanvas : QGraphicsView
338338
*/
339339
void setSnappingUtils( QgsSnappingUtils* utils );
340340

341+
/** Sets an expression context scope for the map canvas. This scope is injected into the expression
342+
* context used for rendering the map, and can be used to apply specific variable overrides for
343+
* expression evaluation for the map canvas render. This method will overwrite the existing expression
344+
* context scope for the canvas.
345+
* @param scope new expression context scope
346+
* @note added in QGIS 2.12
347+
* @see expressionContextScope()
348+
*/
349+
void setExpressionContextScope( const QgsExpressionContextScope& scope );
350+
351+
/** Returns a reference to the expression context scope for the map canvas. This scope is injected
352+
* into the expression context used for rendering the map, and can be used to apply specific variable
353+
* overrides for expression evaluation for the map canvas render.
354+
* @note added in QGIS 2.12
355+
* @see setExpressionContextScope()
356+
*/
357+
QgsExpressionContextScope& expressionContextScope();
358+
341359
public slots:
342360

343361
/** Repaints the canvas map*/

src/gui/qgsmapcanvas.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,14 @@ void QgsMapCanvas::refreshMap()
660660

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

664665
//build the expression context
665666
QgsExpressionContext expressionContext;
666667
expressionContext << QgsExpressionContextUtils::globalScope()
667-
<< QgsExpressionContextUtils::projectScope();
668+
<< QgsExpressionContextUtils::projectScope()
669+
<< new QgsExpressionContextScope( mExpressionContextScope );
670+
668671
mSettings.setExpressionContext( expressionContext );
669672

670673
// create the renderer job

src/gui/qgsmapcanvas.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <memory>
2525
#include <deque>
2626

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

413+
/** Sets an expression context scope for the map canvas. This scope is injected into the expression
414+
* context used for rendering the map, and can be used to apply specific variable overrides for
415+
* expression evaluation for the map canvas render. This method will overwrite the existing expression
416+
* context scope for the canvas.
417+
* @param scope new expression context scope
418+
* @note added in QGIS 2.12
419+
* @see expressionContextScope()
420+
*/
421+
void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
422+
423+
/** Returns a reference to the expression context scope for the map canvas. This scope is injected
424+
* into the expression context used for rendering the map, and can be used to apply specific variable
425+
* overrides for expression evaluation for the map canvas render.
426+
* @note added in QGIS 2.12
427+
* @see setExpressionContextScope()
428+
*/
429+
QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
430+
412431
public slots:
413432

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

699718
QgsSnappingUtils* mSnappingUtils;
700719

720+
QgsExpressionContextScope mExpressionContextScope;
721+
701722
}; // class QgsMapCanvas
702723
Q_NOWARN_DEPRECATED_POP
703724

0 commit comments

Comments
 (0)