Skip to content
Permalink
Browse files

Make map tool snap to grid crs aware

  • Loading branch information
m-kuhn committed Aug 23, 2018
1 parent 8fc08d8 commit 9e387e48fc856a27cfd9a9d89f4a467d81ce34e4
@@ -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
@@ -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 )
@@ -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:

@@ -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 );
}
@@ -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 );
}
@@ -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 );
}

0 comments on commit 9e387e4

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