Skip to content
Permalink
Browse files

[FEATURE] map canvas @canvas_cursor_point variable (#7225)

  • Loading branch information
nirvn committed Jun 28, 2018
1 parent 85fba79 commit fb9e575252ef9457b1e2307033e8110a6d61e5d0
@@ -611,6 +611,8 @@ context scope for the canvas.

.. seealso:: :py:func:`expressionContextScope`

.. seealso:: :py:func:`defaultExpressionContextScope`

.. versionadded:: 2.12
%End

@@ -622,10 +624,23 @@ overrides for expression evaluation for the map canvas render.

.. seealso:: :py:func:`setExpressionContextScope`

.. seealso:: :py:func:`defaultExpressionContextScope`

.. versionadded:: 2.12
%End


QgsExpressionContextScope *defaultExpressionContextScope() /Factory/;
%Docstring
Creates a new scope which contains default variables and functions relating to the map canvas.

.. seealso:: :py:func:`expressionContextScope`

.. seealso:: :py:func:`setExpressionContextScope`

.. versionadded:: 3.4
%End

void setSegmentationTolerance( double tolerance );
%Docstring
Sets the segmentation tolerance applied when rendering curved geometries
@@ -748,6 +748,9 @@ void QgsExpression::initVariableHelp()
sVariableHelpTexts.insert( QStringLiteral( "grid_number" ), QCoreApplication::translate( "variable_help", "Current grid annotation value." ) );
sVariableHelpTexts.insert( QStringLiteral( "grid_axis" ), QCoreApplication::translate( "variable_help", "Current grid annotation axis (e.g., 'x' for longitude, 'y' for latitude)." ) );

// map canvas item variables
sVariableHelpTexts.insert( QStringLiteral( "canvas_cursor_point" ), QCoreApplication::translate( "variable_help", "Last cursor position on the canvas in the project's geographical coordinates." ) );

// map tool capture variables
sVariableHelpTexts.insert( QStringLiteral( "snapping_results" ), QCoreApplication::translate( "variable_help",
"<p>An array with an item for each snapped point.</p>"
@@ -466,6 +466,13 @@ QgsMapLayer *QgsMapCanvas::currentLayer()
return mCurrentLayer;
}

QgsExpressionContextScope *QgsMapCanvas::defaultExpressionContextScope()
{
QgsExpressionContextScope *s = new QgsExpressionContextScope( QObject::tr( "Map Canvas" ) );
s->setVariable( QStringLiteral( "canvas_cursor_point" ), QgsGeometry::fromPointXY( cursorPoint() ), true );

return s;
}

void QgsMapCanvas::refresh()
{
@@ -510,6 +517,7 @@ void QgsMapCanvas::refreshMap()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr )
<< QgsExpressionContextUtils::mapSettingsScope( mSettings )
<< defaultExpressionContextScope()
<< new QgsExpressionContextScope( mExpressionContextScope );

mSettings.setExpressionContext( expressionContext );
@@ -862,6 +870,10 @@ QgsPointXY QgsMapCanvas::center() const
return r.center();
}

QgsPointXY QgsMapCanvas::cursorPoint() const
{
return mCursorPoint;
}

double QgsMapCanvas::rotation() const
{
@@ -1622,9 +1634,8 @@ void QgsMapCanvas::mouseMoveEvent( QMouseEvent *e )
}

// show x y on status bar
QPoint xy = e->pos();
QgsPointXY coord = getCoordinateTransform()->toMapCoordinates( xy );
emit xyCoordinates( coord );
mCursorPoint = getCoordinateTransform()->toMapCoordinates( mCanvasProperties->mouseLastXY );
emit xyCoordinates( mCursorPoint );
}

void QgsMapCanvas::setMapTool( QgsMapTool *tool, bool clean )
@@ -544,6 +544,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
* context scope for the canvas.
* \param scope new expression context scope
* \see expressionContextScope()
* \see defaultExpressionContextScope()
* \since QGIS 2.12
*/
void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
@@ -553,18 +554,28 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
* 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.
* \see setExpressionContextScope()
* \see defaultExpressionContextScope()
* \since QGIS 2.12
*/
QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }

/**
* Returns a const reference to the expression context scope for the map canvas.
* \see setExpressionContextScope()
* \see defaultExpressionContextScope()
* \note not available in Python bindings
* \since QGIS 2.12
*/
const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP

/**
* Creates a new scope which contains default variables and functions relating to the map canvas.
* \see expressionContextScope()
* \see setExpressionContextScope()
* \since QGIS 3.4
*/
QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;

/**
* Sets the segmentation tolerance applied when rendering curved geometries
\param tolerance the segmentation tolerance*/
@@ -990,12 +1001,20 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView

QString mTheme;

QgsPointXY mCursorPoint;

bool mAnnotationsVisible = true;

bool mUsePreviewJobs = false;

QHash< QString, int > mLastLayerRenderTime;

/**
* Returns the last cursor position on the canvas in geographical coordinates
* \since QGIS 3.4
*/
QgsPointXY cursorPoint() const;

/**
* Force a resize of the map canvas item
* \since QGIS 2.16
@@ -83,6 +83,7 @@ QList<QgsExpressionContextScope *> QgsSymbolWidgetContext::globalProjectAtlasMap
if ( mMapCanvas )
{
scopes << QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
<< mMapCanvas->defaultExpressionContextScope()
<< new QgsExpressionContextScope( mMapCanvas->expressionContextScope() );
}
else

0 comments on commit fb9e575

Please sign in to comment.
You can’t perform that action at this time.