QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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 "moc_qgsmaptooledit.cpp"
18#include "qgsproject.h"
19#include "qgsmapcanvas.h"
21#include "qgsrubberband.h"
22#include "qgsvectorlayer.h"
25
26#include <QKeyEvent>
27
28
30 : QgsMapTool( canvas )
31{
32 if ( mCanvas->project() )
33 {
34 connect( mCanvas->project(), &QgsProject::layersAdded, this, &QgsMapToolEdit::connectLayers );
35 connectLayers( mCanvas->project()->mapLayers().values() ); // Connect existing layers
36 }
37}
38
43
48
53
58
63
64
66{
67 QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
69 QColor color = digitizingStrokeColor();
70 if ( alternativeBand )
71 {
73 color.setAlphaF( color.alphaF() * alphaScale );
74 rb->setLineStyle( Qt::DotLine );
75 }
76 rb->setStrokeColor( color );
77
78 const QColor fillColor = digitizingFillColor();
79 rb->setFillColor( fillColor );
80
81 rb->show();
82 return rb;
83}
84
86{
87 return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
88}
89
90
92{
93 if ( !mCanvas )
94 {
96 }
97
98 //find out current vector layer
100
101 if ( !vlayer )
102 {
104 }
105
106 QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
107 for ( ; list_it != vertices.constEnd(); ++list_it )
108 {
109 vlayer->addTopologicalPoints( *list_it );
110 }
112}
113
115{
116 if ( !mCanvas )
117 {
119 }
120
121 //find out current vector layer
123
124 if ( !vlayer )
125 {
127 }
128
130 QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
131 for ( ; list_it != vertices.constEnd(); ++list_it )
132 {
133 vlayer->addTopologicalPoints( *list_it );
134 }
136
138}
139
141{
142 QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
144 if ( alternativeBand )
145 {
146 double alpha = color.alpha() / 255.0 * QgsSettingsRegistryCore::settingsDigitizingLineColorAlphaScale->value();
147 rb->setLineStyle( Qt::DotLine );
148 color.setAlphaF( alpha );
149 }
150 rb->setStrokeColor( color );
151 rb->setFillColor( color );
153 rb->show();
154 return rb;
155}
156
158{
159 emit messageEmitted( tr( "No active vector layer" ) );
160}
161
163{
164 emit messageEmitted( tr( "Layer not editable" ) );
165}
166
167void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
168{
169 for ( QgsMapLayer *layer : layers )
170 {
171 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
172 if ( vlayer )
173 {
174 connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
175 }
176 }
177}
178
179void QgsMapToolEdit::cleanCanvas()
180{
181 if ( editableVectorLayers().isEmpty() )
182 {
183 clean();
184 }
185}
186
187QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
188{
189 QList<QgsVectorLayer *> editableLayers;
190 if ( mCanvas->project() )
191 {
192 const auto layers = mCanvas->project()->mapLayers().values();
193 for ( QgsMapLayer *layer : layers )
194 {
195 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
196 if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
197 editableLayers << vlayer;
198 }
199 }
200 return editableLayers;
201}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
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.
Base class for all map layer types.
Definition qgsmaplayer.h:76
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
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.
QgsRubberBand * createRubberBand(Qgis::GeometryType geometryType=Qgis::GeometryType::Line, bool alternativeBand=false)
Creates a rubber band with the color/line width from the QGIS settings.
static double defaultMValue()
Returns default M value.
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)
static double defaultZValue()
Returns default Z value.
QgsGeometryRubberBand * createGeometryRubberBand(Qgis::GeometryType geometryType=Qgis::GeometryType::Line, bool alternativeBand=false) const
Creates a geometry 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
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:341
QgsMapLayer * layer(const QString &id)
Returns the map layer with the matching ID, or nullptr if no layers could be found.
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::MessageLevel::Info)
emit a message
virtual void clean()
convenient method to clean members
void layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the registry.
A class for drawing transient features (e.g.
void setWidth(double 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.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static const QgsSettingsEntryColor * settingsDigitizingFillColor
Settings entry digitizing fill color.
static const QgsSettingsEntryInteger * settingsDigitizingLineWidth
Settings entry digitizing line width.
static const QgsSettingsEntryDouble * settingsDigitizingLineColorAlphaScale
Settings entry digitizing line color alpha scale.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultMValue
Settings entry digitizing default m value.
static const QgsSettingsEntryColor * settingsDigitizingLineColor
Settings entry digitizing line color.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultZValue
Settings entry digitizing default z value.
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.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6494
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493