QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 : [email protected]
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  {
36  mCadDockWidget->applyConstraints( e ); // updates event's map point
37 
39  return; // decided to eat the event and not pass it to the map tool (construction mode)
40  }
41  else if ( isAutoSnapEnabled() )
42  {
43  e->snapPoint();
44  }
45 
47  if ( mSnapToLayerGridEnabled && layer )
48  {
49  e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
50  }
51 
53 }
54 
56 {
58  {
59  if ( e->button() == Qt::RightButton )
60  {
62  }
63  else
64  {
65  mCadDockWidget->applyConstraints( e ); // updates event's map point
66 
67  if ( mCadDockWidget->alignToSegment( e ) )
68  {
69  // Parallel or perpendicular mode and snapped to segment: do not pass the event to map tool
70  return;
71  }
72 
74 
75  mCadDockWidget->releaseLocks( false );
76 
78  return; // decided to eat the event and not pass it to the map tool (construction mode)
79  }
80  }
81  else if ( isAutoSnapEnabled() )
82  {
83  e->snapPoint();
84  }
85 
87  if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
88  {
89  e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
90  }
91 
93 }
94 
96 {
98  {
99  mCadDockWidget->applyConstraints( e ); // updates event's map point
100 
101  // perpendicular/parallel constraint
102  // do a soft lock when snapping to a segment
105  }
106  else if ( isAutoSnapEnabled() )
107  {
108  e->snapPoint();
109  }
110 
112  if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
113  {
114  e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
115  mSnapToGridCanvasItem->setPoint( e->mapPoint() );
116  }
117 
118  cadCanvasMoveEvent( e );
119 }
120 
122 {
124  connect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
126  mSnapToGridCanvasItem = new QgsSnapToGridCanvasItem( mCanvas );
128  if ( layer )
129  {
130  mSnapToGridCanvasItem->setCrs( currentVectorLayer()->crs() );
131  mSnapToGridCanvasItem->setPrecision( currentVectorLayer()->geometryOptions()->geometryPrecision() );
132  }
133  mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
134 }
135 
137 {
139  disconnect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChangedV2, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
141  delete mSnapToGridCanvasItem;
142  mSnapToGridCanvasItem = nullptr;
143 }
144 
146 {
147  return canvas()->currentLayer();
148 }
149 
150 void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
151 {
152  Q_UNUSED( point )
153  QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
154  qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
155 }
156 
157 void QgsMapToolAdvancedDigitizing::onCurrentLayerChanged()
158 {
159  if ( mSnapToGridCanvasItem )
160  {
162  if ( layer && mSnapToLayerGridEnabled )
163  {
164  mSnapToGridCanvasItem->setPrecision( layer->geometryOptions()->geometryPrecision() );
165  mSnapToGridCanvasItem->setCrs( layer->crs() );
166  }
167 
168  if ( !layer )
169  mSnapToGridCanvasItem->setEnabled( false );
170  else
171  mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
172  }
173 }
174 
176 {
177  return mSnapToLayerGridEnabled;
178 }
179 
181 {
182  mSnapToLayerGridEnabled = snapToGridEnabled;
183 
184  if ( mSnapToGridCanvasItem )
185  {
186  mSnapToGridCanvasItem->setEnabled( snapToGridEnabled );
187  }
188 }
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
construction mode is used to draw intermediate points. These points won't be given any further (i....
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 additional 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.
Definition: qgsmapcanvas.h:89
QPoint mouseLastXY()
returns last position of mouse cursor
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:73
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
QgsPointXY mapPoint() const
mapPoint returns the point in coordinates
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
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.
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...
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...
QgsAdvancedDigitizingDockWidget * cadDockWidget() const
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.
QgsMapCanvas * mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:336
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:215
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:94
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:110
A class to represent a 2D point.
Definition: qgspointxy.h:59
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