QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
23 #include <QKeyEvent>
24 #include <QSettings>
25 
26 
28  : QgsMapTool( canvas )
29 {
30 }
31 
33 {
34 }
35 
36 
38 {
39  QSettings settings;
40  QgsRubberBand* rb = new QgsRubberBand( mCanvas, geometryType );
41  rb->setWidth( settings.value( "/qgis/digitizing/line_width", 1 ).toInt() );
42  QColor color(
43  settings.value( "/qgis/digitizing/line_color_red", 255 ).toInt(),
44  settings.value( "/qgis/digitizing/line_color_green", 0 ).toInt(),
45  settings.value( "/qgis/digitizing/line_color_blue", 0 ).toInt() );
46  double myAlpha = settings.value( "/qgis/digitizing/line_color_alpha", 200 ).toInt() / 255.0;
47  if ( alternativeBand )
48  {
49  myAlpha = myAlpha * settings.value( "/qgis/digitizing/line_color_alpha_scale", 0.75 ).toDouble();
50  rb->setLineStyle( Qt::DotLine );
51  }
52  if ( geometryType == QGis::Polygon )
53  {
54  color.setAlphaF( myAlpha );
55  }
56  color.setAlphaF( myAlpha );
57  rb->setColor( color );
58 
59  QColor fillColor(
60  settings.value( "/qgis/digitizing/fill_color_red", 255 ).toInt(),
61  settings.value( "/qgis/digitizing/fill_color_green", 0 ).toInt(),
62  settings.value( "/qgis/digitizing/fill_color_blue", 0 ).toInt() );
63  myAlpha = settings.value( "/qgis/digitizing/fill_color_alpha", 30 ).toInt() / 255.0 ;
64  fillColor.setAlphaF( myAlpha );
65  rb->setFillColor( fillColor );
66 
67  rb->show();
68  return rb;
69 }
70 
72 {
73  return qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
74 }
75 
76 
78 {
79  if ( !mCanvas )
80  {
81  return 1;
82  }
83 
84  //find out current vector layer
86 
87  if ( !vlayer )
88  {
89  return 2;
90  }
91 
93  for ( ; list_it != geom.constEnd(); ++list_it )
94  {
95  vlayer->addTopologicalPoints( *list_it );
96  }
97  return 0;
98 }
99 
101 {
102  QSettings settings;
103  QgsGeometryRubberBand* rb = new QgsGeometryRubberBand( mCanvas, geometryType );
104  QColor color( settings.value( "/qgis/digitizing/line_color_red", 255 ).toInt(),
105  settings.value( "/qgis/digitizing/line_color_green", 0 ).toInt(),
106  settings.value( "/qgis/digitizing/line_color_blue", 0 ).toInt() );
107  double myAlpha = settings.value( "/qgis/digitizing/line_color_alpha", 200 ).toInt() / 255.0 ;
108  if ( alternativeBand )
109  {
110  myAlpha = myAlpha * settings.value( "/qgis/digitizing/line_color_alpha_scale", 0.75 ).toDouble();
111  rb->setLineStyle( Qt::DotLine );
112  }
113  color.setAlphaF( myAlpha );
114  rb->setOutlineColor( color );
115  rb->setFillColor( color );
116  rb->show();
117  return rb;
118 }
119 
121 {
122  emit messageEmitted( tr( "No active vector layer" ) );
123 }
124 
126 {
127  emit messageEmitted( tr( "Layer not editable" ) );
128 }
void setWidth(int width)
Set the width of the line.
GeometryType
Definition: qgis.h:115
int addTopologicalPoints(const QgsGeometry *geom)
Adds topological points for every vertex of the geometry.
void setLineStyle(Qt::PenStyle penStyle)
Set the style of the line.
void setOutlineColor(const QColor &c)
Sets outline color for vertex markes.
void setFillColor(const QColor &c)
Sets fill color for vertex markers.
virtual ~QgsMapToolEdit()
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
QgsMapCanvas * mCanvas
pointer to map canvas
Definition: qgsmaptool.h:213
void notifyNotEditableLayer()
Display a timed message bar noting the active vector layer is not editable.
void messageEmitted(const QString &message, QgsMessageBar::MessageLevel=QgsMessageBar::INFO)
emit a message
QgsMapToolEdit(QgsMapCanvas *canvas)
int toInt(bool *ok) const
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:32
QgsRubberBand * createRubberBand(QGis::GeometryType geometryType=QGis::Line, bool alternativeBand=false)
Creates a rubber band with the color/line width from the QGIS settings.
void setFillColor(const QColor &color)
Set the fill color for the rubberband.
QgsGeometryRubberBand * createGeometryRubberBand(QGis::GeometryType geometryType=QGis::Line, bool alternativeBand=false) const
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
Abstract base class for all map tools.
Definition: qgsmaptool.h:50
QgsVectorLayer * currentVectorLayer()
Returns the current vector layer of the map canvas or 0.
QVariant value(const QString &key, const QVariant &defaultValue) const
void notifyNotVectorLayer()
Display a timed message bar noting the active layer is not vector.
A rubberband class for QgsAbstractGeometryV2 (considering curved geometries)
int addTopologicalPoints(const QList< QgsPoint > &geom)
Adds vertices to other features to keep topology up to date, e.g.
double toDouble(bool *ok) const
void setColor(const QColor &color)
Set the color for the rubberband.
const_iterator constEnd() const
const_iterator constBegin() const
void setLineStyle(Qt::PenStyle penStyle)
Sets pen style.
Represents a vector layer which manages a vector based data sets.