Skip to content
Permalink
Browse files

Snap nodes in digitizing map tools to grid

  • Loading branch information
m-kuhn committed Aug 23, 2018
1 parent 5ef3751 commit 8fc08d8c13fceaba3db51b9a8b284619e77e9bf0
@@ -117,6 +117,13 @@ The unsnapped, real mouse cursor position in pixel coordinates.
Alias to pos()

:return: Mouse position in pixel coordinates
%End

void snapToGrid( double precision );
%Docstring
Snaps the mapPoint to a grid with the given ``precision``.

.. versionadded:: 3.4
%End

};
@@ -71,6 +71,19 @@ void QgsMapMouseEvent::setMapPoint( const QgsPointXY &point )
mPixelPoint = mapToPixelCoordinates( point );
}

void QgsMapMouseEvent::snapToGrid( double precision )
{
if ( precision <= 0 )
return;

mMapPoint.setX( std::round( mMapPoint.x() / precision ) * precision );
mMapPoint.setY( std::round( mMapPoint.y() / precision ) * precision );

// mSnapMatch = QgsPointLocator::Match( mSnapMatch.type(), mSnapMatch.layer(), mSnapMatch.featureId(), mSnapMatch.distance(), mMapPoint, mSnapMatch.vertexIndex(), mSnapMatch.edgePoints() );

setMapPoint( mMapPoint );
}

QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point )
{
double x = point.x(), y = point.y();
@@ -126,6 +126,13 @@ class GUI_EXPORT QgsMapMouseEvent : public QMouseEvent
*/
QPoint originalPixelPoint() const { return pos(); }

/**
* Snaps the mapPoint to a grid with the given \a precision.
*
* \since QGIS 3.4
*/
void snapToGrid( double precision );

private:

QPoint mapToPixelCoordinates( const QgsPointXY &point );
@@ -17,6 +17,7 @@
#include "qgsmaptooladvanceddigitizing.h"
#include "qgsmapcanvas.h"
#include "qgsadvanceddigitizingdockwidget.h"
#include "qgsvectorlayer.h"

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

if ( currentVectorLayer() )
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );

cadCanvasPressEvent( e );
}

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

if ( currentVectorLayer() )
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );

cadCanvasReleaseEvent( e );
}

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

if ( currentVectorLayer() )
e->snapToGrid( currentVectorLayer()->geometryOptions().geometryPrecision );

cadCanvasMoveEvent( e );
}

0 comments on commit 8fc08d8

Please sign in to comment.
You can’t perform that action at this time.