QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
92 }
93 
94 
95 int QgsMapToolEdit::addTopologicalPoints( const QVector<QgsPointXY> &geom )
96 {
97  if ( !mCanvas )
98  {
99  return 1;
100  }
101 
102  //find out current vector layer
104 
105  if ( !vlayer )
106  {
107  return 2;
108  }
109 
110  QVector<QgsPointXY>::const_iterator list_it = geom.constBegin();
111  for ( ; list_it != geom.constEnd(); ++list_it )
112  {
113  vlayer->addTopologicalPoints( *list_it );
114  }
115  return 0;
116 }
117 
119 {
120  QgsSettings settings;
121  QgsGeometryRubberBand *rb = new QgsGeometryRubberBand( mCanvas, geometryType );
122  QColor color( settings.value( QStringLiteral( "qgis/digitizing/line_color_red" ), 255 ).toInt(),
123  settings.value( QStringLiteral( "qgis/digitizing/line_color_green" ), 0 ).toInt(),
124  settings.value( QStringLiteral( "qgis/digitizing/line_color_blue" ), 0 ).toInt() );
125  double myAlpha = settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha" ), 200 ).toInt() / 255.0;
126  if ( alternativeBand )
127  {
128  myAlpha = myAlpha * settings.value( QStringLiteral( "qgis/digitizing/line_color_alpha_scale" ), 0.75 ).toDouble();
129  rb->setLineStyle( Qt::DotLine );
130  }
131  color.setAlphaF( myAlpha );
132  rb->setStrokeColor( color );
133  rb->setFillColor( color );
135  rb->show();
136  return rb;
137 }
138 
140 {
141  emit messageEmitted( tr( "No active vector layer" ) );
142 }
143 
145 {
146  emit messageEmitted( tr( "Layer not editable" ) );
147 }
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
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
void messageEmitted(const QString &message, Qgis::MessageLevel=Qgis::Info)
emit a message
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setStrokeWidth(int width)
Sets stroke width.
static QColor digitizingFillColor()
Returns fill color for rubber bands (from global settings)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
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:145
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.
QgsGeometryRubberBand * createGeometryRubberBand(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry, bool alternativeBand=false) const
int addTopologicalPoints(const QVector< QgsPointXY > &geom)
Adds vertices to other features to keep topology up to date, e.g.
QgsMapToolEdit(QgsMapCanvas *canvas)
double defaultZValue() const
Returns default Z value Use for set Z coordinate to new vertex for 2.5d geometries.
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:47
void setStrokeColor(const QColor &c)
Sets stroke color for vertex markers.
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
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:138
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
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.
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
Represents a vector layer which manages a vector based data sets.