QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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::pointChanged, 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::pointChanged, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
141  delete mSnapToGridCanvasItem;
142  mSnapToGridCanvasItem = nullptr;
143 }
144 
145 void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
146 {
147  Q_UNUSED( point )
148  QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
149  qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
150 }
151 
152 void QgsMapToolAdvancedDigitizing::onCurrentLayerChanged()
153 {
154  if ( mSnapToGridCanvasItem )
155  {
157  if ( layer && mSnapToLayerGridEnabled )
158  {
159  mSnapToGridCanvasItem->setPrecision( layer->geometryOptions()->geometryPrecision() );
160  mSnapToGridCanvasItem->setCrs( layer->crs() );
161  }
162 
163  if ( !layer )
164  mSnapToGridCanvasItem->setEnabled( false );
165  else
166  mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
167  }
168 }
169 
171 {
172  return mSnapToLayerGridEnabled;
173 }
174 
176 {
177  mSnapToLayerGridEnabled = snapToGridEnabled;
178 
179  if ( mSnapToGridCanvasItem )
180  {
181  mSnapToGridCanvasItem->setEnabled( snapToGridEnabled );
182  }
183 }
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:264
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:89
QgsMapCanvas::currentLayerChanged
void currentLayerChanged(QgsMapLayer *layer)
Emitted when the current layer is changed.
QgsGeometryOptions::geometryPrecision
double geometryPrecision() const
The precision in which geometries on this layer should be saved.
Definition: qgsgeometryoptions.cpp:40
qgsmaptooladvanceddigitizing.h
QgsMapMouseEvent::mapPoint
QgsPointXY mapPoint() const
mapPoint returns the point in coordinates
Definition: qgsmapmouseevent.h:88
QgsMapToolAdvancedDigitizing::mCadDockWidget
QgsAdvancedDigitizingDockWidget * mCadDockWidget
Definition: qgsmaptooladvanceddigitizing.h:111
qgsmapcanvas.h
QgsMapToolAdvancedDigitizing::isAdvancedDigitizingAllowed
bool isAdvancedDigitizingAllowed() const
Returns whether functionality of advanced digitizing dock widget is currently allowed.
Definition: qgsmaptooladvanceddigitizing.h:80
QgsMapToolAdvancedDigitizing::setSnapToLayerGridEnabled
void setSnapToLayerGridEnabled(bool snapToLayerGridEnabled)
Enables or disables snap to grid of mouse events.
Definition: qgsmaptooladvanceddigitizing.cpp:175
QgsMapCanvas::mouseLastXY
QPoint mouseLastXY()
returns last position of mouse cursor
Definition: qgsmapcanvas.cpp:2374
QgsMapToolAdvancedDigitizing::canvasMoveEvent
void canvasMoveEvent(QgsMapMouseEvent *e) override
Catch the mouse move event, filters it, transforms it to map coordinates and send it to virtual metho...
Definition: qgsmaptooladvanceddigitizing.cpp:95
QgsMapMouseEvent::snapPoint
QgsPointXY snapPoint()
snapPoint will snap the points using the map canvas snapping utils configuration
Definition: qgsmapmouseevent.cpp:43
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsVectorLayer::geometryOptions
QgsGeometryOptions * geometryOptions() const
Configuration and logic to apply automatically on any edit happening on this layer.
Definition: qgsvectorlayer.cpp:5595
QgsMapToolAdvancedDigitizing::QgsMapToolAdvancedDigitizing
QgsMapToolAdvancedDigitizing(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget)
Creates an advanced digitizing maptool.
Definition: qgsmaptooladvanceddigitizing.cpp:24
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsMapToolAdvancedDigitizing::activate
void activate() override
Registers this maptool with the cad dock widget.
Definition: qgsmaptooladvanceddigitizing.cpp:121
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:96
QgsAdvancedDigitizingDockWidget::releaseLocks
void releaseLocks(bool releaseRepeatingLocks=true)
unlock all constraints
Definition: qgsadvanceddigitizingdockwidget.cpp:341
QgsMapTool::canvas
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
Definition: qgsmaptool.cpp:195
QgsAdvancedDigitizingDockWidget::alignToSegment
bool alignToSegment(QgsMapMouseEvent *e, QgsAdvancedDigitizingDockWidget::CadConstraint::LockMode lockMode=QgsAdvancedDigitizingDockWidget::CadConstraint::HardLock)
align to segment for additional constraint.
Definition: qgsadvanceddigitizingdockwidget.cpp:800
QgsAdvancedDigitizingDockWidget::enable
void enable()
Enables the tool (call this when an appropriate map tool is set and in the condition to make use of c...
Definition: qgsadvanceddigitizingdockwidget.cpp:1104
QgsSnapToGridCanvasItem::setPrecision
void setPrecision(double precision)
The resolution of the grid in map units.
Definition: qgssnaptogridcanvasitem.cpp:98
QgsMapToolEdit::currentVectorLayer
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
Definition: qgsmaptooledit.cpp:94
QgsAdvancedDigitizingDockWidget::disable
void disable()
Disable the widget.
Definition: qgsadvanceddigitizingdockwidget.cpp:1130
QgsAdvancedDigitizingDockWidget::pointChanged
void pointChanged(const QgsPointXY &point)
Sometimes a constraint may change the current point out of a mouse event.
QgsMapToolAdvancedDigitizing::isAutoSnapEnabled
bool isAutoSnapEnabled() const
Returns whether mouse events (press/move/release) should automatically try to snap mouse position (ac...
Definition: qgsmaptooladvanceddigitizing.h:90
qgssnaptogridcanvasitem.h
QgsMapToolAdvancedDigitizing::canvasPressEvent
void canvasPressEvent(QgsMapMouseEvent *e) override
Catch the mouse press event, filters it, transforms it to map coordinates and send it to virtual meth...
Definition: qgsmaptooladvanceddigitizing.cpp:32
QgsSnapToGridCanvasItem::setEnabled
void setEnabled(bool enabled)
Enable this item.
Definition: qgssnaptogridcanvasitem.cpp:120
QgsMapToolAdvancedDigitizing::canvasReleaseEvent
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Catch the mouse release event, filters it, transforms it to map coordinates and send it to virtual me...
Definition: qgsmaptooladvanceddigitizing.cpp:55
QgsMapMouseEvent::snapToGrid
void snapToGrid(double precision, const QgsCoordinateReferenceSystem &crs)
Snaps the mapPoint to a grid with the given precision.
Definition: qgsmapmouseevent.cpp:74
QgsAdvancedDigitizingDockWidget::constructionMode
bool constructionMode() const
construction mode is used to draw intermediate points. These points won't be given any further (i....
Definition: qgsadvanceddigitizingdockwidget.h:254
QgsAdvancedDigitizingDockWidget::applyConstraints
bool applyConstraints(QgsMapMouseEvent *e)
apply the CAD constraints.
Definition: qgsadvanceddigitizingdockwidget.cpp:631
QgsAdvancedDigitizingDockWidget::clear
void clear()
Clear any cached previous clicks and helper lines.
Definition: qgsadvanceddigitizingdockwidget.cpp:872
QgsAdvancedDigitizingDockWidget
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
Definition: qgsadvanceddigitizingdockwidget.h:49
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:80
QgsAdvancedDigitizingDockWidget::CadConstraint::SoftLock
@ SoftLock
Definition: qgsadvanceddigitizingdockwidget.h:105
qgsvectorlayer.h
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsAdvancedDigitizingDockWidget::updateCadPaintItem
void updateCadPaintItem()
Updates canvas item that displays constraints on the ma.
Definition: qgsadvanceddigitizingdockwidget.cpp:1144
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Definition: qgsmapmouseevent.h:36
qgsadvanceddigitizingdockwidget.h
QgsMapToolEdit
Base class for map tools that edit vector geometry.
Definition: qgsmaptooledit.h:33
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMapToolAdvancedDigitizing::cadCanvasMoveEvent
virtual void cadCanvasMoveEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
Definition: qgsmaptooladvanceddigitizing.h:145
QgsAdvancedDigitizingDockWidget::addPoint
void addPoint(const QgsPointXY &point)
Adds point to the CAD point list.
Definition: qgsadvanceddigitizingdockwidget.cpp:1149
QgsMapToolAdvancedDigitizing::snapToLayerGridEnabled
bool snapToLayerGridEnabled() const
Enables or disables snap to grid of mouse events.
Definition: qgsmaptooladvanceddigitizing.cpp:170
QgsMapToolAdvancedDigitizing::cadDockWidget
QgsAdvancedDigitizingDockWidget * cadDockWidget() const
Definition: qgsmaptooladvanceddigitizing.h:65
QgsMapToolAdvancedDigitizing::deactivate
void deactivate() override
Unregisters this maptool from the cad dock widget.
Definition: qgsmaptooladvanceddigitizing.cpp:136
qgsgeometryoptions.h
QgsSnapToGridCanvasItem::setPoint
void setPoint(const QgsPointXY &point)
A point that will be highlighted on the map canvas.
Definition: qgssnaptogridcanvasitem.cpp:87
QgsSnapToGridCanvasItem
Shows a grid on the map canvas given a spatial resolution.
Definition: qgssnaptogridcanvasitem.h:42
qgsmapmouseevent.h
QgsMapToolAdvancedDigitizing::cadCanvasPressEvent
virtual void cadCanvasPressEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
Definition: qgsmaptooladvanceddigitizing.h:123
QgsMapToolAdvancedDigitizing::cadCanvasReleaseEvent
virtual void cadCanvasReleaseEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
Definition: qgsmaptooladvanceddigitizing.h:134
QgsAdvancedDigitizingDockWidget::cadEnabled
bool cadEnabled() const
determines if CAD tools are enabled or if map tools behaves "nomally"
Definition: qgsadvanceddigitizingdockwidget.h:251
QgsSnapToGridCanvasItem::setCrs
void setCrs(const QgsCoordinateReferenceSystem &crs)
The CRS in which the grid should be calculated.
Definition: qgssnaptogridcanvasitem.cpp:109