QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 }
QgsMapToolEdit::digitizingStrokeWidth
static int digitizingStrokeWidth()
Returns stroke width for rubber bands (from global settings)
Definition: qgsmaptooledit.cpp:54
QgsMapToolEdit::QgsMapToolEdit
QgsMapToolEdit(QgsMapCanvas *canvas)
Definition: qgsmaptooledit.cpp:27
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:264
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:123
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
qgsmapcanvas.h
QgsProject::layersAdded
void layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the registry.
QgsProject::mapLayers
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Definition: qgsproject.cpp:3436
qgsgeometryrubberband.h
QgsRubberBand
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:50
QgsMapToolEdit::notifyNotVectorLayer
void notifyNotVectorLayer()
Display a timed message bar noting the active layer is not vector.
Definition: qgsmaptooledit.cpp:170
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsMapToolEdit::InvalidLayer
@ InvalidLayer
AddTopologicalPoints failed due to an invalid canvas.
Definition: qgsmaptooledit.h:85
QgsRubberBand::setStrokeColor
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
Definition: qgsrubberband.cpp:60
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:3599
QgsRubberBand::setFillColor
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
Definition: qgsrubberband.cpp:52
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsMapTool::clean
virtual void clean()
convenient method to clean members
Definition: qgsmaptool.cpp:106
QgsMapToolEdit::notifyNotEditableLayer
void notifyNotEditableLayer()
Display a timed message bar noting the active vector layer is not editable.
Definition: qgsmaptooledit.cpp:175
QgsMapToolEdit::currentVectorLayer
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
Definition: qgsmaptooledit.cpp:94
QgsVectorLayer::isEditable
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
Definition: qgsvectorlayer.cpp:3594
qgsrubberband.h
QgsMapTool
Abstract base class for all map tools.
Definition: qgsmaptool.h:64
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:797
QgsGeometryRubberBand::setFillColor
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
Definition: qgsgeometryrubberband.cpp:138
QgsMapToolEdit::Success
@ Success
AddTopologicalPoints was successful.
Definition: qgsmaptooledit.h:83
QgsVectorLayer::addTopologicalPoints
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
Definition: qgsvectorlayer.cpp:1387
QgsMapTool::messageEmitted
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::Info)
emit a message
QgsMapToolEdit::InvalidCanvas
@ InvalidCanvas
AddTopologicalPoints failed due to an invalid canvas.
Definition: qgsmaptooledit.h:84
QgsGeometryRubberBand::setLineStyle
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
Definition: qgsgeometryrubberband.cpp:153
QgsMapToolEdit::createGeometryRubberBand
QgsGeometryRubberBand * createGeometryRubberBand(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry, bool alternativeBand=false) const
Definition: qgsmaptooledit.cpp:149
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:73
Qgis::DEFAULT_Z_COORDINATE
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:175
QgsRubberBand::setLineStyle
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
Definition: qgsrubberband.cpp:92
QgsMapToolEdit::digitizingFillColor
static QColor digitizingFillColor()
Returns fill color for rubber bands (from global settings)
Definition: qgsmaptooledit.cpp:60
qgsvectorlayer.h
QgsGeometryRubberBand
A rubberband class for QgsAbstractGeometry (considering curved geometries).
Definition: qgsgeometryrubberband.h:49
QgsMapToolEdit::TopologicalResult
TopologicalResult
Result of addTopologicalPoints.
Definition: qgsmaptooledit.h:82
QgsVectorLayer::editingStopped
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
QgsRubberBand::setWidth
void setWidth(int width)
Sets the width of the line.
Definition: qgsrubberband.cpp:70
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
qgsmaptooledit.h
QgsMapCanvas::project
QgsProject * project()
Returns the project linked to this canvas.
Definition: qgsmapcanvas.cpp:2286
qgssettings.h
QgsGeometryRubberBand::setStrokeWidth
void setStrokeWidth(int width)
Sets stroke width.
Definition: qgsgeometryrubberband.cpp:148
QgsMapCanvas::currentLayer
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
Definition: qgsmapcanvas.cpp:512
QgsGeometryRubberBand::setStrokeColor
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
Definition: qgsgeometryrubberband.cpp:143
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:796
qgsproject.h
QgsMapToolEdit::digitizingStrokeColor
static QColor digitizingStrokeColor()
Returns stroke color for rubber bands (from global settings)
Definition: qgsmaptooledit.cpp:42
QgsMapToolEdit::defaultZValue
double defaultZValue() const
Returns default Z value Use for set Z coordinate to new vertex for 2.5d geometries.
Definition: qgsmaptooledit.cpp:37