QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 }
31 
33 {
34  return QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_z_value" ), Qgis::DEFAULT_Z_COORDINATE ).toDouble();
35 }
36 
38 {
39  QgsSettings settings;
40  QColor color(
41  settings.value( QStringLiteral( "qgis/digitizing/line_color_red" ), 255 ).toInt(),
42  settings.value( QStringLiteral( "qgis/digitizing/line_color_green" ), 0 ).toInt(),
43  settings.value( QStringLiteral( "qgis/digitizing/line_color_blue" ), 0 ).toInt() );
44  double myAlpha = settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha" ), 200 ).toInt() / 255.0;
45  color.setAlphaF( myAlpha );
46  return color;
47 }
48 
50 {
51  QgsSettings settings;
52  return settings.value( QStringLiteral( "qgis/digitizing/line_width" ), 1 ).toInt();
53 }
54 
56 {
57  QgsSettings settings;
58  QColor fillColor(
59  settings.value( QStringLiteral( "qgis/digitizing/fill_color_red" ), 255 ).toInt(),
60  settings.value( QStringLiteral( "qgis/digitizing/fill_color_green" ), 0 ).toInt(),
61  settings.value( QStringLiteral( "qgis/digitizing/fill_color_blue" ), 0 ).toInt() );
62  double myAlpha = settings.value( QStringLiteral( "qgis/digitizing/fill_color_alpha" ), 30 ).toInt() / 255.0;
63  fillColor.setAlphaF( myAlpha );
64  return fillColor;
65 }
66 
67 
69 {
70  QgsSettings settings;
71  QgsRubberBand *rb = new QgsRubberBand( mCanvas, geometryType );
73  QColor color = digitizingStrokeColor();
74  if ( alternativeBand )
75  {
76  double alphaScale = settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha_scale" ), 0.75 ).toDouble();
77  color.setAlphaF( color.alphaF() * alphaScale );
78  rb->setLineStyle( Qt::DotLine );
79  }
80  rb->setStrokeColor( color );
81 
82  QColor fillColor = digitizingFillColor();
83  rb->setFillColor( fillColor );
84 
85  rb->show();
86  return rb;
87 }
88 
90 {
91  return mCanvas ? qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ) : nullptr;
92 }
93 
94 
96 {
97  if ( !mCanvas )
98  {
100  }
101 
102  //find out current vector layer
104 
105  if ( !vlayer )
106  {
108  }
109 
110  QVector<QgsPoint>::const_iterator list_it = vertices.constBegin();
111  for ( ; list_it != vertices.constEnd(); ++list_it )
112  {
113  vlayer->addTopologicalPoints( *list_it );
114  }
116 }
117 
119 {
120  if ( !mCanvas )
121  {
123  }
124 
125  //find out current vector layer
127 
128  if ( !vlayer )
129  {
131  }
132 
134  QVector<QgsPointXY>::const_iterator list_it = vertices.constBegin();
135  for ( ; list_it != vertices.constEnd(); ++list_it )
136  {
137  vlayer->addTopologicalPoints( *list_it );
138  }
140 
142 }
143 
145 {
146  QgsSettings settings;
147  QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
148  QColor color( settings.value( QStringLiteral( "qgis/digitizing/line_color_red" ), 255 ).toInt(),
149  settings.value( QStringLiteral( "qgis/digitizing/line_color_green" ), 0 ).toInt(),
150  settings.value( QStringLiteral( "qgis/digitizing/line_color_blue" ), 0 ).toInt() );
151  double myAlpha = settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha" ), 200 ).toInt() / 255.0;
152  if ( alternativeBand )
153  {
154  myAlpha = myAlpha * settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha_scale" ), 0.75 ).toDouble();
155  rb->setLineStyle( Qt::DotLine );
156  }
157  color.setAlphaF( myAlpha );
158  rb->setStrokeColor( color );
159  rb->setFillColor( color );
161  rb->show();
162  return rb;
163 }
164 
166 {
167  emit messageEmitted( tr( "No active vector layer" ) );
168 }
169 
171 {
172  emit messageEmitted( tr( "Layer not editable" ) );
173 }
void setWidth(int width)
Sets the width of the line.
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::Info)
emit a message
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:649
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
void setStrokeWidth(int width)
Sets stroke width.
static QColor digitizingFillColor()
Returns fill color for rubber bands (from global settings)
TopologicalResult
Result of addTopologicalPoints.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:75
static int digitizingStrokeWidth()
Returns stroke width for rubber bands (from global settings)
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:147
AddTopologicalPoints was successful.
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:241
void notifyNotEditableLayer()
Display a timed message bar noting the active vector layer is not editable.
AddTopologicalPoints failed due to an invalid canvas.
QgsMapToolEdit(QgsMapCanvas *canvas)
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:48
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
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 setFillColor(const QColor &color)
Sets the fill color for the rubberband.
double defaultZValue() const
Returns default Z value Use for set Z coordinate to new vertex for 2.5d geometries.
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
static QColor digitizingStrokeColor()
Returns stroke color for rubber bands (from global settings)
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:139
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:650
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
void notifyNotVectorLayer()
Display a timed message bar noting the active layer is not vector.
A rubberband class for QgsAbstractGeometry (considering curved geometries)
QgsRubberBand * createRubberBand(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry, bool alternativeBand=false)
Creates a rubber band with the color/line width from the QGIS settings.
QgsGeometryRubberBand * createGeometryRubberBand(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry, bool alternativeBand=false) const
AddTopologicalPoints failed due to an invalid canvas.
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
Represents a vector layer which manages a vector based data sets.