Skip to content

Commit

Permalink
Snap nodes in digitizing map tools to grid
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Aug 26, 2018
1 parent 5ef3751 commit 8fc08d8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsmapmouseevent.sip.in
Expand Up @@ -117,6 +117,13 @@ The unsnapped, real mouse cursor position in pixel coordinates.
Alias to pos() Alias to pos()


:return: Mouse position in pixel coordinates :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 %End


}; };
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsmapmouseevent.cpp
Expand Up @@ -71,6 +71,19 @@ void QgsMapMouseEvent::setMapPoint( const QgsPointXY &point )
mPixelPoint = mapToPixelCoordinates( 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 ) QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point )
{ {
double x = point.x(), y = point.y(); double x = point.x(), y = point.y();
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgsmapmouseevent.h
Expand Up @@ -126,6 +126,13 @@ class GUI_EXPORT QgsMapMouseEvent : public QMouseEvent
*/ */
QPoint originalPixelPoint() const { return pos(); } 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: private:


QPoint mapToPixelCoordinates( const QgsPointXY &point ); QPoint mapToPixelCoordinates( const QgsPointXY &point );
Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsmaptooladvanceddigitizing.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgsmaptooladvanceddigitizing.h" #include "qgsmaptooladvanceddigitizing.h"
#include "qgsmapcanvas.h" #include "qgsmapcanvas.h"
#include "qgsadvanceddigitizingdockwidget.h" #include "qgsadvanceddigitizingdockwidget.h"
#include "qgsvectorlayer.h"


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


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

cadCanvasPressEvent( e ); cadCanvasPressEvent( e );
} }


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


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

cadCanvasReleaseEvent( e ); cadCanvasReleaseEvent( e );
} }


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


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

cadCanvasMoveEvent( e ); cadCanvasMoveEvent( e );
} }


Expand Down

0 comments on commit 8fc08d8

Please sign in to comment.