QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
99 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 );
100 }
101 return;
102 }
103 }
104 else
105 {
106 layerGeometry = geometry;
107 }
108 f.setGeometry( layerGeometry );
109 }
110 f.setValid( true );
111 emit digitizingCompleted( f );
112 featureDigitized( f );
113}
114
116{
117 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
118 if ( !vlayer )
119 vlayer = currentVectorLayer();
120
121 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
122 {
123 setCursor( QCursor( Qt::ArrowCursor ) );
125 return;
126 }
127
128 if ( mLayer )
129 {
130 //remember current layer
131 mCurrentLayer = mCanvas->currentLayer();
132 //set the layer with the given
133 mCanvas->setCurrentLayer( mLayer );
134 }
135
137}
138
140{
142
143 if ( mCurrentLayer )
144 //set the layer back to the one remembered
145 mCanvas->setCurrentLayer( mCurrentLayer );
146 emit digitizingFinished();
147}
148
150{
151 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
152 if ( !vlayer )
153 vlayer = currentVectorLayer();
154
155 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
156 {
158 }
159}
160
162{
163 if ( e->key() == Qt::Key_Escape )
164 {
165 emit digitizingCanceled();
166 }
168}
169
171{
172 return mCheckGeometryType;
173}
174
179
181{
182 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
183
184 if ( !vlayer )
185 //if no given layer take the current from canvas
186 vlayer = currentVectorLayer();
187
188 if ( !vlayer )
189 {
191 return;
192 }
193
194 QgsVectorDataProvider *provider = vlayer->dataProvider();
195
197 {
198 emit messageEmitted( tr( "The data provider for this layer does not support the addition of features." ), Qgis::MessageLevel::Warning );
199 return;
200 }
201
202 if ( !vlayer->isEditable() )
203 {
205 return;
206 }
207
208 //check we only use this tool for point/multipoint layers
209 if ( mode() == CapturePoint && vlayer->geometryType() != Qgis::GeometryType::Point && mCheckGeometryType )
210 {
211 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture point' tool on this vector layer" ), Qgis::MessageLevel::Warning );
212 return;
213 }
214
215 //check we only use the line tool for line/multiline layers
216 if ( mode() == CaptureLine && vlayer->geometryType() != Qgis::GeometryType::Line && mCheckGeometryType )
217 {
218 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture line' tool on this vector layer" ), Qgis::MessageLevel::Warning );
219 return;
220 }
221
222 //check we only use the polygon tool for polygon/multipolygon layers
223 if ( mode() == CapturePolygon && vlayer->geometryType() != Qgis::GeometryType::Polygon && mCheckGeometryType )
224 {
225 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture polygon' tool on this vector layer" ), Qgis::MessageLevel::Warning );
226 return;
227 }
228
230}
231
233{
234 mLayer = vl;
235}
@ AddFeatures
Allows adding features.
Definition qgis.h:527
CaptureTechnique
Capture technique.
Definition qgis.h:418
@ NurbsCurve
Digitizes NURBS curves with control points (curve is attracted to but does not pass through control p...
Definition qgis.h:424
@ Shape
Digitize shapes.
Definition qgis.h:422
@ StraightSegments
Default capture mode - capture occurs with straight line segments.
Definition qgis.h:419
@ CircularString
Capture in circular strings.
Definition qgis.h:420
@ Streaming
Streaming points digitizing mode (points are automatically added as the mouse cursor moves).
Definition qgis.h:421
@ PolyBezier
Digitizes poly-Bézier curves with anchors and tangent handles (curve passes through anchor points).
Definition qgis.h:423
@ Warning
Warning message.
Definition qgis.h:162
@ Point
Points.
Definition qgis.h:380
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Null
No geometry.
Definition qgis.h:384
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
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...