Skip to content
Permalink
Browse files

Gracefully handle transform errors in snap to grid

  • Loading branch information
m-kuhn committed Aug 26, 2018
1 parent 5c814cf commit f177228f49309e8738051ea062fcd924e54850e6
Showing with 42 additions and 26 deletions.
  1. +13 −6 src/gui/qgsmapmouseevent.cpp
  2. +29 −20 src/gui/qgssnaptogridcanvasitem.cpp
@@ -76,16 +76,23 @@ void QgsMapMouseEvent::snapToGrid( double precision, const QgsCoordinateReferenc
if ( precision <= 0 )
return;

QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), crs, mMapCanvas->mapSettings().transformContext() );
try
{
QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), crs, mMapCanvas->mapSettings().transformContext() );

QgsPointXY pt = ct.transform( mMapPoint );
QgsPointXY pt = ct.transform( mMapPoint );

pt.setX( std::round( pt.x() / precision ) * precision );
pt.setY( std::round( pt.y() / precision ) * precision );
pt.setX( std::round( pt.x() / precision ) * precision );
pt.setY( std::round( pt.y() / precision ) * precision );

pt = ct.transform( pt, QgsCoordinateTransform::ReverseTransform );
pt = ct.transform( pt, QgsCoordinateTransform::ReverseTransform );

setMapPoint( pt );
setMapPoint( pt );
}
catch ( QgsCsException &e )
{
Q_UNUSED( e )
}
}

QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point )
@@ -38,33 +38,42 @@ void QgsSnapToGridCanvasItem::paint( QPainter *painter )

if ( mEnabled && mAvailableByZoomFactor )
{
const QgsRectangle layerExtent = mTransform.transformBoundingBox( mapRect, QgsCoordinateTransform::ReverseTransform );
const QgsPointXY layerPt = mTransform.transform( mPoint, QgsCoordinateTransform::ReverseTransform );
try
{
const QgsRectangle layerExtent = mTransform.transformBoundingBox( mapRect, QgsCoordinateTransform::ReverseTransform );
const QgsPointXY layerPt = mTransform.transform( mPoint, QgsCoordinateTransform::ReverseTransform );

const double gridXMin = std::ceil( layerExtent.xMinimum() / mPrecision ) * mPrecision;
const double gridXMax = std::ceil( layerExtent.xMaximum() / mPrecision ) * mPrecision;
const double gridYMin = std::ceil( layerExtent.yMinimum() / mPrecision ) * mPrecision;
const double gridYMax = std::ceil( layerExtent.yMaximum() / mPrecision ) * mPrecision;
const double gridXMin = std::ceil( layerExtent.xMinimum() / mPrecision ) * mPrecision;
const double gridXMax = std::ceil( layerExtent.xMaximum() / mPrecision ) * mPrecision;
const double gridYMin = std::ceil( layerExtent.yMinimum() / mPrecision ) * mPrecision;
const double gridYMax = std::ceil( layerExtent.yMaximum() / mPrecision ) * mPrecision;

for ( int x = gridXMin ; x < gridXMax; x += mPrecision )
{
for ( int y = gridYMin ; y < gridYMax; y += mPrecision )
for ( int x = gridXMin ; x < gridXMax; x += mPrecision )
{
const QgsPointXY pt = mTransform.transform( x, y );
const QPointF canvasPt = toCanvasCoordinates( pt );

if ( qgsDoubleNear( layerPt.x(), x, mPrecision / 3 ) && qgsDoubleNear( layerPt.y(), y, mPrecision / 3 ) )
{
painter->setPen( mCurrentPointPen );
}
else
for ( int y = gridYMin ; y < gridYMax; y += mPrecision )
{
painter->setPen( mGridPen );
const QgsPointXY pt = mTransform.transform( x, y );
const QPointF canvasPt = toCanvasCoordinates( pt );

if ( qgsDoubleNear( layerPt.x(), x, mPrecision / 3 ) && qgsDoubleNear( layerPt.y(), y, mPrecision / 3 ) )
{
painter->setPen( mCurrentPointPen );
}
else
{
painter->setPen( mGridPen );
}
painter->drawLine( canvasPt.x() - 3, canvasPt.y(), canvasPt.x() + 3, canvasPt.y() );
painter->drawLine( canvasPt.x(), canvasPt.y() - 3, canvasPt.x(), canvasPt.y() + 3 );

}
painter->drawLine( canvasPt.x() - 3, canvasPt.y(), canvasPt.x() + 3, canvasPt.y() );
painter->drawLine( canvasPt.x(), canvasPt.y() - 3, canvasPt.x(), canvasPt.y() + 3 );
}
}
catch ( QgsCsException &e )
{
Q_UNUSED( e )
mAvailableByZoomFactor = false;
}
}

painter->restore();

0 comments on commit f177228

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