Skip to content

Commit

Permalink
Make map tool snap to grid crs aware
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Aug 26, 2018
1 parent 8fc08d8 commit 9e387e4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion python/gui/auto_generated/qgsmapmouseevent.sip.in
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/gui/qgsmapmouseevent.cpp
Expand Up @@ -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 )
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgsmapmouseevent.h
Expand Up @@ -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:

Expand Down
21 changes: 15 additions & 6 deletions src/gui/qgsmaptooladvanceddigitizing.cpp
Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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 );
}
Expand Down

0 comments on commit 9e387e4

Please sign in to comment.