From 9e387e48fc856a27cfd9a9d89f4a467d81ce34e4 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Thu, 23 Aug 2018 06:43:21 +0200 Subject: [PATCH] Make map tool snap to grid crs aware --- .../auto_generated/qgsmapmouseevent.sip.in | 4 +++- src/gui/qgsmapmouseevent.cpp | 14 ++++++++----- src/gui/qgsmapmouseevent.h | 4 +++- src/gui/qgsmaptooladvanceddigitizing.cpp | 21 +++++++++++++------ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/python/gui/auto_generated/qgsmapmouseevent.sip.in b/python/gui/auto_generated/qgsmapmouseevent.sip.in index a24366fd317..a2547417550 100644 --- a/python/gui/auto_generated/qgsmapmouseevent.sip.in +++ b/python/gui/auto_generated/qgsmapmouseevent.sip.in @@ -119,9 +119,11 @@ Alias to pos() :return: Mouse position in pixel coordinates %End - void snapToGrid( double precision ); + void snapToGrid( double precision, const QgsCoordinateReferenceSystem &crs ); %Docstring Snaps the mapPoint to a grid with the given ``precision``. +The snapping will be done in the specified ``crs``. If this crs is +different from the mapCanvas crs, it will add some overhead. .. versionadded:: 3.4 %End diff --git a/src/gui/qgsmapmouseevent.cpp b/src/gui/qgsmapmouseevent.cpp index 9cf9cd9c016..baaa5ab6381 100644 --- a/src/gui/qgsmapmouseevent.cpp +++ b/src/gui/qgsmapmouseevent.cpp @@ -71,17 +71,21 @@ void QgsMapMouseEvent::setMapPoint( const QgsPointXY &point ) mPixelPoint = mapToPixelCoordinates( point ); } -void QgsMapMouseEvent::snapToGrid( double precision ) +void QgsMapMouseEvent::snapToGrid( double precision, const QgsCoordinateReferenceSystem &crs ) { if ( precision <= 0 ) return; - mMapPoint.setX( std::round( mMapPoint.x() / precision ) * precision ); - mMapPoint.setY( std::round( mMapPoint.y() / precision ) * precision ); + QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), crs, mMapCanvas->mapSettings().transformContext() ); - // mSnapMatch = QgsPointLocator::Match( mSnapMatch.type(), mSnapMatch.layer(), mSnapMatch.featureId(), mSnapMatch.distance(), mMapPoint, mSnapMatch.vertexIndex(), mSnapMatch.edgePoints() ); + QgsPointXY pt = ct.transform( mMapPoint ); - setMapPoint( mMapPoint ); + pt.setX( std::round( mMapPoint.x() / precision ) * precision ); + pt.setY( std::round( mMapPoint.y() / precision ) * precision ); + + pt = ct.transform( pt, QgsCoordinateTransform::ReverseTransform ); + + setMapPoint( pt ); } QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point ) diff --git a/src/gui/qgsmapmouseevent.h b/src/gui/qgsmapmouseevent.h index 0b89cec92a1..01d143d8ff5 100644 --- a/src/gui/qgsmapmouseevent.h +++ b/src/gui/qgsmapmouseevent.h @@ -128,10 +128,12 @@ class GUI_EXPORT QgsMapMouseEvent : public QMouseEvent /** * Snaps the mapPoint to a grid with the given \a precision. + * The snapping will be done in the specified \a crs. If this crs is + * different from the mapCanvas crs, it will add some overhead. * * \since QGIS 3.4 */ - void snapToGrid( double precision ); + void snapToGrid( double precision, const QgsCoordinateReferenceSystem &crs ); private: diff --git a/src/gui/qgsmaptooladvanceddigitizing.cpp b/src/gui/qgsmaptooladvanceddigitizing.cpp index 75c665acf6d..a1fd537f520 100644 --- a/src/gui/qgsmaptooladvanceddigitizing.cpp +++ b/src/gui/qgsmaptooladvanceddigitizing.cpp @@ -39,8 +39,11 @@ void QgsMapToolAdvancedDigitizing::canvasPressEvent( QgsMapMouseEvent *e ) e->snapPoint(); } - if ( currentVectorLayer() ) - e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision ); + QgsVectorLayer *layer = currentVectorLayer(); + if ( layer ) + { + e->snapToGrid( layer->geometryOptions().geometryPrecision, layer->crs() ); + } cadCanvasPressEvent( e ); } @@ -76,8 +79,11 @@ void QgsMapToolAdvancedDigitizing::canvasReleaseEvent( QgsMapMouseEvent *e ) e->snapPoint(); } - if ( currentVectorLayer() ) - e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision ); + QgsVectorLayer *layer = currentVectorLayer(); + if ( layer ) + { + e->snapToGrid( layer->geometryOptions().geometryPrecision, layer->crs() ); + } cadCanvasReleaseEvent( e ); } @@ -98,8 +104,11 @@ void QgsMapToolAdvancedDigitizing::canvasMoveEvent( QgsMapMouseEvent *e ) e->snapPoint(); } - if ( currentVectorLayer() ) - e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision ); + QgsVectorLayer *layer = currentVectorLayer(); + if ( layer ) + { + e->snapToGrid( layer->geometryOptions().geometryPrecision, layer->crs() ); + } cadCanvasMoveEvent( e ); }