Skip to content

Commit 9e387e4

Browse files
committedAug 26, 2018
Make map tool snap to grid crs aware
1 parent 8fc08d8 commit 9e387e4

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed
 

‎python/gui/auto_generated/qgsmapmouseevent.sip.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ Alias to pos()
119119
:return: Mouse position in pixel coordinates
120120
%End
121121

122-
void snapToGrid( double precision );
122+
void snapToGrid( double precision, const QgsCoordinateReferenceSystem &crs );
123123
%Docstring
124124
Snaps the mapPoint to a grid with the given ``precision``.
125+
The snapping will be done in the specified ``crs``. If this crs is
126+
different from the mapCanvas crs, it will add some overhead.
125127

126128
.. versionadded:: 3.4
127129
%End

‎src/gui/qgsmapmouseevent.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,21 @@ void QgsMapMouseEvent::setMapPoint( const QgsPointXY &point )
7171
mPixelPoint = mapToPixelCoordinates( point );
7272
}
7373

74-
void QgsMapMouseEvent::snapToGrid( double precision )
74+
void QgsMapMouseEvent::snapToGrid( double precision, const QgsCoordinateReferenceSystem &crs )
7575
{
7676
if ( precision <= 0 )
7777
return;
7878

79-
mMapPoint.setX( std::round( mMapPoint.x() / precision ) * precision );
80-
mMapPoint.setY( std::round( mMapPoint.y() / precision ) * precision );
79+
QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), crs, mMapCanvas->mapSettings().transformContext() );
8180

82-
// mSnapMatch = QgsPointLocator::Match( mSnapMatch.type(), mSnapMatch.layer(), mSnapMatch.featureId(), mSnapMatch.distance(), mMapPoint, mSnapMatch.vertexIndex(), mSnapMatch.edgePoints() );
81+
QgsPointXY pt = ct.transform( mMapPoint );
8382

84-
setMapPoint( mMapPoint );
83+
pt.setX( std::round( mMapPoint.x() / precision ) * precision );
84+
pt.setY( std::round( mMapPoint.y() / precision ) * precision );
85+
86+
pt = ct.transform( pt, QgsCoordinateTransform::ReverseTransform );
87+
88+
setMapPoint( pt );
8589
}
8690

8791
QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point )

‎src/gui/qgsmapmouseevent.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ class GUI_EXPORT QgsMapMouseEvent : public QMouseEvent
128128

129129
/**
130130
* Snaps the mapPoint to a grid with the given \a precision.
131+
* The snapping will be done in the specified \a crs. If this crs is
132+
* different from the mapCanvas crs, it will add some overhead.
131133
*
132134
* \since QGIS 3.4
133135
*/
134-
void snapToGrid( double precision );
136+
void snapToGrid( double precision, const QgsCoordinateReferenceSystem &crs );
135137

136138
private:
137139

‎src/gui/qgsmaptooladvanceddigitizing.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ void QgsMapToolAdvancedDigitizing::canvasPressEvent( QgsMapMouseEvent *e )
3939
e->snapPoint();
4040
}
4141

42-
if ( currentVectorLayer() )
43-
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );
42+
QgsVectorLayer *layer = currentVectorLayer();
43+
if ( layer )
44+
{
45+
e->snapToGrid( layer->geometryOptions().geometryPrecision, layer->crs() );
46+
}
4447

4548
cadCanvasPressEvent( e );
4649
}
@@ -76,8 +79,11 @@ void QgsMapToolAdvancedDigitizing::canvasReleaseEvent( QgsMapMouseEvent *e )
7679
e->snapPoint();
7780
}
7881

79-
if ( currentVectorLayer() )
80-
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );
82+
QgsVectorLayer *layer = currentVectorLayer();
83+
if ( layer )
84+
{
85+
e->snapToGrid( layer->geometryOptions().geometryPrecision, layer->crs() );
86+
}
8187

8288
cadCanvasReleaseEvent( e );
8389
}
@@ -98,8 +104,11 @@ void QgsMapToolAdvancedDigitizing::canvasMoveEvent( QgsMapMouseEvent *e )
98104
e->snapPoint();
99105
}
100106

101-
if ( currentVectorLayer() )
102-
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );
107+
QgsVectorLayer *layer = currentVectorLayer();
108+
if ( layer )
109+
{
110+
e->snapToGrid( layer->geometryOptions().geometryPrecision, layer->crs() );
111+
}
103112

104113
cadCanvasMoveEvent( e );
105114
}

0 commit comments

Comments
 (0)
Please sign in to comment.