QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
61
63{
64 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
65 if ( !vlayer )
66 vlayer = currentVectorLayer();
67
68 if ( !vlayer )
69 return;
70
71 QgsFeature f( vlayer->fields(), 0 );
72
73 if ( vlayer->isSpatial() )
74 {
75 const Qgis::WkbType layerWKBType = vlayer->wkbType();
76
77 QgsGeometry layerGeometry;
78
79 if ( mCheckGeometryType )
80 {
83 QVector<QgsGeometry> layerGeometries = geometry.coerceToType( layerWKBType, defaultZ, defaultM );
84 if ( layerGeometries.count() > 0 )
85 layerGeometry = layerGeometries.at( 0 );
86
87 if ( layerGeometry.wkbType() != layerWKBType && layerGeometry.wkbType() != QgsWkbTypes::linearType( layerWKBType ) )
88 {
89 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 );
90 return;
91 }
92 }
93 else
94 {
95 layerGeometry = geometry;
96 }
97 f.setGeometry( layerGeometry );
98 }
99 f.setValid( true );
100 emit digitizingCompleted( f );
101 featureDigitized( f );
102}
103
105{
106 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
107 if ( !vlayer )
108 vlayer = currentVectorLayer();
109
110 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
111 {
112 setCursor( QCursor( Qt::ArrowCursor ) );
114 return;
115 }
116
117 if ( mLayer )
118 {
119 //remember current layer
120 mCurrentLayer = mCanvas->currentLayer();
121 //set the layer with the given
122 mCanvas->setCurrentLayer( mLayer );
123 }
124
126}
127
129{
131
132 if ( mCurrentLayer )
133 //set the layer back to the one remembered
134 mCanvas->setCurrentLayer( mCurrentLayer );
135 emit digitizingFinished();
136}
137
139{
140 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
141 if ( !vlayer )
142 vlayer = currentVectorLayer();
143
144 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
145 {
147 }
148}
149
151{
152 if ( e->key() == Qt::Key_Escape )
153 {
154 emit digitizingCanceled();
155 }
157}
158
160{
161 return mCheckGeometryType;
162}
163
168
170{
171 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
172
173 if ( !vlayer )
174 //if no given layer take the current from canvas
175 vlayer = currentVectorLayer();
176
177 if ( !vlayer )
178 {
180 return;
181 }
182
183 QgsVectorDataProvider *provider = vlayer->dataProvider();
184
186 {
187 emit messageEmitted( tr( "The data provider for this layer does not support the addition of features." ), Qgis::MessageLevel::Warning );
188 return;
189 }
190
191 if ( !vlayer->isEditable() )
192 {
194 return;
195 }
196
197 //check we only use this tool for point/multipoint layers
198 if ( mode() == CapturePoint && vlayer->geometryType() != Qgis::GeometryType::Point && mCheckGeometryType )
199 {
200 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture point' tool on this vector layer" ), Qgis::MessageLevel::Warning );
201 return;
202 }
203
204 //check we only use the line tool for line/multiline layers
205 if ( mode() == CaptureLine && vlayer->geometryType() != Qgis::GeometryType::Line && mCheckGeometryType )
206 {
207 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture line' tool on this vector layer" ), Qgis::MessageLevel::Warning );
208 return;
209 }
210
211 //check we only use the polygon tool for polygon/multipolygon layers
212 if ( mode() == CapturePolygon && vlayer->geometryType() != Qgis::GeometryType::Polygon && mCheckGeometryType )
213 {
214 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture polygon' tool on this vector layer" ), Qgis::MessageLevel::Warning );
215 return;
216 }
217
219}
220
222{
223 mLayer = vl;
224}
@ AddFeatures
Allows adding features.
Definition qgis.h:501
CaptureTechnique
Capture technique.
Definition qgis.h:397
@ Shape
Digitize shapes.
Definition qgis.h:401
@ StraightSegments
Default capture mode - capture occurs with straight line segments.
Definition qgis.h:398
@ CircularString
Capture in circular strings.
Definition qgis.h:399
@ Streaming
Streaming points digitizing mode (points are automatically added as the mouse cursor moves).
Definition qgis.h:400
@ Warning
Warning message.
Definition qgis.h:158
@ Point
Points.
Definition qgis.h:359
@ Line
Lines.
Definition qgis.h:360
@ Polygon
Polygons.
Definition qgis.h:361
@ Null
No geometry.
Definition qgis.h:363
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
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:58
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.
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:80
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:360
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:378
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...