Skip to content

Commit f177228

Browse files
committed
Gracefully handle transform errors in snap to grid
1 parent 5c814cf commit f177228

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

src/gui/qgsmapmouseevent.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,23 @@ void QgsMapMouseEvent::snapToGrid( double precision, const QgsCoordinateReferenc
7676
if ( precision <= 0 )
7777
return;
7878

79-
QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), crs, mMapCanvas->mapSettings().transformContext() );
79+
try
80+
{
81+
QgsCoordinateTransform ct( mMapCanvas->mapSettings().destinationCrs(), crs, mMapCanvas->mapSettings().transformContext() );
8082

81-
QgsPointXY pt = ct.transform( mMapPoint );
83+
QgsPointXY pt = ct.transform( mMapPoint );
8284

83-
pt.setX( std::round( pt.x() / precision ) * precision );
84-
pt.setY( std::round( pt.y() / precision ) * precision );
85+
pt.setX( std::round( pt.x() / precision ) * precision );
86+
pt.setY( std::round( pt.y() / precision ) * precision );
8587

86-
pt = ct.transform( pt, QgsCoordinateTransform::ReverseTransform );
88+
pt = ct.transform( pt, QgsCoordinateTransform::ReverseTransform );
8789

88-
setMapPoint( pt );
90+
setMapPoint( pt );
91+
}
92+
catch ( QgsCsException &e )
93+
{
94+
Q_UNUSED( e )
95+
}
8996
}
9097

9198
QPoint QgsMapMouseEvent::mapToPixelCoordinates( const QgsPointXY &point )

src/gui/qgssnaptogridcanvasitem.cpp

+29-20
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,42 @@ void QgsSnapToGridCanvasItem::paint( QPainter *painter )
3838

3939
if ( mEnabled && mAvailableByZoomFactor )
4040
{
41-
const QgsRectangle layerExtent = mTransform.transformBoundingBox( mapRect, QgsCoordinateTransform::ReverseTransform );
42-
const QgsPointXY layerPt = mTransform.transform( mPoint, QgsCoordinateTransform::ReverseTransform );
41+
try
42+
{
43+
const QgsRectangle layerExtent = mTransform.transformBoundingBox( mapRect, QgsCoordinateTransform::ReverseTransform );
44+
const QgsPointXY layerPt = mTransform.transform( mPoint, QgsCoordinateTransform::ReverseTransform );
4345

44-
const double gridXMin = std::ceil( layerExtent.xMinimum() / mPrecision ) * mPrecision;
45-
const double gridXMax = std::ceil( layerExtent.xMaximum() / mPrecision ) * mPrecision;
46-
const double gridYMin = std::ceil( layerExtent.yMinimum() / mPrecision ) * mPrecision;
47-
const double gridYMax = std::ceil( layerExtent.yMaximum() / mPrecision ) * mPrecision;
46+
const double gridXMin = std::ceil( layerExtent.xMinimum() / mPrecision ) * mPrecision;
47+
const double gridXMax = std::ceil( layerExtent.xMaximum() / mPrecision ) * mPrecision;
48+
const double gridYMin = std::ceil( layerExtent.yMinimum() / mPrecision ) * mPrecision;
49+
const double gridYMax = std::ceil( layerExtent.yMaximum() / mPrecision ) * mPrecision;
4850

49-
for ( int x = gridXMin ; x < gridXMax; x += mPrecision )
50-
{
51-
for ( int y = gridYMin ; y < gridYMax; y += mPrecision )
51+
for ( int x = gridXMin ; x < gridXMax; x += mPrecision )
5252
{
53-
const QgsPointXY pt = mTransform.transform( x, y );
54-
const QPointF canvasPt = toCanvasCoordinates( pt );
55-
56-
if ( qgsDoubleNear( layerPt.x(), x, mPrecision / 3 ) && qgsDoubleNear( layerPt.y(), y, mPrecision / 3 ) )
57-
{
58-
painter->setPen( mCurrentPointPen );
59-
}
60-
else
53+
for ( int y = gridYMin ; y < gridYMax; y += mPrecision )
6154
{
62-
painter->setPen( mGridPen );
55+
const QgsPointXY pt = mTransform.transform( x, y );
56+
const QPointF canvasPt = toCanvasCoordinates( pt );
57+
58+
if ( qgsDoubleNear( layerPt.x(), x, mPrecision / 3 ) && qgsDoubleNear( layerPt.y(), y, mPrecision / 3 ) )
59+
{
60+
painter->setPen( mCurrentPointPen );
61+
}
62+
else
63+
{
64+
painter->setPen( mGridPen );
65+
}
66+
painter->drawLine( canvasPt.x() - 3, canvasPt.y(), canvasPt.x() + 3, canvasPt.y() );
67+
painter->drawLine( canvasPt.x(), canvasPt.y() - 3, canvasPt.x(), canvasPt.y() + 3 );
68+
6369
}
64-
painter->drawLine( canvasPt.x() - 3, canvasPt.y(), canvasPt.x() + 3, canvasPt.y() );
65-
painter->drawLine( canvasPt.x(), canvasPt.y() - 3, canvasPt.x(), canvasPt.y() + 3 );
6670
}
6771
}
72+
catch ( QgsCsException &e )
73+
{
74+
Q_UNUSED( e )
75+
mAvailableByZoomFactor = false;
76+
}
6877
}
6978

7079
painter->restore();

0 commit comments

Comments
 (0)