mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-18 00:03:05 -04:00
Make map tool snap to grid crs aware
This commit is contained in:
parent
8fc08d8c13
commit
9e387e48fc
@ -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 );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user