QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  connect( canvas, &QgsMapCanvas::currentLayerChanged, this, &QgsMapToolAdvancedDigitizing::onCurrentLayerChanged );
29 }
30 
32 {
34  {
35  mCadDockWidget->applyConstraints( e ); // updates event's map point
36 
38  return; // decided to eat the event and not pass it to the map tool (construction mode)
39  }
40  else if ( isAutoSnapEnabled() )
41  {
42  e->snapPoint();
43  }
44 
46  if ( mSnapToLayerGridEnabled && layer )
47  {
48  e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
49  }
50 
52 }
53 
55 {
57  {
58  if ( e->button() == Qt::RightButton )
59  {
61  }
62  else
63  {
64  mCadDockWidget->applyConstraints( e ); // updates event's map point
65 
66  if ( mCadDockWidget->alignToSegment( e ) )
67  {
68  // Parallel or perpendicular mode and snapped to segment: do not pass the event to map tool
69  return;
70  }
71 
73 
74  mCadDockWidget->releaseLocks( false );
75 
77  return; // decided to eat the event and not pass it to the map tool (construction mode)
78  }
79  }
80  else if ( isAutoSnapEnabled() )
81  {
82  e->snapPoint();
83  }
84 
86  if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
87  {
88  e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
89  }
90 
92 }
93 
95 {
97  {
98  mCadDockWidget->applyConstraints( e ); // updates event's map point
99 
100  // perpendicular/parallel constraint
101  // do a soft lock when snapping to a segment
104  }
105  else if ( isAutoSnapEnabled() )
106  {
107  e->snapPoint();
108  }
109 
111  if ( mSnapToGridCanvasItem && mSnapToLayerGridEnabled && layer )
112  {
113  e->snapToGrid( layer->geometryOptions()->geometryPrecision(), layer->crs() );
114  mSnapToGridCanvasItem->setPoint( e->mapPoint() );
115  }
116 
117  cadCanvasMoveEvent( e );
118 }
119 
121 {
123  connect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChanged, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
125  mSnapToGridCanvasItem = new QgsSnapToGridCanvasItem( mCanvas );
127  if ( layer )
128  {
129  mSnapToGridCanvasItem->setCrs( currentVectorLayer()->crs() );
130  mSnapToGridCanvasItem->setPrecision( currentVectorLayer()->geometryOptions()->geometryPrecision() );
131  }
132  mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
133 }
134 
136 {
138  disconnect( mCadDockWidget, &QgsAdvancedDigitizingDockWidget::pointChanged, this, &QgsMapToolAdvancedDigitizing::cadPointChanged );
140  delete mSnapToGridCanvasItem;
141  mSnapToGridCanvasItem = nullptr;
142 }
143 
144 void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
145 {
146  Q_UNUSED( point );
147  QMouseEvent *ev = new QMouseEvent( QEvent::MouseMove, mCanvas->mouseLastXY(), Qt::NoButton, Qt::NoButton, Qt::NoModifier );
148  qApp->postEvent( mCanvas->viewport(), ev ); // event queue will delete the event when processed
149 }
150 
151 void QgsMapToolAdvancedDigitizing::onCurrentLayerChanged()
152 {
153  if ( mSnapToGridCanvasItem )
154  {
156  if ( layer && mSnapToLayerGridEnabled )
157  {
158  mSnapToGridCanvasItem->setPrecision( layer->geometryOptions()->geometryPrecision() );
159  mSnapToGridCanvasItem->setCrs( layer->crs() );
160  }
161 
162  if ( !layer )
163  mSnapToGridCanvasItem->setEnabled( false );
164  else
165  mSnapToGridCanvasItem->setEnabled( mSnapToLayerGridEnabled );
166  }
167 }
168 
170 {
171  return mSnapToLayerGridEnabled;
172 }
173 
175 {
176  mSnapToLayerGridEnabled = snapToGridEnabled;
177 
178  if ( mSnapToGridCanvasItem )
179  {
180  mSnapToGridCanvasItem->setEnabled( snapToGridEnabled );
181  }
182 }
QgsMapToolAdvancedDigitizing(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget)
Creates an advanced digitizing maptool.
double geometryPrecision() const
The precision in which geometries on this layer should be saved.
void setCrs(const QgsCoordinateReferenceSystem &crs)
The CRS in which the grid should be calculated.
void setSnapToLayerGridEnabled(bool snapToLayerGridEnabled)
Enables or disables snap to grid of mouse events.
A class to represent a 2D point.
Definition: qgspointxy.h:43
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
void activate() override
Registers this maptool with the cad dock widget.
bool applyConstraints(QgsMapMouseEvent *e)
apply the CAD constraints.
QgsAdvancedDigitizingDockWidget * mCadDockWidget
const QgsCoordinateReferenceSystem & crs
void canvasMoveEvent(QgsMapMouseEvent *e) override
Catch the mouse move event, filters it, transforms it to map coordinates and send it to virtual metho...
void clear()
Clear any cached previous clicks and helper lines.
Base class for map tools that edit vector geometry.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
void releaseLocks(bool releaseRepeatingLocks=true)
unlock all constraints
void setPrecision(double precision)
The resolution of the grid in map units.
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:83
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:241
void canvasPressEvent(QgsMapMouseEvent *e) override
Catch the mouse press event, filters it, transforms it to map coordinates and send it to virtual meth...
Shows a grid on the map canvas given a spatial resolution.
void updateCadPaintItem()
Updates canvas item that displays constraints on the ma.
bool isAutoSnapEnabled() const
Returns whether mouse events (press/move/release) should automatically try to snap mouse position (ac...
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Catch the mouse release event, filters it, transforms it to map coordinates and send it to virtual me...
bool snapToLayerGridEnabled() const
Enables or disables snap to grid of mouse events.
void addPoint(const QgsPointXY &point)
Adds point to the CAD point list.
bool isAdvancedDigitizingAllowed() const
Returns whether functionality of advanced digitizing dock widget is currently allowed.
void setEnabled(bool enabled)
Enable this item.
QgsPointXY mapPoint() const
mapPoint returns the point in coordinates
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
void enable()
Enables the tool (call this when an appropriate map tool is set and in the condition to make use of c...
void currentLayerChanged(QgsMapLayer *layer)
Emitted when the current layer is changed.
void pointChanged(const QgsPointXY &point)
Sometimes a constraint may change the current point out of a mouse event.
QPoint mouseLastXY()
returns last position of mouse cursor
QgsGeometryOptions * geometryOptions() const
Configuration and logic to apply automatically on any edit happening on this layer.
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
void setPoint(const QgsPointXY &point)
A point that will be highlighted on the map canvas.
virtual void cadCanvasPressEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
virtual void cadCanvasMoveEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
bool constructionMode() const
construction mode is used to draw intermediate points. These points won't be given any further (i...
void snapToGrid(double precision, const QgsCoordinateReferenceSystem &crs)
Snaps the mapPoint to a grid with the given precision.
virtual void cadCanvasReleaseEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
QgsPointXY snapPoint()
snapPoint will snap the points using the map canvas snapping utils configuration
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
Represents a vector layer which manages a vector based data sets.
bool alignToSegment(QgsMapMouseEvent *e, QgsAdvancedDigitizingDockWidget::CadConstraint::LockMode lockMode=QgsAdvancedDigitizingDockWidget::CadConstraint::HardLock)
align to segment for additional constraint.
bool cadEnabled() const
determines if CAD tools are enabled or if map tools behaves "nomally"
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:70
void deactivate() override
Unregisters this maptool from the cad dock widget.