Make map tool snap to grid crs aware

This commit is contained in:
Matthias Kuhn 2018-08-23 06:43:21 +02:00
parent 8fc08d8c13
commit 9e387e48fc
4 changed files with 30 additions and 13 deletions

View File

@ -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

View File

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

View File

@ -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:

View File

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