QGIS API Documentation 3.99.0-Master (c22de0620c0)
Loading...
Searching...
No Matches
qgsmaptooldigitizefeature.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptooldigitizefeature.cpp
3
4 ---------------------
5 begin : 7.12.2017
6 copyright : (C) 2017 by David Signer
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
20#include "qgsfields.h"
21#include "qgsgeometry.h"
22#include "qgsmapcanvas.h"
23#include "qgsmapmouseevent.h"
24#include "qgsproject.h"
28#include "qgsvectorlayer.h"
29
30#include <QSettings>
31
32#include "moc_qgsmaptooldigitizefeature.cpp"
33
42
47
63
65{
66 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
67 if ( !vlayer )
68 vlayer = currentVectorLayer();
69
70 if ( !vlayer )
71 return;
72
73 QgsFeature f( vlayer->fields(), 0 );
74
75 if ( vlayer->isSpatial() )
76 {
77 const Qgis::WkbType layerWKBType = vlayer->wkbType();
78
79 QgsGeometry layerGeometry;
80
81 if ( mCheckGeometryType )
82 {
85 QVector<QgsGeometry> layerGeometries = geometry.coerceToType( layerWKBType, defaultZ, defaultM, true );
86 if ( layerGeometries.count() > 0 )
87 layerGeometry = layerGeometries.at( 0 );
88
89 if ( layerGeometry.wkbType() != layerWKBType && layerGeometry.wkbType() != QgsWkbTypes::linearType( layerWKBType ) )
90 {
91 const QString coerceError = geometry.lastError();
92 if ( !coerceError.isEmpty() )
93 {
95 }
96 else
97 {
98 emit messageEmitted( tr( "The digitized geometry type (%1) does not correspond to the layer geometry type (%2)." ).arg( QgsWkbTypes::displayString( layerGeometry.wkbType() ), QgsWkbTypes::displayString( layerWKBType ) ), Qgis::MessageLevel::Warning );
99 }
100 return;
101 }
102 }
103 else
104 {
105 layerGeometry = geometry;
106 }
107 f.setGeometry( layerGeometry );
108 }
109 f.setValid( true );
110 emit digitizingCompleted( f );
111 featureDigitized( f );
112}
113
115{
116 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
117 if ( !vlayer )
118 vlayer = currentVectorLayer();
119
120 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
121 {
122 setCursor( QCursor( Qt::ArrowCursor ) );
124 return;
125 }
126
127 if ( mLayer )
128 {
129 //remember current layer
130 mCurrentLayer = mCanvas->currentLayer();
131 //set the layer with the given
132 mCanvas->setCurrentLayer( mLayer );
133 }
134
136}
137
139{
141
142 if ( mCurrentLayer )
143 //set the layer back to the one remembered
144 mCanvas->setCurrentLayer( mCurrentLayer );
145 emit digitizingFinished();
146}
147
149{
150 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
151 if ( !vlayer )
152 vlayer = currentVectorLayer();
153
154 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
155 {
157 }
158}
159
161{
162 if ( e->key() == Qt::Key_Escape )
163 {
164 emit digitizingCanceled();
165 }
167}
168
170{
171 return mCheckGeometryType;
172}
173
178
180{
181 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
182
183 if ( !vlayer )
184 //if no given layer take the current from canvas
185 vlayer = currentVectorLayer();
186
187 if ( !vlayer )
188 {
190 return;
191 }
192
193 QgsVectorDataProvider *provider = vlayer->dataProvider();
194
196 {
197 emit messageEmitted( tr( "The data provider for this layer does not support the addition of features." ), Qgis::MessageLevel::Warning );
198 return;
199 }
200
201 if ( !vlayer->isEditable() )
202 {
204 return;
205 }
206
207 //check we only use this tool for point/multipoint layers
208 if ( mode() == CapturePoint && vlayer->geometryType() != Qgis::GeometryType::Point && mCheckGeometryType )
209 {
210 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture point' tool on this vector layer" ), Qgis::MessageLevel::Warning );
211 return;
212 }
213
214 //check we only use the line tool for line/multiline layers
215 if ( mode() == CaptureLine && vlayer->geometryType() != Qgis::GeometryType::Line && mCheckGeometryType )
216 {
217 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture line' tool on this vector layer" ), Qgis::MessageLevel::Warning );
218 return;
219 }
220
221 //check we only use the polygon tool for polygon/multipolygon layers
222 if ( mode() == CapturePolygon && vlayer->geometryType() != Qgis::GeometryType::Polygon && mCheckGeometryType )
223 {
224 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture polygon' tool on this vector layer" ), Qgis::MessageLevel::Warning );
225 return;
226 }
227
229}
230
232{
233 mLayer = vl;
234}
@ AddFeatures
Allows adding features.
Definition qgis.h:522
CaptureTechnique
Capture technique.
Definition qgis.h:415
@ NurbsCurve
Digitizes NURBS curves with control points (curve is attracted to but does not pass through control p...
Definition qgis.h:421
@ Shape
Digitize shapes.
Definition qgis.h:419
@ StraightSegments
Default capture mode - capture occurs with straight line segments.
Definition qgis.h:416
@ CircularString
Capture in circular strings.
Definition qgis.h:417
@ Streaming
Streaming points digitizing mode (points are automatically added as the mouse cursor moves).
Definition qgis.h:418
@ PolyBezier
Digitizes poly-Bézier curves with anchors and tangent handles (curve passes through anchor points).
Definition qgis.h:420
@ Warning
Warning message.
Definition qgis.h:160
@ Point
Points.
Definition qgis.h:377
@ Line
Lines.
Definition qgis.h:378
@ Polygon
Polygons.
Definition qgis.h:379
@ Null
No geometry.
Definition qgis.h:381
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:291
A dockable widget used to handle the CAD tools on top of a selection of map tools.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
void setValid(bool validity)
Sets the validity of the feature.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
QVector< QgsGeometry > coerceToType(Qgis::WkbType type, double defaultZ=0, double defaultM=0, bool avoidDuplicates=true) const
Attempts to coerce this geometry into the specified destination type.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
Map canvas is a class for displaying all GIS data types on a canvas.
Base class for all map layer types.
Definition qgsmaplayer.h:83
A mouse event which is the result of a user interaction with a QgsMapCanvas.
QgsAdvancedDigitizingDockWidget * cadDockWidget() const
virtual void cadCanvasReleaseEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
QgsMapToolCaptureLayerGeometry(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode)
Constructor.
void stopCapturing()
Stop capturing.
CaptureMode mode() const
The capture mode.
QFlags< Capability > Capabilities
CaptureMode
Different capture modes.
@ CapturePolygon
Capture polygons.
@ CapturePoint
Capture points.
@ CaptureLine
Capture lines.
@ ValidateGeometries
Tool supports geometry validation.
@ SupportsCurves
Supports curved geometries input.
void cadCanvasReleaseEvent(QgsMapMouseEvent *e) override
Override this method when subclassing this class.
void setCheckGeometryType(bool checkGeometryType)
Check if CaptureMode matches layer type.
void layerGeometryCaptured(const QgsGeometry &geometry) final
Called when the feature has been digitized.
void digitizingCanceled()
Emitted when the digitizing process was interrupted by the user.
void digitizingCompleted(const QgsFeature &feature)
Emitted whenever the digitizing has been successfully completed.
bool checkGeometryType() const
Check if CaptureMode matches layer type.
void digitizingFinished()
Emitted whenever the digitizing has been ended without digitizing any feature.
virtual void featureDigitized(const QgsFeature &feature)
Called when the feature has been digitized.
void keyPressEvent(QKeyEvent *e) override
Intercept key events like Esc or Del to delete the last point.
void activate() override
Registers this maptool with the cad dock widget.
void setLayer(QgsMapLayer *vl)
Change the layer edited by the map tool.
bool supportsTechnique(Qgis::CaptureTechnique technique) const override
Returns true if the tool supports the specified capture technique.
void deactivate() override
Unregisters this maptool from the cad dock widget.
void reactivate() override
Called when the map tool is being activated while it is already active.
QgsMapToolDigitizeFeature(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget, CaptureMode mode=QgsMapToolCapture::CaptureNone)
QgsMapToolDigitizeFeature is a map tool to digitize a feature geometry.
QgsMapToolCapture::Capabilities capabilities() const override
Returns flags containing the supported capabilities.
void notifyNotVectorLayer()
Display a timed message bar noting the active layer is not vector.
void notifyNotEditableLayer()
Display a timed message bar noting the active vector layer is not editable.
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:369
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:387
void messageEmitted(const QString &message, Qgis::MessageLevel level=Qgis::MessageLevel::Info)
Emitted when a message should be shown to the user in the application message bar.
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
virtual void activate()
called when set as currently active map tool
virtual void deactivate()
called when map tool is being deactivated
static QgsProject * instance()
Returns the QgsProject singleton instance.
void cleared()
Emitted when the project is cleared (and additionally when an open project is cleared just before a n...
void readProject(const QDomDocument &document)
Emitted when a project is being read.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultMValue
Settings entry digitizing default m value.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultZValue
Settings entry digitizing default z value.
Base class for vector data providers.
virtual Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based dataset.
bool isEditable() const final
Returns true if the provider is in editing mode.
bool isSpatial() const final
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
Q_INVOKABLE Qgis::WkbType wkbType() const final
Returns the WKBType or WKBUnknown in case of error.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
static Qgis::WkbType linearType(Qgis::WkbType type)
Returns the linear type for a WKB type.
static Q_INVOKABLE QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...