QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsMapToolEdit::digitizingStrokeWidth
static int digitizingStrokeWidth()
Returns stroke width for rubber bands (from global settings)
Definition: qgsmaptooledit.cpp:56
QgsMapToolEdit::QgsMapToolEdit
QgsMapToolEdit(QgsMapCanvas *canvas)
Definition: qgsmaptooledit.cpp:27
qgssettingsregistrycore.h
QgsMapToolEdit::addTopologicalPoints
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.
Definition: qgsmaptooledit.cpp:120
QgsMapLayer::editingStopped
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
qgsmapcanvas.h
QgsProject::layersAdded
void layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the registry.
qgsgeometryrubberband.h
QgsRubberBand
A class for drawing transient features (e.g. digitizing lines) on the map.
Definition: qgsrubberband.h:51
QgsSettingsRegistryCore::settingsDigitizingFillColorRed
static const QgsSettingsEntryInteger settingsDigitizingFillColorRed
Settings entry digitizing fill color red.
Definition: qgssettingsregistrycore.h:76
QgsMapToolEdit::notifyNotVectorLayer
void notifyNotVectorLayer()
Display a timed message bar noting the active layer is not vector.
Definition: qgsmaptooledit.cpp:166
QgsSettingsRegistryCore::settingsDigitizingLineWidth
static const QgsSettingsEntryInteger settingsDigitizingLineWidth
Settings entry digitizing line width.
Definition: qgssettingsregistrycore.h:58
QgsMapToolEdit::defaultMValue
static double defaultMValue()
Returns default M value.
Definition: qgsmaptooledit.cpp:42
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsSettingsRegistryCore::settingsDigitizingFillColorBlue
static const QgsSettingsEntryInteger settingsDigitizingFillColorBlue
Settings entry digitizing fill color blue.
Definition: qgssettingsregistrycore.h:82
QgsMapToolEdit::InvalidLayer
@ InvalidLayer
AddTopologicalPoints failed due to an invalid canvas.
Definition: qgsmaptooledit.h:100
QgsRubberBand::setStrokeColor
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
Definition: qgsrubberband.cpp:68
QgsVectorLayer::isSpatial
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
Definition: qgsvectorlayer.cpp:3733
QgsRubberBand::setFillColor
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
Definition: qgsrubberband.cpp:60
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsSettingsRegistryCore::settingsDigitizingLineColorRed
static const QgsSettingsEntryInteger settingsDigitizingLineColorRed
Settings entry digitizing line color red.
Definition: qgssettingsregistrycore.h:61
QgsMapTool::mCanvas
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:336
QgsMapTool::clean
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:120
QgsMapToolEdit::notifyNotEditableLayer
void notifyNotEditableLayer()
Display a timed message bar noting the active vector layer is not editable.
Definition: qgsmaptooledit.cpp:171
QgsMapToolEdit::currentVectorLayer
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
Definition: qgsmaptooledit.cpp:91
QgsSettingsRegistryCore::settingsDigitizingDefaultMValue
static const QgsSettingsEntryDouble settingsDigitizingDefaultMValue
Settings entry digitizing default m value.
Definition: qgssettingsregistrycore.h:94
QgsVectorLayer::isEditable
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
Definition: qgsvectorlayer.cpp:3728
qgsrubberband.h
QgsMapTool
Abstract base class for all map tools. Map tools are user interactive tools for manipulating the map ...
Definition: qgsmaptool.h:70
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:2820
QgsGeometryRubberBand::setFillColor
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
Definition: qgsgeometryrubberband.cpp:139
QgsMapToolEdit::Success
@ Success
AddTopologicalPoints was successful.
Definition: qgsmaptooledit.h:98
QgsMapTool::layer
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
QgsVectorLayer::addTopologicalPoints
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
Definition: qgsvectorlayer.cpp:1450
QgsMapToolEdit::InvalidCanvas
@ InvalidCanvas
AddTopologicalPoints failed due to an invalid canvas.
Definition: qgsmaptooledit.h:99
QgsGeometryRubberBand::setLineStyle
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
Definition: qgsgeometryrubberband.cpp:154
QgsMapToolEdit::createGeometryRubberBand
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.
Definition: qgsmaptooledit.cpp:146
QgsMapTool::messageEmitted
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
QgsMapToolEdit::defaultZValue
static double defaultZValue()
Returns default Z value.
Definition: qgsmaptooledit.cpp:37
QgsMapToolEdit::createRubberBand
QgsRubberBand * createRubberBand(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry, bool alternativeBand=false)
Creates a rubber band with the color/line width from the QGIS settings.
Definition: qgsmaptooledit.cpp:70
QgsSettingsRegistryCore::settingsDigitizingFillColorGreen
static const QgsSettingsEntryInteger settingsDigitizingFillColorGreen
Settings entry digitizing fill color green.
Definition: qgssettingsregistrycore.h:79
QgsRubberBand::setLineStyle
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
Definition: qgsrubberband.cpp:100
QgsMapToolEdit::digitizingFillColor
static QColor digitizingFillColor()
Returns fill color for rubber bands (from global settings)
Definition: qgsmaptooledit.cpp:61
qgsvectorlayer.h
QgsSettingsRegistryCore::settingsDigitizingLineColorAlphaScale
static const QgsSettingsEntryDouble settingsDigitizingLineColorAlphaScale
Settings entry digitizing line color alpha scale.
Definition: qgssettingsregistrycore.h:73
QgsGeometryRubberBand
A rubberband class for QgsAbstractGeometry (considering curved geometries).
Definition: qgsgeometryrubberband.h:48
QgsMapToolEdit::TopologicalResult
TopologicalResult
Result of addTopologicalPoints.
Definition: qgsmaptooledit.h:96
QgsRubberBand::setWidth
void setWidth(int width)
Sets the width of the line.
Definition: qgsrubberband.cpp:78
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsSettingsRegistryCore::settingsDigitizingLineColorBlue
static const QgsSettingsEntryInteger settingsDigitizingLineColorBlue
Settings entry digitizing line color blue.
Definition: qgssettingsregistrycore.h:67
QgsSettingsRegistryCore::settingsDigitizingLineColorAlpha
static const QgsSettingsEntryInteger settingsDigitizingLineColorAlpha
Settings entry digitizing line color alpha.
Definition: qgssettingsregistrycore.h:70
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
qgsmaptooledit.h
QgsSettingsRegistryCore::settingsDigitizingDefaultZValue
static const QgsSettingsEntryDouble settingsDigitizingDefaultZValue
Settings entry digitizing default z value.
Definition: qgssettingsregistrycore.h:91
QgsSettingsRegistryCore::settingsDigitizingLineColorGreen
static const QgsSettingsEntryInteger settingsDigitizingLineColorGreen
Settings entry digitizing line color green.
Definition: qgssettingsregistrycore.h:64
QgsGeometryRubberBand::setStrokeWidth
void setStrokeWidth(int width)
Sets stroke width.
Definition: qgsgeometryrubberband.cpp:149
QgsGeometryRubberBand::setStrokeColor
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
Definition: qgsgeometryrubberband.cpp:144
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2819
QgsSettingsRegistryCore::settingsDigitizingFillColorAlpha
static const QgsSettingsEntryInteger settingsDigitizingFillColorAlpha
Settings entry digitizing fill color alpha.
Definition: qgssettingsregistrycore.h:85
qgsproject.h
QgsMapToolEdit::digitizingStrokeColor
static QColor digitizingStrokeColor()
Returns stroke color for rubber bands (from global settings)
Definition: qgsmaptooledit.cpp:48
QgsSettingsEntryByValue::value
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
Definition: qgssettingsentry.h:520