QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
17
19#include "qgsgeometryoptions.h"
20#include "qgsmapcanvas.h"
21#include "qgsmapmouseevent.h"
23#include "qgsvectorlayer.h"
24
25#include "moc_qgsmaptooladvanceddigitizing.cpp"
26
34
36
38{
39 if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
40 {
41 mCadDockWidget->applyConstraints( e ); // updates event's map point
42 mCadDockWidget->processCanvasPressEvent( e );
43 if ( !e->isAccepted() )
44 {
45 return; // The dock widget has taken the event
46 }
47 }
48 else if ( isAutoSnapEnabled() )
49 {
50 e->snapPoint();
51 }
52
54 if ( mSnapToLayerGridEnabled && layer )
55 {
56 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
57 }
58
60}
61
63{
64 if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
65 {
66 mCadDockWidget->processCanvasReleaseEvent( e );
67 if ( !e->isAccepted() )
68 {
69 return; // The dock widget has taken the event
70 }
71 }
72 else if ( isAutoSnapEnabled() )
73 {
74 e->snapPoint();
75 }
76
78 if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
79 {
80 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
81 }
82
84}
85
87{
88 if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
89 {
90 mCadDockWidget->applyConstraints( e ); // updates event's map point
91 mCadDockWidget->processCanvasMoveEvent( e );
92 if ( !e->isAccepted() )
93 {
94 return; // The dock widget has taken the event
95 }
96 }
97 else if ( isAutoSnapEnabled() )
98 {
99 e->snapPoint();
100 }
101
103 if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
104 {
105 e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
106 mSnapToGridCanvasItem->setPoint( e->mapPoint() );
107 }
108
109 if ( mSnapIndicator )
110 {
111 mSnapIndicator->setMatch( e->mapPointMatch() );
112 }
113
115}
116
118{
120 connect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
121 mCadDockWidget->enable();
122 mSnapToGridCanvasItem = new QgsSnapToGridCanvasItem( mCanvas );
124 if ( layer )
125 {
126 mSnapToGridCanvasItem->setCrs( currentVectorLayer()->crs() );
127 mSnapToGridCanvasItem->setPrecision( currentVectorLayer()->geometryOptions()->geometryPrecision() );
128 }
129 mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
130}
131
133{
135 disconnect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
136 mCadDockWidget->disable();
137 delete mSnapToGridCanvasItem;
138 mSnapToGridCanvasItem = nullptr;
139
140 if ( mSnapIndicator )
141 mSnapIndicator->setMatch( QgsPointLocator::Match() );
142}
143
148
150{
151 return static_cast<bool>( mSnapIndicator.get() );
152}
153
155{
156 if ( enabled && !mSnapIndicator )
157 {
158 mSnapIndicator = std::make_unique<QgsSnapIndicator>( mCanvas );
159 }
160 else if ( !enabled && mSnapIndicator )
161 {
162 mSnapIndicator.reset();
163 }
164}
165
166void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
167{
168 Q_UNUSED( point )
169 QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
170 qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
171}
172
173void QgsMapToolAdvancedDigitizing::onCurrentLayerChanged()
174{
175 if ( mSnapToGridCanvasItem )
176 {
177 QgsVectorLayer *layer = currentVectorLayer();
178 if ( layer && mSnapToLayerGridEnabled )
179 {
180 mSnapToGridCanvasItem->setPrecision( layer->geometryOptions()->geometryPrecision() );
181 mSnapToGridCanvasItem->setCrs( layer->crs() );
182 }
183 if ( !layer || !layer->isSpatial() )
184 {
185 mCadDockWidget->clear();
186 mCadDockWidget->disable();
187 mSnapToGridCanvasItem->setEnabled( false );
188 }
189 else
190 {
191 mCadDockWidget->enable();
192 mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
193 }
194 }
195}
196
198{
199 return mSnapToLayerGridEnabled;
200}
201
203{
204 mSnapToLayerGridEnabled = snapToGridEnabled;
205
206 if ( mSnapToGridCanvasItem )
207 {
208 mSnapToGridCanvasItem->setEnabled( snapToGridEnabled );
209 }
210}
A dockable widget used to handle the CAD tools on top of a selection of map tools.
void pointChangedV2(const QgsPoint &point)
Sometimes a constraint may change the current point out of a mouse event.
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:80
A mouse event which is the result of a user interaction with 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.
QgsMapToolEdit(QgsMapCanvas *canvas)
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:360
friend class QgsMapCanvas
Definition qgsmaptool.h:380
virtual void activate()
called when set as currently active map tool
virtual void deactivate()
called when map tool is being deactivated
Represents a 2D point.
Definition qgspointxy.h:60
Shows a grid on the map canvas given a spatial resolution.
Represents a vector layer which manages a vector based dataset.