QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgssnaptogridcanvasitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssnaptogridcanvasitem.cpp
3 ----------------------
4 begin : August 2018
5 copyright : (C) Matthias Kuhn
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgssnaptogridcanvasitem.cpp"
18#include "qgsmapcanvas.h"
19#include "qgsrendercontext.h"
20
22 : QgsMapCanvasItem( mapCanvas )
23{
24 updateMapCanvasCrs();
25 connect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, &QgsSnapToGridCanvasItem::updateZoomFactor );
26 connect( mMapCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsSnapToGridCanvasItem::updateMapCanvasCrs );
27}
28
29void QgsSnapToGridCanvasItem::paint( QPainter *painter )
30{
31 if ( !mEnabled || !mAvailableByZoomFactor )
32 return;
33
34 const QgsScopedQPainterState painterState( painter );
35 const QgsRectangle mapRect = mMapCanvas->extent();
36
37 painter->setRenderHints( QPainter::Antialiasing );
38 painter->setCompositionMode( QPainter::CompositionMode_Difference );
39
40 const double scaleFactor = painter->fontMetrics().xHeight() * .2;
41
42 mGridPen.setWidth( scaleFactor );
43 mCurrentPointPen.setWidth( scaleFactor * 3 );
44 const int gridMarkerLength = scaleFactor * 3;
45
46 try
47 {
48 QgsCoordinateTransform extentTransform = mTransform;
49 extentTransform.setBallparkTransformsAreAppropriate( true );
50 const QgsRectangle layerExtent = extentTransform.transformBoundingBox( mapRect, Qgis::TransformDirection::Reverse );
51 const QgsPointXY layerPt = mTransform.transform( mPoint, Qgis::TransformDirection::Reverse );
52
53 const double gridXMin = std::ceil( layerExtent.xMinimum() / mPrecision ) * mPrecision;
54 const double gridXMax = std::ceil( layerExtent.xMaximum() / mPrecision ) * mPrecision;
55 const double gridYMin = std::ceil( layerExtent.yMinimum() / mPrecision ) * mPrecision;
56 const double gridYMax = std::ceil( layerExtent.yMaximum() / mPrecision ) * mPrecision;
57
58 for ( double x = gridXMin ; x < gridXMax; x += mPrecision )
59 {
60 for ( double y = gridYMin ; y < gridYMax; y += mPrecision )
61 {
62 const QgsPointXY pt = mTransform.transform( x, y );
63 const QPointF canvasPt = toCanvasCoordinates( pt );
64
65 if ( qgsDoubleNear( layerPt.x(), x, mPrecision / 2 ) && qgsDoubleNear( layerPt.y(), y, mPrecision / 2 ) )
66 {
67 painter->setPen( mCurrentPointPen );
68 }
69 else
70 {
71 painter->setPen( mGridPen );
72 }
73 painter->drawLine( canvasPt.x() - gridMarkerLength, canvasPt.y(), canvasPt.x() + gridMarkerLength, canvasPt.y() );
74 painter->drawLine( canvasPt.x(), canvasPt.y() - gridMarkerLength, canvasPt.x(), canvasPt.y() + gridMarkerLength );
75
76 }
77 }
78 }
79 catch ( QgsCsException &e )
80 {
81 Q_UNUSED( e )
82 mAvailableByZoomFactor = false;
83 }
84}
85
87{
88 return mPoint;
89}
90
92{
93 mPoint = point;
94 update();
95}
96
98{
99 return mPrecision;
100}
101
103{
104 mPrecision = precision;
105 updateZoomFactor();
106}
107
109{
110 return mTransform.sourceCrs();
111}
112
114{
115 mTransform.setSourceCrs( crs );
116 updateZoomFactor();
117}
118
120{
121 return mEnabled;
122}
123
125{
126 mEnabled = enabled;
127 update();
128}
129
130void QgsSnapToGridCanvasItem::updateMapCanvasCrs()
131{
134 update();
135}
136
137
138
139void QgsSnapToGridCanvasItem::updateZoomFactor()
140{
141 if ( !isVisible() )
142 return;
143
144 try
145 {
146 const int threshold = 5;
147
148 const QgsRectangle extent = mMapCanvas->extent();
149 if ( extent != rect() )
150 setRect( extent );
151
152 const QgsPointXY centerPoint = mMapCanvas->extent().center();
153 const QPointF canvasCenter = toCanvasCoordinates( centerPoint );
154
155 const QgsPointXY pt1 = mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( static_cast<int>( canvasCenter.x() - threshold ),
156 static_cast<int>( canvasCenter.y() - threshold ) );
157 const QgsPointXY pt2 = mMapCanvas->mapSettings().mapToPixel().toMapCoordinates( static_cast<int>( canvasCenter.x() + threshold ),
158 static_cast<int>( canvasCenter.y() + threshold ) );
159
160 const QgsPointXY layerPt1 = mTransform.transform( pt1, Qgis::TransformDirection::Reverse );
161 const QgsPointXY layerPt2 = mTransform.transform( pt2, Qgis::TransformDirection::Reverse );
162
163 const double dist = layerPt1.distance( layerPt2 );
164
165 if ( dist < mPrecision )
166 mAvailableByZoomFactor = true;
167 else
168 mAvailableByZoomFactor = false;
169 }
170 catch ( QgsCsException & )
171 {
172 // transform errors?
173 // you've probably got worse problems than the grid with your digitizing operations in the current projection.
174 mAvailableByZoomFactor = false;
175 }
176}
@ Reverse
Reverse/inverse transform (from destination to source)
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from.
void setContext(const QgsCoordinateTransformContext &context)
Sets the context in which the coordinate transform should be calculated.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs)
Sets the source coordinate reference system.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination coordinate reference system.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
An abstract class for items that can be placed on the map canvas.
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
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.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
const QgsMapToPixel & mapToPixel() const
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...
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
A class to represent a 2D point.
Definition qgspointxy.h:60
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition qgspointxy.h:206
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
A rectangle specified with double values.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
QgsPointXY center() const
Returns the center point of the rectangle.
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)
Definition qgis.h:5917
const QgsCoordinateReferenceSystem & crs
int precision