QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 {
44  return QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_m_value" ), Qgis::DEFAULT_M_COORDINATE ).toDouble();
45 }
46 
48 {
53 }
54 
56 {
58 }
59 
61 {
66 }
67 
68 
70 {
71  const QgsSettings settings;
72  QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
74  QColor color = digitizingStrokeColor();
75  if ( alternativeBand )
76  {
78  color.setAlphaF( color.alphaF() * alphaScale );
79  rb->setLineStyle( Qt::DotLine );
80  }
81  rb->setStrokeColor( color );
82 
83  const QColor fillColor = digitizingFillColor();
84  rb->setFillColor( fillColor );
85 
86  rb->show();
87  return rb;
88 }
89 
91 {
92  return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
93 }
94 
95 
97 {
98  if ( !mCanvas )
99  {
101  }
102 
103  //find out current vector layer
105 
106  if ( !vlayer )
107  {
109  }
110 
111  QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
112  for ( ; list_it != vertices.constEnd(); ++list_it )
113  {
114  vlayer->addTopologicalPoints( *list_it );
115  }
117 }
118 
120 {
121  if ( !mCanvas )
122  {
124  }
125 
126  //find out current vector layer
128 
129  if ( !vlayer )
130  {
132  }
133 
135  QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
136  for ( ; list_it != vertices.constEnd(); ++list_it )
137  {
138  vlayer->addTopologicalPoints( *list_it );
139  }
141 
143 }
144 
146 {
147  QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
152  if ( alternativeBand )
153  {
155  rb->setLineStyle( Qt::DotLine );
156  }
157  color.setAlphaF( myAlpha );
158  rb->setStrokeColor( color );
159  rb->setFillColor( color );
161  rb->show();
162  return rb;
163 }
164 
166 {
167  emit messageEmitted( tr( "No active vector layer" ) );
168 }
169 
171 {
172  emit messageEmitted( tr( "Layer not editable" ) );
173 }
174 
175 void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
176 {
177  for ( QgsMapLayer *layer : layers )
178  {
179  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
180  if ( vlayer )
181  {
182  connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
183  }
184  }
185 }
186 
187 void QgsMapToolEdit::cleanCanvas()
188 {
189  if ( editableVectorLayers().isEmpty() )
190  {
191  clean();
192  }
193 }
194 
195 QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
196 {
197  QList<QgsVectorLayer *> editableLayers;
198  if ( mCanvas->project() )
199  {
200  const auto layers = mCanvas->project()->mapLayers().values();
201  for ( QgsMapLayer *layer : layers )
202  {
203  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
204  if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
205  editableLayers << vlayer;
206  }
207  }
208  return editableLayers;
209 }
static const double DEFAULT_M_COORDINATE
Default M coordinate value.
Definition: qgis.h:1045
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:89
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.
double defaultZValue() const
Returns default Z value.
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.
double defaultMValue() const
Returns default M value.
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
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)
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.
double value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, double defaultValueOverride=0.0) const
Returns settings value.
qlonglong value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, qlonglong defaultValueOverride=0) 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 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
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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:1742
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:1741