QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
7 email : david@opengis.ch
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#include "moc_qgsmaptooldigitizefeature.cpp"
20#include "qgsfields.h"
21#include "qgsgeometry.h"
22#include "qgsmapcanvas.h"
23#include "qgsmapmouseevent.h"
24#include "qgsproject.h"
27#include "qgsvectorlayer.h"
29
30#include <QSettings>
31
40
45
59
60void QgsMapToolDigitizeFeature::layerGeometryCaptured( const QgsGeometry &geometry )
61{
62 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
63 if ( !vlayer )
64 vlayer = currentVectorLayer();
65
66 if ( !vlayer )
67 return;
68
69 QgsFeature f( vlayer->fields(), 0 );
70
71 if ( vlayer->isSpatial() )
72 {
73 const Qgis::WkbType layerWKBType = vlayer->wkbType();
74
75 QgsGeometry layerGeometry;
76
77 if ( mCheckGeometryType )
78 {
81 QVector<QgsGeometry> layerGeometries = geometry.coerceToType( layerWKBType, defaultZ, defaultM );
82 if ( layerGeometries.count() > 0 )
83 layerGeometry = layerGeometries.at( 0 );
84
85 if ( layerGeometry.wkbType() != layerWKBType && layerGeometry.wkbType() != QgsWkbTypes::linearType( layerWKBType ) )
86 {
87 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 );
88 return;
89 }
90 }
91 else
92 {
93 layerGeometry = geometry;
94 }
95 f.setGeometry( layerGeometry );
96 }
97 f.setValid( true );
98 emit digitizingCompleted( f );
99 featureDigitized( f );
100}
101
103{
104 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
105 if ( !vlayer )
106 vlayer = currentVectorLayer();
107
108 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
109 {
110 setCursor( QCursor( Qt::ArrowCursor ) );
111 layerGeometryCaptured( QgsGeometry() );
112 return;
113 }
114
115 if ( mLayer )
116 {
117 //remember current layer
118 mCurrentLayer = mCanvas->currentLayer();
119 //set the layer with the given
120 mCanvas->setCurrentLayer( mLayer );
121 }
122
124}
125
127{
129
130 if ( mCurrentLayer )
131 //set the layer back to the one remembered
132 mCanvas->setCurrentLayer( mCurrentLayer );
133 emit digitizingFinished();
134}
135
137{
138 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
139 if ( !vlayer )
140 vlayer = currentVectorLayer();
141
142 if ( vlayer && vlayer->geometryType() == Qgis::GeometryType::Null )
143 {
144 layerGeometryCaptured( QgsGeometry() );
145 }
146}
147
149{
150 if ( e->key() == Qt::Key_Escape )
151 {
152 emit digitizingCanceled();
153 }
155}
156
158{
159 return mCheckGeometryType;
160}
161
163{
164 mCheckGeometryType = checkGeometryType;
165}
166
168{
169 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
170
171 if ( !vlayer )
172 //if no given layer take the current from canvas
173 vlayer = currentVectorLayer();
174
175 if ( !vlayer )
176 {
178 return;
179 }
180
181 QgsVectorDataProvider *provider = vlayer->dataProvider();
182
184 {
185 emit messageEmitted( tr( "The data provider for this layer does not support the addition of features." ), Qgis::MessageLevel::Warning );
186 return;
187 }
188
189 if ( !vlayer->isEditable() )
190 {
192 return;
193 }
194
195 //check we only use this tool for point/multipoint layers
196 if ( mode() == CapturePoint && vlayer->geometryType() != Qgis::GeometryType::Point && mCheckGeometryType )
197 {
198 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture point' tool on this vector layer" ), Qgis::MessageLevel::Warning );
199 return;
200 }
201
202 //check we only use the line tool for line/multiline layers
203 if ( mode() == CaptureLine && vlayer->geometryType() != Qgis::GeometryType::Line && mCheckGeometryType )
204 {
205 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture line' tool on this vector layer" ), Qgis::MessageLevel::Warning );
206 return;
207 }
208
209 //check we only use the polygon tool for polygon/multipolygon layers
210 if ( mode() == CapturePolygon && vlayer->geometryType() != Qgis::GeometryType::Polygon && mCheckGeometryType )
211 {
212 emit messageEmitted( tr( "Wrong editing tool, cannot apply the 'capture polygon' tool on this vector layer" ), Qgis::MessageLevel::Warning );
213 return;
214 }
215
217}
218
220{
221 mLayer = vl;
222}
223
@ AddFeatures
Allows adding features.
CaptureTechnique
Capture technique.
Definition qgis.h:376
@ Shape
Digitize shapes.
@ StraightSegments
Default capture mode - capture occurs with straight line segments.
@ CircularString
Capture in circular strings.
@ Streaming
Streaming points digitizing mode (points are automatically added as the mouse cursor moves).
@ Warning
Warning message.
Definition qgis.h:156
@ Polygon
Polygons.
@ Null
No geometry.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
A geometry is the spatial representation of a feature.
QVector< QgsGeometry > coerceToType(Qgis::WkbType type, double defaultZ=0, double defaultM=0) 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:76
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
virtual void cadCanvasReleaseEvent(QgsMapMouseEvent *e)
Override this method when subclassing this class.
QgsMapToolCaptureLayerGeometry is a base class for map tools digitizing layer geometries This map too...
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 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.
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.
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:341
virtual void setCursor(const QCursor &cursor)
Sets a user defined cursor.
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:359
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
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.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultMValue
Settings entry digitizing default m value.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultZValue
Settings entry digitizing default z value.
This is the 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 data sets.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
static Qgis::WkbType linearType(Qgis::WkbType type)
Returns the linear type for a WKB type.
static 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...