QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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
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 "moc_qgsmaptooladvanceddigitizing.cpp"
19#include "qgsmapcanvas.h"
21#include "qgsvectorlayer.h"
22#include "qgsgeometryoptions.h"
24
26 : QgsMapToolEdit( canvas )
27 , mCadDockWidget( cadDockWidget )
28{
29 Q_ASSERT( cadDockWidget );
30 connect( canvas, &QgsMapCanvas::currentLayerChanged, this, &QgsMapToolAdvancedDigitizing::onCurrentLayerChanged );
31}
32
34
36{
38 {
39 mCadDockWidget->applyConstraints( e ); // updates event's map point
41 if ( !e->isAccepted() )
42 {
43 return; // The dock widget has taken the event
44 }
45 }
46 else if ( isAutoSnapEnabled() )
47 {
48 e->snapPoint();
49 }
50
52 if ( mSnapToLayerGridEnabled && layer )
53 {
54 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
55 }
56
58}
59
61{
63 {
65 if ( !e->isAccepted() )
66 {
67 return; // The dock widget has taken the event
68 }
69 }
70 else if ( isAutoSnapEnabled() )
71 {
72 e->snapPoint();
73 }
74
76 if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
77 {
78 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
79 }
80
82}
83
85{
87 {
88 mCadDockWidget->applyConstraints( e ); // updates event's map point
90 if ( !e->isAccepted() )
91 {
92 return; // The dock widget has taken the event
93 }
94 }
95 else if ( isAutoSnapEnabled() )
96 {
97 e->snapPoint();
98 }
99
101 if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
102 {
103 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
104 mSnapToGridCanvasItem->setPoint( e->mapPoint() );
105 }
106
107 if ( mSnapIndicator )
108 {
109 mSnapIndicator->setMatch( e->mapPointMatch() );
110 }
111
113}
114
116{
118 connect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
120 mSnapToGridCanvasItem = new QgsSnapToGridCanvasItem( mCanvas );
122 if ( layer )
123 {
124 mSnapToGridCanvasItem->setCrs( currentVectorLayer()->crs() );
125 mSnapToGridCanvasItem->setPrecision( currentVectorLayer()->geometryOptions()->geometryPrecision() );
126 }
127 mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
128}
129
131{
133 disconnect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
135 delete mSnapToGridCanvasItem;
136 mSnapToGridCanvasItem = nullptr;
137
138 if ( mSnapIndicator )
139 mSnapIndicator->setMatch( QgsPointLocator::Match() );
140}
141
146
148{
149 return static_cast< bool >( mSnapIndicator.get() );
150}
151
153{
154 if ( enabled && !mSnapIndicator )
155 {
156 mSnapIndicator = std::make_unique< QgsSnapIndicator >( mCanvas );
157 }
158 else if ( !enabled && mSnapIndicator )
159 {
160 mSnapIndicator.reset();
161 }
162}
163
164void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
165{
166 Q_UNUSED( point )
167 QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
168 qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
169}
170
171void QgsMapToolAdvancedDigitizing::onCurrentLayerChanged()
172{
173 if ( mSnapToGridCanvasItem )
174 {
176 if ( layer && mSnapToLayerGridEnabled )
177 {
178 mSnapToGridCanvasItem->setPrecision( layer->geometryOptions()->geometryPrecision() );
179 mSnapToGridCanvasItem->setCrs( layer->crs() );
180 }
181 if ( !layer || !layer->isSpatial() )
182 {
185 mSnapToGridCanvasItem->setEnabled( false );
186 }
187 else
188 {
190 mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
191 }
192 }
193}
194
196{
197 return mSnapToLayerGridEnabled;
198}
199
201{
202 mSnapToLayerGridEnabled = snapToGridEnabled;
203
204 if ( mSnapToGridCanvasItem )
205 {
206 mSnapToGridCanvasItem->setEnabled( snapToGridEnabled );
207 }
208}
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...
void clear()
Clear any cached previous clicks and helper lines.
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
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
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