QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
qgsmaptooladvanceddigitizing.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptooladvanceddigitizing.cpp - map tool with event in map coordinates
3 ----------------------
4 begin : October 2014
5 copyright : (C) Denis Rouzaud
6 email : denis.rouzaud@gmail.com
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
16#include "qgsmapmouseevent.h"
18#include "qgsmapcanvas.h"
20#include "qgsvectorlayer.h"
21#include "qgsgeometryoptions.h"
23
25 : QgsMapToolEdit( canvas )
26 , mCadDockWidget( cadDockWidget )
27{
28 Q_ASSERT( cadDockWidget );
29 connect( canvas, &QgsMapCanvas::currentLayerChanged, this, &QgsMapToolAdvancedDigitizing::onCurrentLayerChanged );
30}
31
33
35{
37 {
38 mCadDockWidget->applyConstraints( e ); // updates event's map point
40 if ( !e->isAccepted() )
41 {
42 return; // The dock widget has taken the event
43 }
44 }
45 else if ( isAutoSnapEnabled() )
46 {
47 e->snapPoint();
48 }
49
51 if ( mSnapToLayerGridEnabled && layer )
52 {
53 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
54 }
55
57}
58
60{
62 {
64 if ( !e->isAccepted() )
65 {
66 return; // The dock widget has taken the event
67 }
68 }
69 else if ( isAutoSnapEnabled() )
70 {
71 e->snapPoint();
72 }
73
75 if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
76 {
77 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
78 }
79
81}
82
84{
86 {
87 mCadDockWidget->applyConstraints( e ); // updates event's map point
89 if ( !e->isAccepted() )
90 {
91 return; // The dock widget has taken the event
92 }
93 }
94 else if ( isAutoSnapEnabled() )
95 {
96 e->snapPoint();
97 }
98
100 if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
101 {
102 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
103 mSnapToGridCanvasItem->setPoint( e->mapPoint() );
104 }
105
106 if ( mSnapIndicator )
107 {
108 mSnapIndicator->setMatch( e->mapPointMatch() );
109 }
110
112}
113
115{
117 connect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
119 mSnapToGridCanvasItem = new QgsSnapToGridCanvasItem( mCanvas );
121 if ( layer )
122 {
123 mSnapToGridCanvasItem->setCrs( currentVectorLayer()->crs() );
124 mSnapToGridCanvasItem->setPrecision( currentVectorLayer()->geometryOptions()->geometryPrecision() );
125 }
126 mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
127}
128
130{
132 disconnect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
134 delete mSnapToGridCanvasItem;
135 mSnapToGridCanvasItem = nullptr;
136
137 if ( mSnapIndicator )
138 mSnapIndicator->setMatch( QgsPointLocator::Match() );
139}
140
145
147{
148 return static_cast< bool >( mSnapIndicator.get() );
149}
150
152{
153 if ( enabled && !mSnapIndicator )
154 {
155 mSnapIndicator = std::make_unique< QgsSnapIndicator >( mCanvas );
156 }
157 else if ( !enabled && mSnapIndicator )
158 {
159 mSnapIndicator.reset();
160 }
161}
162
163void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
164{
165 Q_UNUSED( point )
166 QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
167 qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
168}
169
170void QgsMapToolAdvancedDigitizing::onCurrentLayerChanged()
171{
172 if ( mSnapToGridCanvasItem )
173 {
175 if ( layer && mSnapToLayerGridEnabled )
176 {
177 mSnapToGridCanvasItem->setPrecision( layer->geometryOptions()->geometryPrecision() );
178 mSnapToGridCanvasItem->setCrs( layer->crs() );
179 }
180
181 if ( !layer )
182 mSnapToGridCanvasItem->setEnabled( false );
183 else
184 mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
185 }
186}
187
189{
190 return mSnapToLayerGridEnabled;
191}
192
194{
195 mSnapToLayerGridEnabled = snapToGridEnabled;
196
197 if ( mSnapToGridCanvasItem )
198 {
199 mSnapToGridCanvasItem->setEnabled( snapToGridEnabled );
200 }
201}
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
bool cadEnabled() const
determines if CAD tools are enabled or if map tools behaves "nomally"
bool applyConstraints(QgsMapMouseEvent *e)
apply the CAD constraints.
void processCanvasReleaseEvent(QgsMapMouseEvent *event)
Processes the canvas release event.
void pointChangedV2(const QgsPoint &point)
Sometimes a constraint may change the current point out of a mouse event.
void processCanvasPressEvent(QgsMapMouseEvent *event)
Processes the canvas press event.
void processCanvasMoveEvent(QgsMapMouseEvent *event)
Processes the canvas move event.
void enable()
Enables the tool (call this when an appropriate map tool is set and in the condition to make use of c...
Map canvas is a class for displaying all GIS data types on a canvas.
void currentLayerChanged(QgsMapLayer *layer)
Emitted when the current layer is changed.
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
Base class for all map layer types.
Definition qgsmaplayer.h:76
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
QgsPointXY mapPoint() const
mapPoint returns the point in coordinates
QgsPointLocator::Match mapPointMatch() const
Returns the matching data from the most recently snapped point.
void snapToGrid(double precision, const QgsCoordinateReferenceSystem &crs)
Snaps the mapPoint to a grid with the given precision.
QgsPointXY snapPoint()
snapPoint will snap the points using the map canvas snapping utils configuration
virtual void cadCanvasMoveEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
bool isAutoSnapEnabled() const
Returns whether mouse events (press/move/release) should automatically try to snap mouse position (ac...
QgsAdvancedDigitizingDockWidget * mCadDockWidget
bool useSnappingIndicator() const
Returns whether the snapping indicator should automatically be used.
void deactivate() override
Unregisters this maptool from the cad dock widget.
bool snapToLayerGridEnabled() const
Enables or disables snap to grid of mouse events.
void canvasMoveEvent(QgsMapMouseEvent *e) override
Catch the mouse move event, filters it, transforms it to map coordinates and send it to virtual metho...
virtual void cadCanvasPressEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
virtual QgsMapLayer * layer() const
Returns the layer associated with the map tool.
QgsAdvancedDigitizingDockWidget * cadDockWidget() const
~QgsMapToolAdvancedDigitizing() override
void setSnapToLayerGridEnabled(bool snapToLayerGridEnabled)
Enables or disables snap to grid of mouse events.
void canvasPressEvent(QgsMapMouseEvent *e) override
Catch the mouse press event, filters it, transforms it to map coordinates and send it to virtual meth...
void setUseSnappingIndicator(bool enabled)
Sets whether a snapping indicator should automatically be used.
virtual void cadCanvasReleaseEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Catch the mouse release event, filters it, transforms it to map coordinates and send it to virtual me...
QgsMapToolAdvancedDigitizing(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget)
Creates an advanced digitizing maptool.
void activate() override
Registers this maptool with the cad dock widget.
bool isAdvancedDigitizingAllowed() const
Returns whether functionality of advanced digitizing dock widget is currently allowed.
Base class for map tools that edit vector geometry.
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:341
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
virtual void activate()
called when set as currently active map tool
virtual void deactivate()
called when map tool is being deactivated
A class to represent a 2D point.
Definition qgspointxy.h:60
Shows a grid on the map canvas given a spatial resolution.
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.
void setPrecision(double precision)
The resolution of the grid in map units.
Represents a vector layer which manages a vector based data sets.
const QgsCoordinateReferenceSystem & crs