QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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"
24
25#include <QKeyEvent>
26
27
29 : QgsMapTool( canvas )
30{
31 if ( mCanvas->project() )
32 {
33 connect( mCanvas->project(), &QgsProject::layersAdded, this, &QgsMapToolEdit::connectLayers );
34 connectLayers( mCanvas->project()->mapLayers().values() ); // Connect existing layers
35 }
36}
37
39{
41}
42
44{
46}
47
49{
51}
52
54{
56}
57
59{
61}
62
63
65{
66 QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
68 QColor color = digitizingStrokeColor();
69 if ( alternativeBand )
70 {
72 color.setAlphaF( color.alphaF() * alphaScale );
73 rb->setLineStyle( Qt::DotLine );
74 }
75 rb->setStrokeColor( color );
76
77 const QColor fillColor = digitizingFillColor();
78 rb->setFillColor( fillColor );
79
80 rb->show();
81 return rb;
82}
83
85{
86 return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
87}
88
89
91{
92 if ( !mCanvas )
93 {
95 }
96
97 //find out current vector layer
99
100 if ( !vlayer )
101 {
103 }
104
105 QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
106 for ( ; list_it != vertices.constEnd(); ++list_it )
107 {
108 vlayer->addTopologicalPoints( *list_it );
109 }
111}
112
114{
115 if ( !mCanvas )
116 {
118 }
119
120 //find out current vector layer
122
123 if ( !vlayer )
124 {
126 }
127
129 QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
130 for ( ; list_it != vertices.constEnd(); ++list_it )
131 {
132 vlayer->addTopologicalPoints( *list_it );
133 }
135
137}
138
140{
141 QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
143 if ( alternativeBand )
144 {
145 double alpha = color.alpha() / 255.0 * QgsSettingsRegistryCore::settingsDigitizingLineColorAlphaScale->value();
146 rb->setLineStyle( Qt::DotLine );
147 color.setAlphaF( alpha );
148 }
149 rb->setStrokeColor( color );
150 rb->setFillColor( color );
152 rb->show();
153 return rb;
154}
155
157{
158 emit messageEmitted( tr( "No active vector layer" ) );
159}
160
162{
163 emit messageEmitted( tr( "Layer not editable" ) );
164}
165
166void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
167{
168 for ( QgsMapLayer *layer : layers )
169 {
170 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
171 if ( vlayer )
172 {
173 connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
174 }
175 }
176}
177
178void QgsMapToolEdit::cleanCanvas()
179{
180 if ( editableVectorLayers().isEmpty() )
181 {
182 clean();
183 }
184}
185
186QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
187{
188 QList<QgsVectorLayer *> editableLayers;
189 if ( mCanvas->project() )
190 {
191 const auto layers = mCanvas->project()->mapLayers().values();
192 for ( QgsMapLayer *layer : layers )
193 {
194 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
195 if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
196 editableLayers << vlayer;
197 }
198 }
199 return editableLayers;
200}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition: qgis.h:255
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:93
Base class for all map layer types.
Definition: qgsmaplayer.h:75
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.
QgsRubberBand * createRubberBand(Qgis::GeometryType geometryType=Qgis::GeometryType::Line, bool alternativeBand=false)
Creates a 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.
QgsGeometryRubberBand * createGeometryRubberBand(Qgis::GeometryType geometryType=Qgis::GeometryType::Line, bool alternativeBand=false) const
Creates a geometry 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:341
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:126
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:54
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.
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.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:5776
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:5775