QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 "qgssettings.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 QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_z_value" ), Qgis::DEFAULT_Z_COORDINATE ).toDouble();
40 }
41 
43 {
44  QgsSettings settings;
45  QColor color(
46  settings.value( QStringLiteral( "qgis/digitizing/line_color_red" ), 255 ).toInt(),
47  settings.value( QStringLiteral( "qgis/digitizing/line_color_green" ), 0 ).toInt(),
48  settings.value( QStringLiteral( "qgis/digitizing/line_color_blue" ), 0 ).toInt() );
49  double myAlpha = settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha" ), 200 ).toInt() / 255.0;
50  color.setAlphaF( myAlpha );
51  return color;
52 }
53 
55 {
56  QgsSettings settings;
57  return settings.value( QStringLiteral( "qgis/digitizing/line_width" ), 1 ).toInt();
58 }
59 
61 {
62  QgsSettings settings;
63  QColor fillColor(
64  settings.value( QStringLiteral( "qgis/digitizing/fill_color_red" ), 255 ).toInt(),
65  settings.value( QStringLiteral( "qgis/digitizing/fill_color_green" ), 0 ).toInt(),
66  settings.value( QStringLiteral( "qgis/digitizing/fill_color_blue" ), 0 ).toInt() );
67  double myAlpha = settings.value( QStringLiteral( "qgis/digitizing/fill_color_alpha" ), 30 ).toInt() / 255.0;
68  fillColor.setAlphaF( myAlpha );
69  return fillColor;
70 }
71 
72 
74 {
75  QgsSettings settings;
76  QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
78  QColor color = digitizingStrokeColor();
79  if ( alternativeBand )
80  {
81  double alphaScale = settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha_scale" ), 0.75 ).toDouble();
82  color.setAlphaF( color.alphaF() * alphaScale );
83  rb->setLineStyle( Qt::DotLine );
84  }
85  rb->setStrokeColor( color );
86 
87  QColor fillColor = digitizingFillColor();
88  rb->setFillColor( fillColor );
89 
90  rb->show();
91  return rb;
92 }
93 
95 {
96  return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
97 }
98 
99 
101 {
102  if ( !mCanvas )
103  {
105  }
106 
107  //find out current vector layer
109 
110  if ( !vlayer )
111  {
113  }
114 
115  QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
116  for ( ; list_it != vertices.constEnd(); ++list_it )
117  {
118  vlayer->addTopologicalPoints( *list_it );
119  }
121 }
122 
124 {
125  if ( !mCanvas )
126  {
128  }
129 
130  //find out current vector layer
132 
133  if ( !vlayer )
134  {
136  }
137 
139  QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
140  for ( ; list_it != vertices.constEnd(); ++list_it )
141  {
142  vlayer->addTopologicalPoints( *list_it );
143  }
145 
147 }
148 
150 {
151  QgsSettings settings;
152  QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
153  QColor color( settings.value( QStringLiteral( "qgis/digitizing/line_color_red" ), 255 ).toInt(),
154  settings.value( QStringLiteral( "qgis/digitizing/line_color_green" ), 0 ).toInt(),
155  settings.value( QStringLiteral( "qgis/digitizing/line_color_blue" ), 0 ).toInt() );
156  double myAlpha = settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha" ), 200 ).toInt() / 255.0;
157  if ( alternativeBand )
158  {
159  myAlpha = myAlpha * settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha_scale" ), 0.75 ).toDouble();
160  rb->setLineStyle( Qt::DotLine );
161  }
162  color.setAlphaF( myAlpha );
163  rb->setStrokeColor( color );
164  rb->setFillColor( color );
166  rb->show();
167  return rb;
168 }
169 
171 {
172  emit messageEmitted( tr( "No active vector layer" ) );
173 }
174 
176 {
177  emit messageEmitted( tr( "Layer not editable" ) );
178 }
179 
180 void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
181 {
182  for ( QgsMapLayer *layer : layers )
183  {
184  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
185  if ( vlayer )
186  {
187  connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
188  }
189  }
190 }
191 
192 void QgsMapToolEdit::cleanCanvas()
193 {
194  if ( editableVectorLayers().isEmpty() )
195  {
196  clean();
197  }
198 }
199 
200 QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
201 {
202  QList<QgsVectorLayer *> editableLayers;
203  if ( mCanvas->project() )
204  {
205  const auto layers = mCanvas->project()->mapLayers().values();
206  for ( QgsMapLayer *layer : layers )
207  {
208  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
209  if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
210  editableLayers << vlayer;
211  }
212  }
213  return editableLayers;
214 }
static const double DEFAULT_Z_COORDINATE
Default Z coordinate value for 2.5d geometry This value have to be assigned to the Z coordinate for t...
Definition: qgis.h:176
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:85
double defaultZValue() const
Returns default Z value Use for set Z coordinate to new vertex for 2.5d geometries.
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
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:288
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::Info)
emit a message
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:106
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:50
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.
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.
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:798
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:797