QGIS API Documentation  3.13.0-Master (788156190c)
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 }
QgsMapToolEdit::digitizingStrokeWidth
static int digitizingStrokeWidth()
Returns stroke width for rubber bands (from global settings)
Definition: qgsmaptooledit.cpp:49
QgsMapToolEdit::QgsMapToolEdit
QgsMapToolEdit(QgsMapCanvas *canvas)
Definition: qgsmaptooledit.cpp:27
QgsMapTool::mCanvas
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:241
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:118
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
qgsgeometryrubberband.h
QgsRubberBand
Definition: qgsrubberband.h:49
QgsMapToolEdit::notifyNotVectorLayer
void notifyNotVectorLayer()
Display a timed message bar noting the active layer is not vector.
Definition: qgsmaptooledit.cpp:165
QgsMapCanvas
Definition: qgsmapcanvas.h:78
QgsMapToolEdit::InvalidLayer
@ InvalidLayer
AddTopologicalPoints failed due to an invalid canvas.
Definition: qgsmaptooledit.h:75
QgsRubberBand::setStrokeColor
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
Definition: qgsrubberband.cpp:60
QgsRubberBand::setFillColor
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
Definition: qgsrubberband.cpp:52
QgsSettings
Definition: qgssettings.h:61
QgsMapToolEdit::notifyNotEditableLayer
void notifyNotEditableLayer()
Display a timed message bar noting the active vector layer is not editable.
Definition: qgsmaptooledit.cpp:170
QgsMapToolEdit::currentVectorLayer
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
Definition: qgsmaptooledit.cpp:89
qgsrubberband.h
QgsMapTool
Definition: qgsmaptool.h:62
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:732
QgsGeometryRubberBand::setFillColor
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
Definition: qgsgeometryrubberband.cpp:130
QgsMapToolEdit::Success
@ Success
AddTopologicalPoints was successful.
Definition: qgsmaptooledit.h:73
QgsVectorLayer::addTopologicalPoints
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
Definition: qgsvectorlayer.cpp:1357
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:74
QgsGeometryRubberBand::setLineStyle
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
Definition: qgsgeometryrubberband.cpp:145
QgsMapToolEdit::createGeometryRubberBand
QgsGeometryRubberBand * createGeometryRubberBand(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry, bool alternativeBand=false) const
Definition: qgsmaptooledit.cpp:144
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:68
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:188
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:55
qgsvectorlayer.h
QgsGeometryRubberBand
Definition: qgsgeometryrubberband.h:41
QgsMapToolEdit::TopologicalResult
TopologicalResult
Result of addTopologicalPoints.
Definition: qgsmaptooledit.h:71
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:139
QgsVectorLayer
Definition: qgsvectorlayer.h:386
qgsmaptooledit.h
qgssettings.h
QgsGeometryRubberBand::setStrokeWidth
void setStrokeWidth(int width)
Sets stroke width.
Definition: qgsgeometryrubberband.cpp:140
QgsMapCanvas::currentLayer
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
Definition: qgsmapcanvas.cpp:495
QgsGeometryRubberBand::setStrokeColor
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
Definition: qgsgeometryrubberband.cpp:135
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:731
qgsproject.h
QgsMapToolEdit::digitizingStrokeColor
static QColor digitizingStrokeColor()
Returns stroke color for rubber bands (from global settings)
Definition: qgsmaptooledit.cpp:37
QgsMapToolEdit::defaultZValue
double defaultZValue() const
Returns default Z value Use for set Z coordinate to new vertex for 2.5d geometries.
Definition: qgsmaptooledit.cpp:32