|
| 1 | +#include "qgssnaptogridcanvasitem.h" |
| 2 | +#include "qgsmapcanvas.h" |
| 3 | + |
| 4 | +QgsSnapToGridCanvasItem::QgsSnapToGridCanvasItem( QgsMapCanvas *mapCanvas ) |
| 5 | + : QgsMapCanvasItem( mapCanvas ) |
| 6 | + , mGridPen( QPen( QColor( 127, 127, 127, 150 ), 1 ) ) |
| 7 | + , mCurrentPointPen( QPen( QColor( 200, 200, 200, 150 ), 3 ) ) |
| 8 | +{ |
| 9 | + updateMapCanvasCrs(); |
| 10 | + connect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, &QgsSnapToGridCanvasItem::updateZoomFactor ); |
| 11 | + connect( mMapCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsSnapToGridCanvasItem::updateMapCanvasCrs ); |
| 12 | +} |
| 13 | + |
| 14 | +void QgsSnapToGridCanvasItem::paint( QPainter *painter ) |
| 15 | +{ |
| 16 | + painter->save(); |
| 17 | + QgsRectangle mapRect = mMapCanvas->extent(); |
| 18 | + if ( rect() != mapRect ) |
| 19 | + setRect( mapRect ); |
| 20 | + |
| 21 | + painter->setRenderHints( QPainter::Antialiasing ); |
| 22 | + painter->setCompositionMode( QPainter::CompositionMode_Difference ); |
| 23 | + |
| 24 | + if ( mEnabled && mAvailableByZoomFactor ) |
| 25 | + { |
| 26 | + const QgsRectangle layerExtent = mTransform.transformBoundingBox( mapRect, QgsCoordinateTransform::ReverseTransform ); |
| 27 | + const QgsPointXY layerPt = mTransform.transform( mPoint, QgsCoordinateTransform::ReverseTransform ); |
| 28 | + |
| 29 | + const double gridXMin = std::ceil( layerExtent.xMinimum() / mPrecision ) * mPrecision; |
| 30 | + const double gridXMax = std::ceil( layerExtent.xMaximum() / mPrecision ) * mPrecision; |
| 31 | + const double gridYMin = std::ceil( layerExtent.yMinimum() / mPrecision ) * mPrecision; |
| 32 | + const double gridYMax = std::ceil( layerExtent.yMaximum() / mPrecision ) * mPrecision; |
| 33 | + |
| 34 | + for ( int x = gridXMin ; x < gridXMax; x += mPrecision ) |
| 35 | + { |
| 36 | + for ( int y = gridYMin ; y < gridYMax; y += mPrecision ) |
| 37 | + { |
| 38 | + const QgsPointXY pt = mTransform.transform( x, y ); |
| 39 | + const QPointF canvasPt = toCanvasCoordinates( pt ); |
| 40 | + |
| 41 | + if ( qgsDoubleNear( layerPt.x(), x, mPrecision / 3 ) && qgsDoubleNear( layerPt.y(), y, mPrecision / 3 ) ) |
| 42 | + { |
| 43 | + painter->setPen( mCurrentPointPen ); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + painter->setPen( mGridPen ); |
| 48 | + } |
| 49 | + painter->drawLine( canvasPt.x() - 3, canvasPt.y(), canvasPt.x() + 3, canvasPt.y() ); |
| 50 | + painter->drawLine( canvasPt.x(), canvasPt.y() - 3, canvasPt.x(), canvasPt.y() + 3 ); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + painter->restore(); |
| 56 | +} |
| 57 | + |
| 58 | +QgsPointXY QgsSnapToGridCanvasItem::point() const |
| 59 | +{ |
| 60 | + return mPoint; |
| 61 | +} |
| 62 | + |
| 63 | +void QgsSnapToGridCanvasItem::setPoint( const QgsPointXY &point ) |
| 64 | +{ |
| 65 | + mPoint = point; |
| 66 | + update(); |
| 67 | +} |
| 68 | + |
| 69 | +double QgsSnapToGridCanvasItem::precision() const |
| 70 | +{ |
| 71 | + return mPrecision; |
| 72 | +} |
| 73 | + |
| 74 | +void QgsSnapToGridCanvasItem::setPrecision( double precision ) |
| 75 | +{ |
| 76 | + mPrecision = precision; |
| 77 | + updateZoomFactor(); |
| 78 | +} |
| 79 | + |
| 80 | +QgsCoordinateReferenceSystem QgsSnapToGridCanvasItem::crs() const |
| 81 | +{ |
| 82 | + return mTransform.sourceCrs(); |
| 83 | +} |
| 84 | + |
| 85 | +void QgsSnapToGridCanvasItem::setCrs( const QgsCoordinateReferenceSystem &crs ) |
| 86 | +{ |
| 87 | + mTransform.setSourceCrs( crs ); |
| 88 | + updateZoomFactor(); |
| 89 | +} |
| 90 | + |
| 91 | +bool QgsSnapToGridCanvasItem::enabled() const |
| 92 | +{ |
| 93 | + return mEnabled; |
| 94 | +} |
| 95 | + |
| 96 | +void QgsSnapToGridCanvasItem::setEnabled( bool enabled ) |
| 97 | +{ |
| 98 | + mEnabled = enabled; |
| 99 | + update(); |
| 100 | +} |
| 101 | + |
| 102 | +void QgsSnapToGridCanvasItem::updateMapCanvasCrs() |
| 103 | +{ |
| 104 | + mTransform.setContext( mMapCanvas->mapSettings().transformContext() ); |
| 105 | + mTransform.setDestinationCrs( mMapCanvas->mapSettings().destinationCrs() ); |
| 106 | + update(); |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +void QgsSnapToGridCanvasItem::updateZoomFactor() |
| 112 | +{ |
| 113 | + if ( !isVisible() ) |
| 114 | + return; |
| 115 | + |
| 116 | + try |
| 117 | + { |
| 118 | + const int threshold = 5; |
| 119 | + |
| 120 | + const QgsPointXY centerPoint = mMapCanvas->extent().center(); |
| 121 | + const QPointF canvasCenter = toCanvasCoordinates( centerPoint ); |
| 122 | + |
| 123 | + const QgsPointXY pt1 = mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( canvasCenter.x() - threshold, canvasCenter.y() - threshold ); |
| 124 | + const QgsPointXY pt2 = mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( canvasCenter.x() + threshold, canvasCenter.y() + threshold ); |
| 125 | + |
| 126 | + const QgsPointXY layerPt1 = mTransform.transform( pt1, QgsCoordinateTransform::ReverseTransform ); |
| 127 | + const QgsPointXY layerPt2 = mTransform.transform( pt2, QgsCoordinateTransform::ReverseTransform ); |
| 128 | + |
| 129 | + const double dist = layerPt1.distance( layerPt2 ); |
| 130 | + |
| 131 | + if ( dist < mPrecision ) |
| 132 | + mAvailableByZoomFactor = true; |
| 133 | + else |
| 134 | + mAvailableByZoomFactor = false; |
| 135 | + } |
| 136 | + catch ( QgsCsException &e ) |
| 137 | + { |
| 138 | + // transform errors? |
| 139 | + // you've probably got worse problems than the grid with your digitizing operations in the current projection. |
| 140 | + mAvailableByZoomFactor = false; |
| 141 | + } |
| 142 | +} |
0 commit comments