QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 <QVector>
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 class QgsSymbol;
34 
35 #ifdef SIP_RUN
36 % ModuleHeaderCode
37 // For ConvertToSubClassCode.
38 #include <qgsrubberband.h>
39 % End
40 #endif
41 
49 #ifndef SIP_RUN
50 class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
51 {
52 #else
53 class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
54 {
55 #endif
56  Q_OBJECT
57 
58 #ifdef SIP_RUN
60  if ( dynamic_cast<QgsRubberBand *>( sipCpp ) )
61  sipType = sipType_QgsRubberBand;
62  else
63  sipType = nullptr;
64  SIP_END
65 #endif
66  public:
67 
68  Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
69  Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor )
70  Q_PROPERTY( int iconSize READ iconSize WRITE setIconSize )
71  Q_PROPERTY( QColor secondaryStrokeColor READ secondaryStrokeColor WRITE setSecondaryStrokeColor )
72  Q_PROPERTY( int width READ width WRITE setWidth )
73 
74 
75  enum IconType
76  {
77 
82 
87 
92 
97 
102 
107 
113 
119 
124  ICON_SVG
125  };
126 
136  ~QgsRubberBand() override;
137 
143  void setColor( const QColor &color );
144 
150  void setFillColor( const QColor &color );
151 
155  QColor fillColor() const { return mBrush.color(); }
156 
162  void setStrokeColor( const QColor &color );
163 
167  QColor strokeColor() const { return mPen.color(); }
168 
175  void setSecondaryStrokeColor( const QColor &color );
176 
180  QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
181 
186  void setWidth( int width );
187 
191  int width() const { return mPen.width(); }
192 
197  void setIcon( IconType icon );
198 
206  void setSvgIcon( const QString &path, QPoint drawOffset );
207 
208 
212  IconType icon() const { return mIconType; }
213 
217  void setIconSize( int iconSize );
218 
222  int iconSize() const { return mIconSize; }
223 
227  void setLineStyle( Qt::PenStyle penStyle );
228 
232  void setBrushStyle( Qt::BrushStyle brushStyle );
233 
239  void reset( QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::LineGeometry );
240 
250  void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
251 
260  void closePoints( bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
261 
269  void removePoint( int index = 0, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
270 
274  void removeLastPoint( int geometryIndex = 0, bool doUpdate = true, int ringIndex = 0 );
275 
280  void movePoint( const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
281 
286  void movePoint( int index, const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
287 
293  int partSize( int geometryIndex ) const;
294 
303  void setToGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
304 
314  void setToGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
315 
320  void setToCanvasRectangle( QRect rect );
321 
337  void addGeometry( const QgsGeometry &geometry, QgsVectorLayer *layer, bool doUpdate = true );
338 
350  void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), bool doUpdate = true );
351 
357  void setTranslationOffset( double dx, double dy );
358 
363  int size() const;
364 
369  int numberOfVertices() const;
370 
371  // TODO QGIS 4: rename i to geometryIndex, j to vertexIndex
372  // TODO QGIS 4: reorder parameters to geom, ring, ring
373 
380  const QgsPointXY *getPoint( int i, int j = 0, int ringIndex = 0 ) const;
381 
386  QgsGeometry asGeometry() const;
387 
388  void updatePosition() override;
389 
396  QgsSymbol *symbol() const;
397 
411  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
412 
413  protected:
414 
419  void paint( QPainter *p ) override;
420 
426  void drawShape( QPainter *p, const QVector<QPointF> &pts );
427 
433  void drawShape( QPainter *p, const QVector<QPolygonF> &rings );
434 
436  void updateRect();
437 
438  private:
439  QBrush mBrush;
440  QPen mPen;
441  QPen mSecondaryPen;
442 
444  int mIconSize = 5;
445 
447  IconType mIconType = ICON_CIRCLE;
448  std::unique_ptr<QSvgRenderer> mSvgRenderer;
449  QPoint mSvgOffset;
450 
451  std::unique_ptr< QgsSymbol > mSymbol;
452 
456  QVector< QVector< QVector <QgsPointXY> > > mPoints;
458  double mTranslationOffsetX = 0.0;
459  double mTranslationOffsetY = 0.0;
460 
461  QgsRubberBand();
462 
463 };
464 
465 #endif
This class represents a coordinate reference system (CRS).
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
An abstract class for items that can be placed on the map canvas.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:86
A class to represent a 2D point.
Definition: qgspointxy.h:59
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:51
~QgsRubberBand() override
QColor fillColor() const
Returns the current fill color.
IconType icon() const
Returns the current icon type to highlight point geometries.
@ ICON_X
A cross is used to highlight points (x)
Definition: qgsrubberband.h:91
@ ICON_FULL_DIAMOND
A diamond is used to highlight points (◆)
@ ICON_FULL_BOX
A full box is used to highlight points (■)
@ ICON_NONE
No icon is used.
Definition: qgsrubberband.h:81
@ ICON_CROSS
A cross is used to highlight points (+)
Definition: qgsrubberband.h:86
@ ICON_CIRCLE
A circle is used to highlight points (○)
@ ICON_DIAMOND
A diamond is used to highlight points (◇)
@ ICON_BOX
A box is used to highlight points (□)
Definition: qgsrubberband.h:96
QColor secondaryStrokeColor() const
Returns the current secondary stroke color.
int iconSize() const
Returns the current icon size of the point icons.
int width() const
Returns the current width of the line or stroke width for polygon.
QColor strokeColor() const
Returns the current stroke color.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
Represents a vector layer which manages a vector based data sets.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:194
const QgsCoordinateReferenceSystem & crs