Skip to content

Commit 8fc08d8

Browse files
committed
Snap nodes in digitizing map tools to grid
1 parent 5ef3751 commit 8fc08d8

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

python/gui/auto_generated/qgsmapmouseevent.sip.in

+7
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ The unsnapped, real mouse cursor position in pixel coordinates.
117117
Alias to pos()
118118

119119
:return: Mouse position in pixel coordinates
120+
%End
121+
122+
void snapToGrid( double precision );
123+
%Docstring
124+
Snaps the mapPoint to a grid with the given ``precision``.
125+
126+
.. versionadded:: 3.4
120127
%End
121128

122129
};

src/gui/qgsmapmouseevent.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ void QgsMapMouseEvent::setMapPoint( const QgsPointXY &point )
7171
mPixelPoint = mapToPixelCoordinates( point );
7272
}
7373

74+
void QgsMapMouseEvent::snapToGrid( double precision )
75+
{
76+
if ( precision <= 0 )
77+
return;
78+
79+
mMapPoint.setX( std::round( mMapPoint.x() / precision ) * precision );
80+
mMapPoint.setY( std::round( mMapPoint.y() / precision ) * precision );
81+
82+
// mSnapMatch = QgsPointLocator::Match( mSnapMatch.type(), mSnapMatch.layer(), mSnapMatch.featureId(), mSnapMatch.distance(), mMapPoint, mSnapMatch.vertexIndex(), mSnapMatch.edgePoints() );
83+
84+
setMapPoint( mMapPoint );
85+
}
86+
7487
QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point )
7588
{
7689
double x = point.x(), y = point.y();

src/gui/qgsmapmouseevent.h

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ class GUI_EXPORT QgsMapMouseEvent : public QMouseEvent
126126
*/
127127
QPoint originalPixelPoint() const { return pos(); }
128128

129+
/**
130+
* Snaps the mapPoint to a grid with the given \a precision.
131+
*
132+
* \since QGIS 3.4
133+
*/
134+
void snapToGrid( double precision );
135+
129136
private:
130137

131138
QPoint mapToPixelCoordinates( const QgsPointXY &point );

src/gui/qgsmaptooladvanceddigitizing.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "qgsmaptooladvanceddigitizing.h"
1818
#include "qgsmapcanvas.h"
1919
#include "qgsadvanceddigitizingdockwidget.h"
20+
#include "qgsvectorlayer.h"
2021

2122
QgsMapToolAdvancedDigitizing::QgsMapToolAdvancedDigitizing( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget )
2223
: QgsMapToolEdit( canvas )
@@ -38,6 +39,9 @@ void QgsMapToolAdvancedDigitizing::canvasPressEvent( QgsMapMouseEvent *e )
3839
e->snapPoint();
3940
}
4041

42+
if ( currentVectorLayer() )
43+
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );
44+
4145
cadCanvasPressEvent( e );
4246
}
4347

@@ -72,6 +76,9 @@ void QgsMapToolAdvancedDigitizing::canvasReleaseEvent( QgsMapMouseEvent *e )
7276
e->snapPoint();
7377
}
7478

79+
if ( currentVectorLayer() )
80+
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );
81+
7582
cadCanvasReleaseEvent( e );
7683
}
7784

@@ -91,6 +98,9 @@ void QgsMapToolAdvancedDigitizing::canvasMoveEvent( QgsMapMouseEvent *e )
9198
e->snapPoint();
9299
}
93100

101+
if ( currentVectorLayer() )
102+
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );
103+
94104
cadCanvasMoveEvent( e );
95105
}
96106

0 commit comments

Comments
 (0)