QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #include "qgscoordinatetransform.h"
22 
23 #include <QBrush>
24 #include <QVector>
25 #include <QPen>
26 #include <QPolygon>
27 #include <QObject>
28 #include <QSvgRenderer>
29 
30 #include "qgis_gui.h"
31 
32 class QgsVectorLayer;
33 class QPaintEvent;
34 class QgsSymbol;
35 
36 #ifdef SIP_RUN
37 % ModuleHeaderCode
38 // For ConvertToSubClassCode.
39 #include <qgsrubberband.h>
40 % End
41 #endif
42 
50 #ifndef SIP_RUN
51 class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
52 {
53 #else
54 class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
55 {
56 #endif
57  Q_OBJECT
58 
59 #ifdef SIP_RUN
61  if ( dynamic_cast<QgsRubberBand *>( sipCpp ) )
62  sipType = sipType_QgsRubberBand;
63  else
64  sipType = nullptr;
65  SIP_END
66 #endif
67  public:
68 
69  Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
70  Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor )
71  Q_PROPERTY( int iconSize READ iconSize WRITE setIconSize )
72  Q_PROPERTY( QColor secondaryStrokeColor READ secondaryStrokeColor WRITE setSecondaryStrokeColor )
73  Q_PROPERTY( int width READ width WRITE setWidth )
74 
75 
76  enum IconType
77  {
78 
83 
88 
93 
98 
103 
108 
114 
120 
125  ICON_SVG
126  };
127 
137  ~QgsRubberBand() override;
138 
144  void setColor( const QColor &color );
145 
151  void setFillColor( const QColor &color );
152 
156  QColor fillColor() const { return mBrush.color(); }
157 
163  void setStrokeColor( const QColor &color );
164 
168  QColor strokeColor() const { return mPen.color(); }
169 
176  void setSecondaryStrokeColor( const QColor &color );
177 
181  QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
182 
187  void setWidth( int width );
188 
192  int width() const { return mPen.width(); }
193 
198  void setIcon( IconType icon );
199 
207  void setSvgIcon( const QString &path, QPoint drawOffset );
208 
209 
213  IconType icon() const { return mIconType; }
214 
218  void setIconSize( int iconSize );
219 
223  int iconSize() const { return mIconSize; }
224 
228  void setLineStyle( Qt::PenStyle penStyle );
229 
233  void setBrushStyle( Qt::BrushStyle brushStyle );
234 
240  void reset( QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::LineGeometry );
241 
251  void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
252 
261  void closePoints( bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
262 
270  void removePoint( int index = 0, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
271 
275  void removeLastPoint( int geometryIndex = 0, bool doUpdate = true, int ringIndex = 0 );
276 
281  void movePoint( const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
282 
287  void movePoint( int index, const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
288 
294  int partSize( int geometryIndex ) const;
295 
304  void setToGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
305 
315  void setToGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
316 
321  void setToCanvasRectangle( QRect rect );
322 
328  void copyPointsFrom( const QgsRubberBand *other );
329 
344  void addGeometry( const QgsGeometry &geometry, QgsMapLayer *layer, bool doUpdate = true );
345 
357  void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), bool doUpdate = true );
358 
364  void setTranslationOffset( double dx, double dy );
365 
370  int size() const;
371 
376  int numberOfVertices() const;
377 
378  // TODO QGIS 4: rename i to geometryIndex, j to vertexIndex
379  // TODO QGIS 4: reorder parameters to geom, ring, ring
380 
387  const QgsPointXY *getPoint( int i, int j = 0, int ringIndex = 0 ) const;
388 
393  QgsGeometry asGeometry() const;
394 
395  void updatePosition() override;
396 
403  QgsSymbol *symbol() const;
404 
418  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
419 
420  protected:
421 
426  void paint( QPainter *p ) override;
427 
433  void drawShape( QPainter *p, const QVector<QPointF> &pts );
434 
440  void drawShape( QPainter *p, const QVector<QPolygonF> &rings );
441 
443  void updateRect();
444 
445  private:
446  QBrush mBrush;
447  QPen mPen;
448  QPen mSecondaryPen;
449 
451  int mIconSize = 5;
452 
454  IconType mIconType = ICON_CIRCLE;
455  std::unique_ptr<QSvgRenderer> mSvgRenderer;
456  QPoint mSvgOffset;
457 
458  std::unique_ptr< QgsSymbol > mSymbol;
459 
463  QVector< QVector< QVector <QgsPointXY> > > mPoints;
465  double mTranslationOffsetX = 0.0;
466  double mTranslationOffsetY = 0.0;
467 
468  QgsRubberBand();
469 
470 };
471 
472 #endif
This class represents a coordinate reference system (CRS).
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
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:90
Base class for all map layer types.
Definition: qgsmaplayer.h:73
A class to represent a 2D point.
Definition: qgspointxy.h:59
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:52
~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:92
@ 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:82
@ ICON_CROSS
A cross is used to highlight points (+)
Definition: qgsrubberband.h:87
@ 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:97
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