QGIS API Documentation 3.39.0-Master (be2050b798e)
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 "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
39
41 return; // decided to eat the event and not pass it to the map tool (construction mode)
42 }
43 else if ( isAutoSnapEnabled() )
44 {
45 e->snapPoint();
46 }
47
49 if ( mSnapToLayerGridEnabled && layer )
50 {
51 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
52 }
53
55}
56
58{
60 {
61 if ( e->button() == Qt::RightButton )
62 {
64 }
65 else
66 {
67 mCadDockWidget->applyConstraints( e ); // updates event's map point
68
70 {
71 // Parallel or perpendicular mode and snapped to segment: do not pass the event to map tool
72 return;
73 }
74
76
78
80 return; // decided to eat the event and not pass it to the map tool (construction mode)
81 }
82 }
83 else if ( isAutoSnapEnabled() )
84 {
85 e->snapPoint();
86 }
87
89 if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
90 {
91 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
92 }
93
95}
96
98{
100 {
101 mCadDockWidget->applyConstraints( e ); // updates event's map point
102
103 // perpendicular/parallel constraint
104 // do a soft lock when snapping to a segment
107 }
108 else if ( isAutoSnapEnabled() )
109 {
110 e->snapPoint();
111 }
112
114 if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
115 {
116 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
117 mSnapToGridCanvasItem->setPoint( e->mapPoint() );
118 }
119
120 if ( mSnapIndicator )
121 {
122 mSnapIndicator->setMatch( e->mapPointMatch() );
123 }
124
126}
127
129{
131 connect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
133 mSnapToGridCanvasItem = new QgsSnapToGridCanvasItem( mCanvas );
135 if ( layer )
136 {
137 mSnapToGridCanvasItem->setCrs( currentVectorLayer()->crs() );
138 mSnapToGridCanvasItem->setPrecision( currentVectorLayer()->geometryOptions()->geometryPrecision() );
139 }
140 mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
141}
142
144{
146 disconnect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
148 delete mSnapToGridCanvasItem;
149 mSnapToGridCanvasItem = nullptr;
150
151 if ( mSnapIndicator )
152 mSnapIndicator->setMatch( QgsPointLocator::Match() );
153}
154
159
161{
162 return static_cast< bool >( mSnapIndicator.get() );
163}
164
166{
167 if ( enabled && !mSnapIndicator )
168 {
169 mSnapIndicator = std::make_unique< QgsSnapIndicator >( mCanvas );
170 }
171 else if ( !enabled && mSnapIndicator )
172 {
173 mSnapIndicator.reset();
174 }
175}
176
177void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
178{
179 Q_UNUSED( point )
180 QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
181 qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
182}
183
184void QgsMapToolAdvancedDigitizing::onCurrentLayerChanged()
185{
186 if ( mSnapToGridCanvasItem )
187 {
189 if ( layer && mSnapToLayerGridEnabled )
190 {
191 mSnapToGridCanvasItem->setPrecision( layer->geometryOptions()->geometryPrecision() );
192 mSnapToGridCanvasItem->setCrs( layer->crs() );
193 }
194
195 if ( !layer )
196 mSnapToGridCanvasItem->setEnabled( false );
197 else
198 mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
199 }
200}
201
203{
204 return mSnapToLayerGridEnabled;
205}
206
208{
209 mSnapToLayerGridEnabled = snapToGridEnabled;
210
211 if ( mSnapToGridCanvasItem )
212 {
213 mSnapToGridCanvasItem->setEnabled( snapToGridEnabled );
214 }
215}
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 addPoint(const QgsPointXY &point)
Adds point to the CAD point list.
void releaseLocks(bool releaseRepeatingLocks=true)
unlock all constraints
bool constructionMode() const
Returns whether the construction mode is activated.
void updateCadPaintItem()
Updates canvas item that displays constraints on the ma.
void pointChangedV2(const QgsPoint &point)
Sometimes a constraint may change the current point out of a mouse event.
void enable()
Enables the tool (call this when an appropriate map tool is set and in the condition to make use of c...
bool alignToSegment(QgsMapMouseEvent *e, QgsAdvancedDigitizingDockWidget::CadConstraint::LockMode lockMode=QgsAdvancedDigitizingDockWidget::CadConstraint::HardLock)
align to segment for between line constraint.
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:75
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:82
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