QGIS API Documentation  3.20.0-Odense (decaadbb31)
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"
22 #include "qgssettingsregistrycore.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 {
39  return QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.value();
40 }
41 
43 {
44  return QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_m_value" ), Qgis::DEFAULT_M_COORDINATE ).toDouble();
45 }
46 
48 {
49  return QColor( QgsSettingsRegistryCore::settingsDigitizingLineColorRed.value(),
50  QgsSettingsRegistryCore::settingsDigitizingLineColorGreen.value(),
51  QgsSettingsRegistryCore::settingsDigitizingLineColorBlue.value(),
52  QgsSettingsRegistryCore::settingsDigitizingLineColorAlpha.value() );
53 }
54 
56 {
57  return QgsSettingsRegistryCore::settingsDigitizingLineWidth.value();
58 }
59 
61 {
62  return QColor( QgsSettingsRegistryCore::settingsDigitizingFillColorRed.value(),
63  QgsSettingsRegistryCore::settingsDigitizingFillColorGreen.value(),
64  QgsSettingsRegistryCore::settingsDigitizingFillColorBlue.value(),
65  QgsSettingsRegistryCore::settingsDigitizingFillColorAlpha.value() );
66 }
67 
68 
70 {
71  QgsSettings settings;
72  QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
74  QColor color = digitizingStrokeColor();
75  if ( alternativeBand )
76  {
77  double alphaScale = QgsSettingsRegistryCore::settingsDigitizingLineColorAlphaScale.value();
78  color.setAlphaF( color.alphaF() * alphaScale );
79  rb->setLineStyle( Qt::DotLine );
80  }
81  rb->setStrokeColor( color );
82 
83  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 );
148  QColor color( QgsSettingsRegistryCore::settingsDigitizingLineColorRed.value(),
149  QgsSettingsRegistryCore::settingsDigitizingLineColorGreen.value(),
150  QgsSettingsRegistryCore::settingsDigitizingLineColorBlue.value() );
151  double myAlpha = QgsSettingsRegistryCore::settingsDigitizingLineColorAlpha.value() / 255.0;
152  if ( alternativeBand )
153  {
154  myAlpha = myAlpha * QgsSettingsRegistryCore::settingsDigitizingLineColorAlphaScale.value();
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:409
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:86
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:70
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:66
QgsMapCanvas * mCanvas
The pointer to the map canvas.
Definition: qgsmaptool.h:297
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:110
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:51
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.
Represents a vector layer which manages a vector based data sets.
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
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:1080
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:1079