QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
19#include "qgsmapcanvas.h"
20#include "qgsproject.h"
21#include "qgsrubberband.h"
24#include "qgsvectorlayer.h"
25
26#include <QKeyEvent>
27
28#include "moc_qgsmaptooledit.cpp"
29
32{
33 if ( mCanvas->project() )
34 {
35 connect( mCanvas->project(), &QgsProject::layersAdded, this, &QgsMapToolEdit::connectLayers );
36 connectLayers( mCanvas->project()->mapLayers().values() ); // Connect existing layers
37 }
38}
39
44
49
54
59
64
65
67{
68 QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
70 QColor color = digitizingStrokeColor();
71 if ( alternativeBand )
72 {
74 color.setAlphaF( color.alphaF() * alphaScale );
75 rb->setLineStyle( Qt::DotLine );
76 }
77 rb->setStrokeColor( color );
78
79 const QColor fillColor = digitizingFillColor();
80 rb->setFillColor( fillColor );
81
82 rb->show();
83 return rb;
84}
85
87{
88 return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
89}
90
91
93{
94 if ( !mCanvas )
95 {
97 }
98
99 //find out current vector layer
101
102 if ( !vlayer )
103 {
105 }
106
107 QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
108 for ( ; list_it != vertices.constEnd(); ++list_it )
109 {
110 vlayer->addTopologicalPoints( *list_it );
111 }
113}
114
116{
117 if ( !mCanvas )
118 {
120 }
121
122 //find out current vector layer
124
125 if ( !vlayer )
126 {
128 }
129
131 QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
132 for ( ; list_it != vertices.constEnd(); ++list_it )
133 {
134 vlayer->addTopologicalPoints( *list_it );
135 }
137
139}
140
142{
143 QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
145 if ( alternativeBand )
146 {
147 double alpha = color.alpha() / 255.0 * QgsSettingsRegistryCore::settingsDigitizingLineColorAlphaScale->value();
148 rb->setLineStyle( Qt::DotLine );
149 color.setAlphaF( alpha );
150 }
151 rb->setStrokeColor( color );
152 rb->setFillColor( color );
154 rb->show();
155 return rb;
156}
157
159{
160 emit messageEmitted( tr( "No active vector layer" ) );
161}
162
164{
165 emit messageEmitted( tr( "Layer not editable" ) );
166}
167
168void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
169{
170 for ( QgsMapLayer *layer : layers )
171 {
172 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
173 if ( vlayer )
174 {
175 connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
176 }
177 }
178}
179
180void QgsMapToolEdit::cleanCanvas()
181{
182 if ( editableVectorLayers().isEmpty() )
183 {
184 clean();
185 }
186}
187
188QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
189{
190 QList<QgsVectorLayer *> editableLayers;
191 if ( mCanvas->project() )
192 {
193 const auto layers = mCanvas->project()->mapLayers().values();
194 for ( QgsMapLayer *layer : layers )
195 {
196 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
197 if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
198 editableLayers << vlayer;
199 }
200 }
201 return editableLayers;
202}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:358
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.
Base class for all map layer types.
Definition qgsmaplayer.h:80
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.
QgsMapLayer * layer(const QString &id)
Returns the map layer with the matching ID, or nullptr if no layers could be found.
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
QgsMapTool(QgsMapCanvas *canvas)
Constructor takes a map canvas as a parameter.
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:360
friend class QgsMapCanvas
Definition qgsmaptool.h:380
void messageEmitted(const QString &message, Qgis::MessageLevel level=Qgis::MessageLevel::Info)
Emitted when a message should be shown to the user in the application message bar.
virtual void clean()
convenient method to clean members
Responsible 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.
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 dataset.
bool isEditable() const final
Returns true if the provider is in editing mode.
bool isSpatial() const final
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169