QGIS API Documentation 3.99.0-Master (357b655ed83)
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
62
64{
65 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
66 if ( !vlayer )
67 vlayer = currentVectorLayer();
68
69 if ( !vlayer )
70 return;
71
72 QgsFeature f( vlayer->fields(), 0 );
73
74 if ( vlayer->isSpatial() )
75 {
76 const Qgis::WkbType layerWKBType = vlayer->wkbType();
77
78 QgsGeometry layerGeometry;
79
80 if ( mCheckGeometryType )
81 {
84 QVector<QgsGeometry> layerGeometries = geometry.coerceToType( layerWKBType, defaultZ, defaultM, true );
85 if ( layerGeometries.count() > 0 )
86 layerGeometry = layerGeometries.at( 0 );
87
88 if ( layerGeometry.wkbType() != layerWKBType && layerGeometry.wkbType() != QgsWkbTypes::linearType( layerWKBType ) )
89 {
90 const QString coerceError = geometry.lastError();
91 if ( !coerceError.isEmpty() )
92 {
94 }
95 else
96 {
97 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 );
98 }
99 return;
100 }
101 }
102 else
103 {
104 layerGeometry = geometry;
105 }
106 f.setGeometry( layerGeometry );
107 }
108 f.setValid( true );
109 emit digitizingCompleted( f );
110 featureDigitized( f );
111}
112
114{
115 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
116 if ( !vlayer )
117 vlayer = currentVectorLayer();
118
119 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
120 {
121 setCursor( QCursor( Qt::ArrowCursor ) );
123 return;
124 }
125
126 if ( mLayer )
127 {
128 //remember current layer
129 mCurrentLayer = mCanvas->currentLayer();
130 //set the layer with the given
131 mCanvas->setCurrentLayer( mLayer );
132 }
133
135}
136
138{
140
141 if ( mCurrentLayer )
142 //set the layer back to the one remembered
143 mCanvas->setCurrentLayer( mCurrentLayer );
144 emit digitizingFinished();
145}
146
148{
149 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
150 if ( !vlayer )
151 vlayer = currentVectorLayer();
152
153 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
154 {
156 }
157}
158
160{
161 if ( e->key() == Qt::Key_Escape )
162 {
163 emit digitizingCanceled();
164 }
166}
167
169{
170 return mCheckGeometryType;
171}
172
177
179{
180 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
181
182 if ( !vlayer )
183 //if no given layer take the current from canvas
184 vlayer = currentVectorLayer();
185
186 if ( !vlayer )
187 {
189 return;
190 }
191
192 QgsVectorDataProvider *provider = vlayer->dataProvider();
193
195 {
196 emit messageEmitted( tr( "The data provider for this layer does not support the addition of features." ), Qgis::MessageLevel::Warning );
197 return;
198 }
199
200 if ( !vlayer->isEditable() )
201 {
203 return;
204 }
205
206 //check we only use this tool for point/multipoint layers
207 if ( mode() == CapturePoint && vlayer->geometryType() != Qgis::GeometryType::Point && mCheckGeometryType )
208 {
209 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture point' tool on this vector layer" ), Qgis::MessageLevel::Warning );
210 return;
211 }
212
213 //check we only use the line tool for line/multiline layers
214 if ( mode() == CaptureLine && vlayer->geometryType() != Qgis::GeometryType::Line && mCheckGeometryType )
215 {
216 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture line' tool on this vector layer" ), Qgis::MessageLevel::Warning );
217 return;
218 }
219
220 //check we only use the polygon tool for polygon/multipolygon layers
221 if ( mode() == CapturePolygon && vlayer->geometryType() != Qgis::GeometryType::Polygon && mCheckGeometryType )
222 {
223 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture polygon' tool on this vector layer" ), Qgis::MessageLevel::Warning );
224 return;
225 }
226
228}
229
231{
232 mLayer = vl;
233}
@ AddFeatures
Allows adding features.
Definition qgis.h:520
CaptureTechnique
Capture technique.
Definition qgis.h:404
@ NurbsCurve
Digitizes NURBS curves with control points.
Definition qgis.h:409
@ Shape
Digitize shapes.
Definition qgis.h:408
@ StraightSegments
Default capture mode - capture occurs with straight line segments.
Definition qgis.h:405
@ CircularString
Capture in circular strings.
Definition qgis.h:406
@ Streaming
Streaming points digitizing mode (points are automatically added as the mouse cursor moves).
Definition qgis.h:407
@ Warning
Warning message.
Definition qgis.h:161
@ Point
Points.
Definition qgis.h:366
@ Line
Lines.
Definition qgis.h:367
@ Polygon
Polygons.
Definition qgis.h:368
@ Null
No geometry.
Definition qgis.h:370
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
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...