Skip to content
Permalink
Browse files

Early exit if snap to grid item is disabled

  • Loading branch information
m-kuhn committed Aug 27, 2018
1 parent ac8eab2 commit 3744043a684d8bb0c5ec9d52b9c77dac8de13aee
Showing with 30 additions and 30 deletions.
  1. +30 −30 src/gui/qgssnaptogridcanvasitem.cpp
@@ -28,6 +28,9 @@ QgsSnapToGridCanvasItem::QgsSnapToGridCanvasItem( QgsMapCanvas *mapCanvas )

void QgsSnapToGridCanvasItem::paint( QPainter *painter )
{
if ( !mEnabled || !mAvailableByZoomFactor )
return;

painter->save();
QgsRectangle mapRect = mMapCanvas->extent();
if ( rect() != mapRect )
@@ -36,44 +39,41 @@ void QgsSnapToGridCanvasItem::paint( QPainter *painter )
painter->setRenderHints( QPainter::Antialiasing );
painter->setCompositionMode( QPainter::CompositionMode_Difference );

if ( mEnabled && mAvailableByZoomFactor )
try
{
try
{
const QgsRectangle layerExtent = mTransform.transformBoundingBox( mapRect, QgsCoordinateTransform::ReverseTransform );
const QgsPointXY layerPt = mTransform.transform( mPoint, QgsCoordinateTransform::ReverseTransform );
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 x = gridXMin ; x < gridXMax; x += mPrecision )
{
for ( int y = gridYMin ; y < gridYMax; y += mPrecision )
{
for ( int y = gridYMin ; y < gridYMax; y += 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
{
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 );
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 );

}
}
catch ( QgsCsException &e )
{
Q_UNUSED( e )
mAvailableByZoomFactor = false;
}
}
catch ( QgsCsException &e )
{
Q_UNUSED( e )
mAvailableByZoomFactor = false;
}

painter->restore();

0 comments on commit 3744043

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