QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsrubberband.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrubberband.h - Rubberband widget for drawing multilines and polygons
3  --------------------------------------
4  Date : 07-Jan-2006
5  Copyright : (C) 2006 by Tom Elwertowski
6  Email : telwertowski at users dot sourceforge dot net
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 #ifndef QGSRUBBERBAND_H
16 #define QGSRUBBERBAND_H
17 
18 #include "qgsmapcanvasitem.h"
19 #include "qgis_sip.h"
20 #include "qgsgeometry.h"
21 
22 #include <QBrush>
23 #include <QList>
24 #include <QPen>
25 #include <QPolygon>
26 #include <QObject>
27 #include <QSvgRenderer>
28 
29 #include "qgis_gui.h"
30 
31 class QgsVectorLayer;
32 class QPaintEvent;
33 
34 #ifdef SIP_RUN
35 % ModuleHeaderCode
36 // For ConvertToSubClassCode.
37 #include <qgsrubberband.h>
38 % End
39 #endif
40 
48 class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
49 {
50  Q_OBJECT
51 
52 #ifdef SIP_RUN
54  if ( dynamic_cast<QgsRubberBand *>( sipCpp ) )
55  sipType = sipType_QgsRubberBand;
56  else
57  sipType = nullptr;
58  SIP_END
59 #endif
60  public:
61 
62  Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
63  Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor )
64  Q_PROPERTY( int iconSize READ iconSize WRITE setIconSize )
65  Q_PROPERTY( QColor secondaryStrokeColor READ secondaryStrokeColor WRITE setSecondaryStrokeColor )
66  Q_PROPERTY( int width READ width WRITE setWidth )
67 
69  enum IconType
70  {
71 
76 
81 
86 
91 
96 
101 
107 
113 
118  ICON_SVG
119  };
120 
130 
136  void setColor( const QColor &color );
137 
143  void setFillColor( const QColor &color );
144 
148  QColor fillColor() const { return mBrush.color(); }
149 
155  void setStrokeColor( const QColor &color );
156 
160  QColor strokeColor() const { return mPen.color(); }
161 
168  void setSecondaryStrokeColor( const QColor &color );
169 
173  QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
174 
179  void setWidth( int width );
180 
184  int width() const { return mPen.width(); }
185 
190  void setIcon( IconType icon );
191 
199  void setSvgIcon( const QString &path, QPoint drawOffset );
200 
201 
205  IconType icon() const { return mIconType; }
206 
210  void setIconSize( int iconSize );
211 
215  int iconSize() const { return mIconSize; }
216 
220  void setLineStyle( Qt::PenStyle penStyle );
221 
225  void setBrushStyle( Qt::BrushStyle brushStyle );
226 
232  void reset( QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::LineGeometry );
233 
242  void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0 );
243 
251  void closePoints( bool doUpdate = true, int geometryIndex = 0 );
252 
259  void removePoint( int index = 0, bool doUpdate = true, int geometryIndex = 0 );
260 
264  void removeLastPoint( int geometryIndex = 0, bool doUpdate = true );
265 
270  void movePoint( const QgsPointXY &p, int geometryIndex = 0 );
271 
276  void movePoint( int index, const QgsPointXY &p, int geometryIndex = 0 );
277 
283  int partSize( int geometryIndex ) const;
284 
293  void setToGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
294 
304  void setToGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
305 
310  void setToCanvasRectangle( QRect rect );
311 
323  void addGeometry( const QgsGeometry &geometry, QgsVectorLayer *layer );
324 
333  void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
334 
340  void setTranslationOffset( double dx, double dy );
341 
346  int size() const;
347 
352  int numberOfVertices() const;
353 
359  const QgsPointXY *getPoint( int i, int j = 0 ) const;
360 
365  QgsGeometry asGeometry() const;
366 
367  void updatePosition() override;
368 
369  protected:
370 
375  void paint( QPainter *p ) override;
376 
382  void drawShape( QPainter *p, const QVector<QPointF> &pts );
383 
385  void updateRect();
386 
387  private:
388  QBrush mBrush;
389  QPen mPen;
390  QPen mSecondaryPen;
391 
393  int mIconSize = 5;
394 
396  IconType mIconType = ICON_CIRCLE;
397  std::unique_ptr<QSvgRenderer> mSvgRenderer;
398  QPoint mSvgOffset;
399 
403  QList< QList <QgsPointXY> > mPoints;
405  double mTranslationOffsetX = 0.0;
406  double mTranslationOffsetY = 0.0;
407 
408  QgsRubberBand();
409 
410  static QgsPolylineXY getPolyline( const QList<QgsPointXY> &points );
411 
412 };
413 
414 #endif
A cross is used to highlight points (x)
Definition: qgsrubberband.h:85
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
IconType icon() const
Returns the current icon type to highlight point geometries.
A class to represent a 2D point.
Definition: qgspointxy.h:43
An abstract class for items that can be placed on the map canvas.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
A diamond is used to highlight points (◇)
const QgsCoordinateReferenceSystem & crs
A cross is used to highlight points (+)
Definition: qgsrubberband.h:80
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:75
int width() const
Returns the current width of the line or stroke width for polygon.
A circle is used to highlight points (○)
Definition: qgsrubberband.h:95
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:48
#define SIP_END
Definition: qgis_sip.h:189
QColor fillColor() const
Returns the current fill color.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window&#39;s toolbar icons.
virtual void paint(QPainter *painter)=0
function to be implemented by derived classes
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
QColor secondaryStrokeColor() const
Returns the current secondary stroke color.
A box is used to highlight points (□)
Definition: qgsrubberband.h:90
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:50
int iconSize() const
Returns the current icon size of the point icons.
This class represents a coordinate reference system (CRS).
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
A diamond is used to highlight points (◆)
No icon is used.
Definition: qgsrubberband.h:75
QColor strokeColor() const
Returns the current stroke color.
Represents a vector layer which manages a vector based data sets.
virtual void updatePosition()
called on changed extent or resize event to update position of the item
A full box is used to highlight points (■)