From 70fa078410ffdfaa8b5fb7e60ba4a1e090740b4d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 24 May 2021 13:18:34 +1000 Subject: [PATCH] Make QgsMapCanvas a QgsExpressionContextGenerator (cherry picked from commit 79338d130607d6c36c5d4625eb394463c2909d61) --- python/gui/auto_generated/qgsmapcanvas.sip.in | 8 +++-- src/gui/qgsmapcanvas.cpp | 35 ++++++++++--------- src/gui/qgsmapcanvas.h | 6 ++-- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/python/gui/auto_generated/qgsmapcanvas.sip.in b/python/gui/auto_generated/qgsmapcanvas.sip.in index dc8d9ffc14a1..6ebb3bfa6770 100644 --- a/python/gui/auto_generated/qgsmapcanvas.sip.in +++ b/python/gui/auto_generated/qgsmapcanvas.sip.in @@ -21,7 +21,8 @@ -class QgsMapCanvas : QGraphicsView + +class QgsMapCanvas : QGraphicsView, QgsExpressionContextGenerator { %Docstring Map canvas is a class for displaying all GIS data types on a canvas. @@ -694,7 +695,7 @@ overrides for expression evaluation for the map canvas render. %End - QgsExpressionContextScope *defaultExpressionContextScope() /Factory/; + QgsExpressionContextScope *defaultExpressionContextScope() const /Factory/; %Docstring Creates a new scope which contains default variables and functions relating to the map canvas. @@ -705,6 +706,9 @@ Creates a new scope which contains default variables and functions relating to t .. versionadded:: 3.4 %End + virtual QgsExpressionContext createExpressionContext() const; + + void setSegmentationTolerance( double tolerance ); %Docstring Sets the segmentation tolerance applied when rendering curved geometries diff --git a/src/gui/qgsmapcanvas.cpp b/src/gui/qgsmapcanvas.cpp index 3f5c4ad446a1..2c35cf631dd7 100644 --- a/src/gui/qgsmapcanvas.cpp +++ b/src/gui/qgsmapcanvas.cpp @@ -514,14 +514,30 @@ QgsMapLayer *QgsMapCanvas::currentLayer() return mCurrentLayer; } -QgsExpressionContextScope *QgsMapCanvas::defaultExpressionContextScope() +QgsExpressionContextScope *QgsMapCanvas::defaultExpressionContextScope() const { QgsExpressionContextScope *s = new QgsExpressionContextScope( QObject::tr( "Map Canvas" ) ); s->setVariable( QStringLiteral( "canvas_cursor_point" ), QgsGeometry::fromPointXY( cursorPoint() ), true ); - return s; } +QgsExpressionContext QgsMapCanvas::createExpressionContext() const +{ + //build the expression context + QgsExpressionContext expressionContext; + expressionContext << QgsExpressionContextUtils::globalScope() + << QgsExpressionContextUtils::projectScope( QgsProject::instance() ) + << QgsExpressionContextUtils::atlasScope( nullptr ) + << QgsExpressionContextUtils::mapSettingsScope( mSettings ); + if ( QgsExpressionContextScopeGenerator *generator = dynamic_cast< QgsExpressionContextScopeGenerator * >( mController ) ) + { + expressionContext << generator->createExpressionContextScope(); + } + expressionContext << defaultExpressionContextScope() + << new QgsExpressionContextScope( mExpressionContextScope ); + return expressionContext; +} + void QgsMapCanvas::refresh() { if ( !mSettings.hasValidSettings() ) @@ -559,20 +575,7 @@ void QgsMapCanvas::refreshMap() stopRendering(); // if any... stopPreviewJobs(); - //build the expression context - QgsExpressionContext expressionContext; - expressionContext << QgsExpressionContextUtils::globalScope() - << QgsExpressionContextUtils::projectScope( QgsProject::instance() ) - << QgsExpressionContextUtils::atlasScope( nullptr ) - << QgsExpressionContextUtils::mapSettingsScope( mSettings ); - if ( QgsExpressionContextScopeGenerator *generator = dynamic_cast< QgsExpressionContextScopeGenerator * >( mController ) ) - { - expressionContext << generator->createExpressionContextScope(); - } - expressionContext << defaultExpressionContextScope() - << new QgsExpressionContextScope( mExpressionContextScope ); - - mSettings.setExpressionContext( expressionContext ); + mSettings.setExpressionContext( createExpressionContext() ); mSettings.setPathResolver( QgsProject::instance()->pathResolver() ); if ( !mTheme.isEmpty() ) diff --git a/src/gui/qgsmapcanvas.h b/src/gui/qgsmapcanvas.h index c613d8dd32f4..6bb56a8b8cb2 100644 --- a/src/gui/qgsmapcanvas.h +++ b/src/gui/qgsmapcanvas.h @@ -81,7 +81,7 @@ class QgsMapMouseEvent; * \brief Map canvas is a class for displaying all GIS data types on a canvas. */ -class GUI_EXPORT QgsMapCanvas : public QGraphicsView +class GUI_EXPORT QgsMapCanvas : public QGraphicsView, public QgsExpressionContextGenerator { #ifdef SIP_RUN @@ -658,7 +658,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView * \see setExpressionContextScope() * \since QGIS 3.4 */ - QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY; + QgsExpressionContextScope *defaultExpressionContextScope() const SIP_FACTORY; + + QgsExpressionContext createExpressionContext() const override; /** * Sets the segmentation tolerance applied when rendering curved geometries