QGIS API Documentation 3.29.0-Master (da8bb1db43)
qgsmaptooledit.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptooledit.cpp - base class for editing map tools
3 ---------------------
4 begin : Juli 2007
5 copyright : (C) 2007 by Marco Hugentobler
6 email : marco dot hugentobler at karto dot baug dot ethz dot ch
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#include "qgsmaptooledit.h"
17#include "qgsproject.h"
18#include "qgsmapcanvas.h"
20#include "qgsrubberband.h"
21#include "qgsvectorlayer.h"
23
24#include <QKeyEvent>
25
26
28 : QgsMapTool( canvas )
29{
30 if ( mCanvas->project() )
31 {
32 connect( mCanvas->project(), &QgsProject::layersAdded, this, &QgsMapToolEdit::connectLayers );
33 connectLayers( mCanvas->project()->mapLayers().values() ); // Connect existing layers
34 }
35}
36
38{
40}
41
43{
45}
46
48{
50}
51
53{
55}
56
58{
60}
61
62
64{
65 QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
67 QColor color = digitizingStrokeColor();
68 if ( alternativeBand )
69 {
71 color.setAlphaF( color.alphaF() * alphaScale );
72 rb->setLineStyle( Qt::DotLine );
73 }
74 rb->setStrokeColor( color );
75
76 const QColor fillColor = digitizingFillColor();
77 rb->setFillColor( fillColor );
78
79 rb->show();
80 return rb;
81}
82
84{
85 return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
86}
87
88
90{
91 if ( !mCanvas )
92 {
94 }
95
96 //find out current vector layer
98
99 if ( !vlayer )
100 {
102 }
103
104 QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
105 for ( ; list_it != vertices.constEnd(); ++list_it )
106 {
107 vlayer->addTopologicalPoints( *list_it );
108 }
110}
111
113{
114 if ( !mCanvas )
115 {
117 }
118
119 //find out current vector layer
121
122 if ( !vlayer )
123 {
125 }
126
128 QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
129 for ( ; list_it != vertices.constEnd(); ++list_it )
130 {
131 vlayer->addTopologicalPoints( *list_it );
132 }
134
136}
137
139{
140 QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
142 if ( alternativeBand )
143 {
144 double alpha = color.alpha() / 255.0 * QgsSettingsRegistryCore::settingsDigitizingLineColorAlphaScale->value();
145 rb->setLineStyle( Qt::DotLine );
146 color.setAlphaF( alpha );
147 }
148 rb->setStrokeColor( color );
149 rb->setFillColor( color );
151 rb->show();
152 return rb;
153}
154
156{
157 emit messageEmitted( tr( "No active vector layer" ) );
158}
159
161{
162 emit messageEmitted( tr( "Layer not editable" ) );
163}
164
165void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
166{
167 for ( QgsMapLayer *layer : layers )
168 {
169 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
170 if ( vlayer )
171 {
172 connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
173 }
174 }
175}
176
177void QgsMapToolEdit::cleanCanvas()
178{
179 if ( editableVectorLayers().isEmpty() )
180 {
181 clean();
182 }
183}
184
185QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
186{
187 QList<QgsVectorLayer *> editableLayers;
188 if ( mCanvas->project() )
189 {
190 const auto layers = mCanvas->project()->mapLayers().values();
191 for ( QgsMapLayer *layer : layers )
192 {
193 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
194 if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
195 editableLayers << vlayer;
196 }
197 }
198 return editableLayers;
199}
A rubberband class for QgsAbstractGeometry (considering curved geometries).
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
void setStrokeWidth(int width)
Sets stroke width.
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
Base class for all map layer types.
Definition: qgsmaplayer.h:73
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
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.
Q_DECL_DEPRECATED TopologicalResult addTopologicalPoints(const QVector< QgsPointXY > &vertices)
Adds a list of vertices to other features to keep topology up to date, e.g.
QgsGeometryRubberBand * createGeometryRubberBand(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry, bool alternativeBand=false) const
Creates a geometry rubber band with the color/line width from the QGIS settings.
static double defaultMValue()
Returns default M value.
QgsMapToolEdit(QgsMapCanvas *canvas)
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
static QColor digitizingFillColor()
Returns fill color for rubber bands (from global settings)
static double defaultZValue()
Returns default Z value.
QgsRubberBand * createRubberBand(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry, bool alternativeBand=false)
Creates a rubber band with the color/line width from the QGIS settings.
static QColor digitizingStrokeColor()
Returns stroke color for rubber bands (from global settings)
static int digitizingStrokeWidth()
Returns stroke width for rubber bands (from global settings)
TopologicalResult
Result of addTopologicalPoints.
@ InvalidLayer
AddTopologicalPoints failed due to an invalid canvas.
@ InvalidCanvas
AddTopologicalPoints failed due to an invalid canvas.
@ Success
AddTopologicalPoints was successful.
Abstract base class for all map tools.
Definition: qgsmaptool.h:71
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:338
QgsMapLayer * layer(const QString &id)
Returns the map layer with the matching ID, or nullptr if no layers could be found.
Definition: qgsmaptool.cpp:84
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:120
void layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the registry.
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:52
void setWidth(int width)
Sets the width of the line.
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static const QgsSettingsEntryColor * settingsDigitizingFillColor
Settings entry digitizing fill color.
static const QgsSettingsEntryInteger * settingsDigitizingLineWidth
Settings entry digitizing line width.
static const QgsSettingsEntryDouble * settingsDigitizingLineColorAlphaScale
Settings entry digitizing line color alpha scale.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultMValue
Settings entry digitizing default m value.
static const QgsSettingsEntryColor * settingsDigitizingLineColor
Settings entry digitizing line color.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultZValue
Settings entry digitizing default z value.
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.
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:3674
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:3673