QGIS API Documentation  3.25.0-Master (dec16ba68b)
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"
19 #include "qgsgeometryrubberband.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 }
47 
49 {
54 }
55 
57 {
59 }
60 
62 {
67 }
68 
69 
71 {
72  const QgsSettings settings;
73  QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
75  QColor color = digitizingStrokeColor();
76  if ( alternativeBand )
77  {
79  color.setAlphaF( color.alphaF() * alphaScale );
80  rb->setLineStyle( Qt::DotLine );
81  }
82  rb->setStrokeColor( color );
83 
84  const QColor fillColor = digitizingFillColor();
85  rb->setFillColor( fillColor );
86 
87  rb->show();
88  return rb;
89 }
90 
92 {
93  return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
94 }
95 
96 
98 {
99  if ( !mCanvas )
100  {
102  }
103 
104  //find out current vector layer
106 
107  if ( !vlayer )
108  {
110  }
111 
112  QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
113  for ( ; list_it != vertices.constEnd(); ++list_it )
114  {
115  vlayer->addTopologicalPoints( *list_it );
116  }
118 }
119 
121 {
122  if ( !mCanvas )
123  {
125  }
126 
127  //find out current vector layer
129 
130  if ( !vlayer )
131  {
133  }
134 
136  QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
137  for ( ; list_it != vertices.constEnd(); ++list_it )
138  {
139  vlayer->addTopologicalPoints( *list_it );
140  }
142 
144 }
145 
147 {
148  QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
153  if ( alternativeBand )
154  {
156  rb->setLineStyle( Qt::DotLine );
157  }
158  color.setAlphaF( myAlpha );
159  rb->setStrokeColor( color );
160  rb->setFillColor( color );
162  rb->show();
163  return rb;
164 }
165 
167 {
168  emit messageEmitted( tr( "No active vector layer" ) );
169 }
170 
172 {
173  emit messageEmitted( tr( "Layer not editable" ) );
174 }
175 
176 void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
177 {
178  for ( QgsMapLayer *layer : layers )
179  {
180  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
181  if ( vlayer )
182  {
183  connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
184  }
185  }
186 }
187 
188 void QgsMapToolEdit::cleanCanvas()
189 {
190  if ( editableVectorLayers().isEmpty() )
191  {
192  clean();
193  }
194 }
195 
196 QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
197 {
198  QList<QgsVectorLayer *> editableLayers;
199  if ( mCanvas->project() )
200  {
201  const auto layers = mCanvas->project()->mapLayers().values();
202  for ( QgsMapLayer *layer : layers )
203  {
204  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
205  if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
206  editableLayers << vlayer;
207  }
208  }
209  return editableLayers;
210 }
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
QgsProject * project()
Returns the project linked to this canvas.
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
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
QgsMapCanvas * mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:336
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.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
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.
static const QgsSettingsEntryInteger settingsDigitizingFillColorAlpha
Settings entry digitizing fill color alpha.
static const QgsSettingsEntryInteger settingsDigitizingFillColorBlue
Settings entry digitizing fill color blue.
static const QgsSettingsEntryDouble settingsDigitizingDefaultZValue
Settings entry digitizing default z value.
static const QgsSettingsEntryInteger settingsDigitizingLineColorAlpha
Settings entry digitizing line color alpha.
static const QgsSettingsEntryInteger settingsDigitizingLineColorGreen
Settings entry digitizing line color green.
static const QgsSettingsEntryInteger settingsDigitizingLineColorRed
Settings entry digitizing line color red.
static const QgsSettingsEntryInteger settingsDigitizingFillColorRed
Settings entry digitizing fill color red.
static const QgsSettingsEntryInteger settingsDigitizingLineColorBlue
Settings entry digitizing line color blue.
static const QgsSettingsEntryInteger settingsDigitizingFillColorGreen
Settings entry digitizing fill color green.
static const QgsSettingsEntryDouble settingsDigitizingDefaultMValue
Settings entry digitizing default m value.
static const QgsSettingsEntryInteger settingsDigitizingLineWidth
Settings entry digitizing line width.
static const QgsSettingsEntryDouble settingsDigitizingLineColorAlphaScale
Settings entry digitizing line color alpha scale.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
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:2545
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2544