QGIS API Documentation 4.3.0-Master (767b36bf018)
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"
22#include "qgsrubberband_impl.h"
25#include "qgsvectorlayer.h"
26
27#include <QKeyEvent>
28
29#include "moc_qgsmaptooledit.cpp"
30
33{
34 if ( mCanvas->project() )
35 {
36 connect( mCanvas->project(), &QgsProject::layersAdded, this, &QgsMapToolEdit::connectLayers );
37 connectLayers( mCanvas->project()->mapLayers().values() ); // Connect existing layers
38 }
39}
40
45
50
55
60
65
66
68{
69 QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
71 QColor color = digitizingStrokeColor();
72 if ( alternativeBand )
73 {
75 color.setAlphaF( color.alphaF() * alphaScale );
76 rb->setLineStyle( Qt::DotLine );
77 }
78 rb->setStrokeColor( color );
79
80 const QColor fillColor = digitizingFillColor();
81 rb->setFillColor( fillColor );
82
83 rb->show();
84 return rb;
85}
86
87QgsRubberBand *QgsMapToolEdit::createRubberBandForLayer( QgsVectorLayer *layer, const QList< QgsFeatureId > &fids, bool alternativeBand )
88{
90 Qgis::GeometryType geomType = vlayer ? vlayer->geometryType() : Qgis::GeometryType::Line;
91
92 QgsRubberBand *rb = createRubberBand( geomType, alternativeBand );
93 prepareRubberBandForLayer( rb, vlayer, fids );
94
95 return rb;
96}
97
98void QgsMapToolEdit::prepareRubberBandForLayer( QgsRubberBand *rubberBand, QgsVectorLayer *layer, const QList< QgsFeatureId > &fids )
99{
100 if ( !rubberBand )
101 return;
102
103 if ( QgsVectorLayer *vlayer = layer ? layer : currentVectorLayer() )
104 {
105 // attach label preview decorator if the layer has labeling enabled and a valid feature was provided
106 if ( !fids.isEmpty() && vlayer->labelsEnabled() && vlayer->labeling() )
107 {
108 rubberBand->addPreviewItem( new QgsVectorLayerLabelRubberBandPreview( rubberBand, fids, vlayer ) );
109 }
110 }
111}
112
114{
115 return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
116}
117
118
120{
121 if ( !mCanvas )
122 {
124 }
125
126 //find out current vector layer
128
129 if ( !vlayer )
130 {
132 }
133
134 QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
135 for ( ; list_it != vertices.constEnd(); ++list_it )
136 {
137 vlayer->addTopologicalPoints( *list_it );
138 }
140}
141
143{
144 if ( !mCanvas )
145 {
147 }
148
149 //find out current vector layer
151
152 if ( !vlayer )
153 {
155 }
156
158 QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
159 for ( ; list_it != vertices.constEnd(); ++list_it )
160 {
161 vlayer->addTopologicalPoints( *list_it );
162 }
164
166}
167
169{
170 QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
172 if ( alternativeBand )
173 {
174 double alpha = color.alpha() / 255.0 * QgsSettingsRegistryCore::settingsDigitizingLineColorAlphaScale->value();
175 rb->setLineStyle( Qt::DotLine );
176 color.setAlphaF( alpha );
177 }
178 rb->setStrokeColor( color );
179 rb->setFillColor( color );
181 rb->show();
182 return rb;
183}
184
186{
187 emit messageEmitted( tr( "No active vector layer" ) );
188}
189
191{
192 emit messageEmitted( tr( "Layer not editable" ) );
193}
194
195void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
196{
197 for ( QgsMapLayer *layer : layers )
198 {
199 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
200 if ( vlayer )
201 {
202 connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
203 }
204 }
205}
206
207void QgsMapToolEdit::cleanCanvas()
208{
209 if ( editableVectorLayers().isEmpty() )
210 {
211 clean();
212 }
213}
214
215QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
216{
217 QList<QgsVectorLayer *> editableLayers;
218 if ( mCanvas->project() )
219 {
220 const auto layers = mCanvas->project()->mapLayers().values();
221 for ( QgsMapLayer *layer : layers )
222 {
223 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
224 if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
225 editableLayers << vlayer;
226 }
227 }
228 return editableLayers;
229}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:379
@ Line
Lines.
Definition qgis.h:381
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:83
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.
void prepareRubberBandForLayer(QgsRubberBand *rubberBand, QgsVectorLayer *layer=nullptr, const QList< QgsFeatureId > &fids=QList< QgsFeatureId >())
Configures a rubberBand for a specific layer and optional list of feature IDs.
QgsRubberBand * createRubberBand(Qgis::GeometryType geometryType=Qgis::GeometryType::Line, bool alternativeBand=false)
Creates a rubber band with the color/line width respecting the user's settings.
QgsRubberBand * createRubberBandForLayer(QgsVectorLayer *layer=nullptr, const QList< QgsFeatureId > &fids=QList< QgsFeatureId >(), bool alternativeBand=false)
Creates and prepares a rubber band for a layer and optional set of feature IDs.
static double defaultMValue()
Returns default M value.
QgsMapToolEdit(QgsMapCanvas *canvas)
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer for the map canvas or nullptr if none is set.
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:403
friend class QgsMapCanvas
Definition qgsmaptool.h:423
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 addPreviewItem(QgsRubberBandPreviewItem *item)
Adds a custom preview item decorator to the rubber band.
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.
Rubber band preview item for showing vector layer labels.
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...
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:8086
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:8085