Skip to content

Commit fb9e575

Browse files
authored
[FEATURE] map canvas @canvas_cursor_point variable (#7225)
1 parent 85fba79 commit fb9e575

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

python/gui/auto_generated/qgsmapcanvas.sip.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ context scope for the canvas.
611611

612612
.. seealso:: :py:func:`expressionContextScope`
613613

614+
.. seealso:: :py:func:`defaultExpressionContextScope`
615+
614616
.. versionadded:: 2.12
615617
%End
616618

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

623625
.. seealso:: :py:func:`setExpressionContextScope`
624626

627+
.. seealso:: :py:func:`defaultExpressionContextScope`
628+
625629
.. versionadded:: 2.12
626630
%End
627631

628632

633+
QgsExpressionContextScope *defaultExpressionContextScope() /Factory/;
634+
%Docstring
635+
Creates a new scope which contains default variables and functions relating to the map canvas.
636+
637+
.. seealso:: :py:func:`expressionContextScope`
638+
639+
.. seealso:: :py:func:`setExpressionContextScope`
640+
641+
.. versionadded:: 3.4
642+
%End
643+
629644
void setSegmentationTolerance( double tolerance );
630645
%Docstring
631646
Sets the segmentation tolerance applied when rendering curved geometries

src/core/expression/qgsexpression.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,9 @@ void QgsExpression::initVariableHelp()
748748
sVariableHelpTexts.insert( QStringLiteral( "grid_number" ), QCoreApplication::translate( "variable_help", "Current grid annotation value." ) );
749749
sVariableHelpTexts.insert( QStringLiteral( "grid_axis" ), QCoreApplication::translate( "variable_help", "Current grid annotation axis (e.g., 'x' for longitude, 'y' for latitude)." ) );
750750

751+
// map canvas item variables
752+
sVariableHelpTexts.insert( QStringLiteral( "canvas_cursor_point" ), QCoreApplication::translate( "variable_help", "Last cursor position on the canvas in the project's geographical coordinates." ) );
753+
751754
// map tool capture variables
752755
sVariableHelpTexts.insert( QStringLiteral( "snapping_results" ), QCoreApplication::translate( "variable_help",
753756
"<p>An array with an item for each snapped point.</p>"

src/gui/qgsmapcanvas.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,13 @@ QgsMapLayer *QgsMapCanvas::currentLayer()
466466
return mCurrentLayer;
467467
}
468468

469+
QgsExpressionContextScope *QgsMapCanvas::defaultExpressionContextScope()
470+
{
471+
QgsExpressionContextScope *s = new QgsExpressionContextScope( QObject::tr( "Map Canvas" ) );
472+
s->setVariable( QStringLiteral( "canvas_cursor_point" ), QgsGeometry::fromPointXY( cursorPoint() ), true );
473+
474+
return s;
475+
}
469476

470477
void QgsMapCanvas::refresh()
471478
{
@@ -510,6 +517,7 @@ void QgsMapCanvas::refreshMap()
510517
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
511518
<< QgsExpressionContextUtils::atlasScope( nullptr )
512519
<< QgsExpressionContextUtils::mapSettingsScope( mSettings )
520+
<< defaultExpressionContextScope()
513521
<< new QgsExpressionContextScope( mExpressionContextScope );
514522

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

873+
QgsPointXY QgsMapCanvas::cursorPoint() const
874+
{
875+
return mCursorPoint;
876+
}
865877

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

16241636
// show x y on status bar
1625-
QPoint xy = e->pos();
1626-
QgsPointXY coord = getCoordinateTransform()->toMapCoordinates( xy );
1627-
emit xyCoordinates( coord );
1637+
mCursorPoint = getCoordinateTransform()->toMapCoordinates( mCanvasProperties->mouseLastXY );
1638+
emit xyCoordinates( mCursorPoint );
16281639
}
16291640

16301641
void QgsMapCanvas::setMapTool( QgsMapTool *tool, bool clean )

src/gui/qgsmapcanvas.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
544544
* context scope for the canvas.
545545
* \param scope new expression context scope
546546
* \see expressionContextScope()
547+
* \see defaultExpressionContextScope()
547548
* \since QGIS 2.12
548549
*/
549550
void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
@@ -553,18 +554,28 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
553554
* into the expression context used for rendering the map, and can be used to apply specific variable
554555
* overrides for expression evaluation for the map canvas render.
555556
* \see setExpressionContextScope()
557+
* \see defaultExpressionContextScope()
556558
* \since QGIS 2.12
557559
*/
558560
QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
559561

560562
/**
561563
* Returns a const reference to the expression context scope for the map canvas.
562564
* \see setExpressionContextScope()
565+
* \see defaultExpressionContextScope()
563566
* \note not available in Python bindings
564567
* \since QGIS 2.12
565568
*/
566569
const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
567570

571+
/**
572+
* Creates a new scope which contains default variables and functions relating to the map canvas.
573+
* \see expressionContextScope()
574+
* \see setExpressionContextScope()
575+
* \since QGIS 3.4
576+
*/
577+
QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;
578+
568579
/**
569580
* Sets the segmentation tolerance applied when rendering curved geometries
570581
\param tolerance the segmentation tolerance*/
@@ -990,12 +1001,20 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
9901001

9911002
QString mTheme;
9921003

1004+
QgsPointXY mCursorPoint;
1005+
9931006
bool mAnnotationsVisible = true;
9941007

9951008
bool mUsePreviewJobs = false;
9961009

9971010
QHash< QString, int > mLastLayerRenderTime;
9981011

1012+
/**
1013+
* Returns the last cursor position on the canvas in geographical coordinates
1014+
* \since QGIS 3.4
1015+
*/
1016+
QgsPointXY cursorPoint() const;
1017+
9991018
/**
10001019
* Force a resize of the map canvas item
10011020
* \since QGIS 2.16

src/gui/symbology/qgssymbolwidgetcontext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ QList<QgsExpressionContextScope *> QgsSymbolWidgetContext::globalProjectAtlasMap
8383
if ( mMapCanvas )
8484
{
8585
scopes << QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
86+
<< mMapCanvas->defaultExpressionContextScope()
8687
<< new QgsExpressionContextScope( mMapCanvas->expressionContextScope() );
8788
}
8889
else

0 commit comments

Comments
 (0)