QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsmaptoolcapturelayergeometry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptoolcapturelayergeometry.cpp - base class for map tools digitizing layer geometries
3 ---------------------
4 begin : January 2022
5 copyright : (C) Denis Rouzaud
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
18#include "qgsproject.h"
19#include "qgscurvepolygon.h"
20#include "qgscurve.h"
21
22
23
24
25void QgsMapToolCaptureLayerGeometry::geometryCaptured( const QgsGeometry &geometry )
26{
27 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer() );
28 if ( !vlayer )
29 return;
30
31 QgsGeometry g( geometry );
32
33 switch ( mode() )
34 {
37 break;
40 //does compoundcurve contain circular strings?
41 //does provider support circular strings?
42 const bool hasCurvedSegments = captureCurve()->hasCurvedSegments();
43 const bool providerSupportsCurvedSegments = vlayer && ( vlayer->dataProvider()->capabilities() & QgsVectorDataProvider::CircularGeometries );
44 if ( !hasCurvedSegments || !providerSupportsCurvedSegments )
45 g = QgsGeometry( g.constGet()->segmentize() );
46
47 QList<QgsVectorLayer *> avoidIntersectionsLayers;
48 switch ( QgsProject::instance()->avoidIntersectionsMode() )
49 {
51 if ( vlayer )
52 avoidIntersectionsLayers.append( vlayer );
53 break;
55 avoidIntersectionsLayers = QgsProject::instance()->avoidIntersectionsLayers();
56 break;
58 break;
59 }
60 if ( avoidIntersectionsLayers.size() > 0 )
61 {
62 const int avoidIntersectionsReturn = g.avoidIntersections( avoidIntersectionsLayers );
63 if ( avoidIntersectionsReturn == 3 )
64 {
65 emit messageEmitted( tr( "The feature has been added, but at least one geometry intersected is invalid. These geometries must be manually repaired." ), Qgis::MessageLevel::Warning );
66 }
67 if ( g.isEmpty() ) //avoid intersection might have removed the whole geometry
68 {
69 emit messageEmitted( tr( "The feature cannot be added because its geometry collapsed due to intersection avoidance" ), Qgis::MessageLevel::Critical );
71 return;
72 }
73 }
74 break;
75 }
76
77 layerGeometryCaptured( g );
78
79 switch ( mode() )
80 {
82 break;
84 layerPointCaptured( *qgsgeometry_cast<const QgsPoint *>( g.constGet() ) );
85 break;
87 layerLineCaptured( qgsgeometry_cast<const QgsCurve *>( g.constGet() ) );
88 break;
90 layerPolygonCaptured( qgsgeometry_cast<const QgsCurvePolygon *>( g.constGet() ) );
91 break;
92 }
93}
@ AvoidIntersectionsLayers
Overlap with features from a specified list of layers when digitizing new features not allowed.
@ AllowIntersections
Overlap with any feature allowed when digitizing new features.
@ AvoidIntersectionsCurrentLayer
Overlap with features from the active layer when digitizing new features not allowed.
bool hasCurvedSegments() const override
Returns true if the geometry contains curved segments.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
virtual QgsMapLayer * layer() const
Returns the layer associated with the map tool.
void stopCapturing()
Stop capturing.
CaptureMode mode() const
The capture mode.
@ CapturePolygon
Capture polygons.
@ CaptureNone
Do not capture / determine mode from layer geometry type.
@ CapturePoint
Capture points.
@ CaptureLine
Capture lines.
const QgsCompoundCurve * captureCurve() const
Gets the capture curve.
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:477
QList< QgsVectorLayer * > avoidIntersectionsLayers
Definition: qgsproject.h:116
@ CircularGeometries
Supports circular geometry types (circularstring, compoundcurve, curvepolygon)
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based data sets.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.