Skip to content
Permalink
Browse files
Make QgsMapCanvas a QgsExpressionContextGenerator
  • Loading branch information
nyalldawson committed May 25, 2021
1 parent 19ff469 commit aba8ddaa48b40355eec6021652a51ebdc28fc722
Showing with 28 additions and 20 deletions.
  1. +5 −2 python/gui/auto_generated/qgsmapcanvas.sip.in
  2. +19 −16 src/gui/qgsmapcanvas.cpp
  3. +4 −2 src/gui/qgsmapcanvas.h
@@ -22,7 +22,7 @@



class QgsMapCanvas : QGraphicsView
class QgsMapCanvas : QGraphicsView, QgsExpressionContextGenerator
{
%Docstring(signature="appended")
Map canvas is a class for displaying all GIS data types on a canvas.
@@ -737,7 +737,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.

@@ -748,6 +748,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
@@ -538,14 +538,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() )
@@ -585,20 +601,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() )
@@ -82,7 +82,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
@@ -703,7 +703,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 aba8dda

Please sign in to comment.