Skip to content

Commit

Permalink
[FEATURE] map canvas @canvas_cursor_point variable (#7225)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jun 28, 2018
1 parent 85fba79 commit fb9e575
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
15 changes: 15 additions & 0 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Expand Up @@ -611,6 +611,8 @@ context scope for the canvas.

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

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

.. versionadded:: 2.12
%End

Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -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>"
Expand Down
17 changes: 14 additions & 3 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -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()
{
Expand Down Expand Up @@ -510,6 +517,7 @@ void QgsMapCanvas::refreshMap()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr )
<< QgsExpressionContextUtils::mapSettingsScope( mSettings )
<< defaultExpressionContextScope()
<< new QgsExpressionContextScope( mExpressionContextScope );

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

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

double QgsMapCanvas::rotation() const
{
Expand Down Expand Up @@ -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 )
Expand Down
19 changes: 19 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -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; }
Expand All @@ -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*/
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/gui/symbology/qgssymbolwidgetcontext.cpp
Expand Up @@ -83,6 +83,7 @@ QList<QgsExpressionContextScope *> QgsSymbolWidgetContext::globalProjectAtlasMap
if ( mMapCanvas )
{
scopes << QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
<< mMapCanvas->defaultExpressionContextScope()
<< new QgsExpressionContextScope( mMapCanvas->expressionContextScope() );
}
else
Expand Down

0 comments on commit fb9e575

Please sign in to comment.