Skip to content
Permalink
Browse files
Make QgsMapCanvas a QgsExpressionContextGenerator
(cherry picked from commit 79338d1)
  • Loading branch information
nyalldawson committed Jun 14, 2021
1 parent d1ac81a commit 8a687ee4589453aee42788a6c0da9d217953811f
Showing with 29 additions and 20 deletions.
  1. +6 −2 python/gui/auto_generated/qgsmapcanvas.sip.in
  2. +19 −16 src/gui/qgsmapcanvas.cpp
  3. +4 −2 src/gui/qgsmapcanvas.h
@@ -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
@@ -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() )
@@ -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

0 comments on commit 8a687ee

Please sign in to comment.