21#include "moc_qgssnaptogridcanvasitem.cpp"
33 if ( !mEnabled || !mAvailableByZoomFactor )
39 painter->setRenderHints( QPainter::Antialiasing );
40 painter->setCompositionMode( QPainter::CompositionMode_Difference );
42 const double scaleFactor = painter->fontMetrics().xHeight() * .2;
44 mGridPen.setWidth( scaleFactor );
45 mCurrentPointPen.setWidth( scaleFactor * 3 );
46 const int gridMarkerLength = scaleFactor * 3;
55 const double gridXMin = std::ceil( layerExtent.
xMinimum() / mPrecision ) * mPrecision;
56 const double gridXMax = std::ceil( layerExtent.
xMaximum() / mPrecision ) * mPrecision;
57 const double gridYMin = std::ceil( layerExtent.
yMinimum() / mPrecision ) * mPrecision;
58 const double gridYMax = std::ceil( layerExtent.
yMaximum() / mPrecision ) * mPrecision;
60 for (
double x = gridXMin; x < gridXMax; x += mPrecision )
62 for (
double y = gridYMin; y < gridYMax; y += mPrecision )
64 const QgsPointXY pt = mTransform.transform( x, y );
69 painter->setPen( mCurrentPointPen );
73 painter->setPen( mGridPen );
75 painter->drawLine( canvasPt.x() - gridMarkerLength, canvasPt.y(), canvasPt.x() + gridMarkerLength, canvasPt.y() );
76 painter->drawLine( canvasPt.x(), canvasPt.y() - gridMarkerLength, canvasPt.x(), canvasPt.y() + gridMarkerLength );
83 mAvailableByZoomFactor =
false;
111 return mTransform.sourceCrs();
116 mTransform.setSourceCrs(
crs );
131void QgsSnapToGridCanvasItem::updateMapCanvasCrs()
139void QgsSnapToGridCanvasItem::updateZoomFactor()
146 const int threshold = 5;
148 const QgsRectangle extent =
mMapCanvas->extent();
149 if ( extent !=
rect() )
152 const QgsPointXY centerPoint =
mMapCanvas->extent().center();
155 const QgsPointXY pt1 =
mMapCanvas->mapSettings().mapToPixel().toMapCoordinates(
static_cast<int>( canvasCenter.x() - threshold ),
static_cast<int>( canvasCenter.y() - threshold ) );
156 const QgsPointXY pt2 =
mMapCanvas->mapSettings().mapToPixel().toMapCoordinates(
static_cast<int>( canvasCenter.x() + threshold ),
static_cast<int>( canvasCenter.y() + threshold ) );
161 const double dist = layerPt1.
distance( layerPt2 );
163 if ( dist < mPrecision )
164 mAvailableByZoomFactor =
true;
166 mAvailableByZoomFactor =
false;
168 catch ( QgsCsException & )
172 mAvailableByZoomFactor =
false;
@ Reverse
Reverse/inverse transform (from destination to source).
Represents a coordinate reference system (CRS).
Custom exception class for Coordinate Reference System related exceptions.
QgsRectangle rect() const
returns canvas item rectangle in map units
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
QgsMapCanvas * mMapCanvas
pointer to map canvas
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
QgsMapCanvasItem(QgsMapCanvas *mapCanvas)
protected constructor: cannot be constructed directly
Map canvas is a class for displaying all GIS data types on a canvas.
void extentsChanged()
Emitted when the extents of the map change.
void destinationCrsChanged()
Emitted when map CRS has changed.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
A rectangle specified with double values.
Scoped object for saving and restoring a QPainter object's state.
bool enabled() const
Enable this item.
double precision() const
The resolution of the grid in map units.
void setCrs(const QgsCoordinateReferenceSystem &crs)
The CRS in which the grid should be calculated.
void setPoint(const QgsPointXY &point)
A point that will be highlighted on the map canvas.
void setEnabled(bool enabled)
Enable this item.
QgsSnapToGridCanvasItem(QgsMapCanvas *mapCanvas)
Will automatically be added to the mapCanvas.
void paint(QPainter *painter) override
function to be implemented by derived classes
QgsCoordinateReferenceSystem crs() const
The CRS in which the grid should be calculated.
void setPrecision(double precision)
The resolution of the grid in map units.
QgsPointXY point() const
A point that will be highlighted on the map canvas.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).