QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
34 #ifdef SIP_RUN
35 % ModuleHeaderCode
36 // For ConvertToSubClassCode.
37 #include <qgsrubberband.h>
38 % End
39 #endif
40 
48 #ifndef SIP_RUN
49 class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
50 {
51 #else
52 class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
53 {
54 #endif
55  Q_OBJECT
56 
57 #ifdef SIP_RUN
59  if ( dynamic_cast<QgsRubberBand *>( sipCpp ) )
60  sipType = sipType_QgsRubberBand;
61  else
62  sipType = nullptr;
63  SIP_END
64 #endif
65  public:
66 
67  Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
68  Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor )
69  Q_PROPERTY( int iconSize READ iconSize WRITE setIconSize )
70  Q_PROPERTY( QColor secondaryStrokeColor READ secondaryStrokeColor WRITE setSecondaryStrokeColor )
71  Q_PROPERTY( int width READ width WRITE setWidth )
72 
73 
74  enum IconType
75  {
76 
81 
86 
91 
96 
101 
106 
112 
118 
123  ICON_SVG
124  };
125 
135 
141  void setColor( const QColor &color );
142 
148  void setFillColor( const QColor &color );
149 
153  QColor fillColor() const { return mBrush.color(); }
154 
160  void setStrokeColor( const QColor &color );
161 
165  QColor strokeColor() const { return mPen.color(); }
166 
173  void setSecondaryStrokeColor( const QColor &color );
174 
178  QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
179 
184  void setWidth( int width );
185 
189  int width() const { return mPen.width(); }
190 
195  void setIcon( IconType icon );
196 
204  void setSvgIcon( const QString &path, QPoint drawOffset );
205 
206 
210  IconType icon() const { return mIconType; }
211 
215  void setIconSize( int iconSize );
216 
220  int iconSize() const { return mIconSize; }
221 
225  void setLineStyle( Qt::PenStyle penStyle );
226 
230  void setBrushStyle( Qt::BrushStyle brushStyle );
231 
237  void reset( QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::LineGeometry );
238 
248  void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
249 
258  void closePoints( bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
259 
267  void removePoint( int index = 0, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
268 
272  void removeLastPoint( int geometryIndex = 0, bool doUpdate = true, int ringIndex = 0 );
273 
278  void movePoint( const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
279 
284  void movePoint( int index, const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
285 
291  int partSize( int geometryIndex ) const;
292 
301  void setToGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
302 
312  void setToGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
313 
318  void setToCanvasRectangle( QRect rect );
319 
331  void addGeometry( const QgsGeometry &geometry, QgsVectorLayer *layer );
332 
341  void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
342 
348  void setTranslationOffset( double dx, double dy );
349 
354  int size() const;
355 
360  int numberOfVertices() const;
361 
362  // TODO QGIS 4: rename i to geometryIndex, j to vertexIndex
363  // TODO QGIS 4: reorder parameters to geom, ring, ring
364 
371  const QgsPointXY *getPoint( int i, int j = 0, int ringIndex = 0 ) const;
372 
377  QgsGeometry asGeometry() const;
378 
379  void updatePosition() override;
380 
381  protected:
382 
387  void paint( QPainter *p ) override;
388 
394  void drawShape( QPainter *p, const QVector<QPointF> &pts );
395 
401  void drawShape( QPainter *p, const QVector<QPolygonF> &rings );
402 
404  void updateRect();
405 
406  private:
407  QBrush mBrush;
408  QPen mPen;
409  QPen mSecondaryPen;
410 
412  int mIconSize = 5;
413 
415  IconType mIconType = ICON_CIRCLE;
416  std::unique_ptr<QSvgRenderer> mSvgRenderer;
417  QPoint mSvgOffset;
418 
422  QVector< QVector< QVector <QgsPointXY> > > mPoints;
424  double mTranslationOffsetX = 0.0;
425  double mTranslationOffsetY = 0.0;
426 
427  QgsRubberBand();
428 
429 };
430 
431 #endif
QgsRubberBand::width
int width() const
Returns the current width of the line or stroke width for polygon.
Definition: qgsrubberband.h:189
QgsRubberBand::iconSize
int iconSize() const
Returns the current icon size of the point icons.
Definition: qgsrubberband.h:220
QgsRubberBand::ICON_CIRCLE
@ ICON_CIRCLE
A circle is used to highlight points (○)
Definition: qgsrubberband.h:100
QgsRubberBand
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:50
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsRubberBand::ICON_BOX
@ ICON_BOX
A box is used to highlight points (□)
Definition: qgsrubberband.h:95
QgsRubberBand::IconType
IconType
Icons.
Definition: qgsrubberband.h:75
QgsRubberBand::ICON_NONE
@ ICON_NONE
No icon is used.
Definition: qgsrubberband.h:80
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsGuiUtils::iconSize
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
Definition: qgsguiutils.cpp:250
qgsrubberband.h
QgsRubberBand::ICON_FULL_DIAMOND
@ ICON_FULL_DIAMOND
A diamond is used to highlight points (◆)
Definition: qgsrubberband.h:117
QgsMapCanvasItem::updatePosition
virtual void updatePosition()
called on changed extent or resize event to update position of the item
Definition: qgsmapcanvasitem.cpp:135
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
QgsMapCanvasItem::paint
virtual void paint(QPainter *painter)=0
function to be implemented by derived classes
qgsmapcanvasitem.h
QgsMapCanvasItem
An abstract class for items that can be placed on the map canvas.
Definition: qgsmapcanvasitem.h:34
QgsRubberBand::fillColor
QColor fillColor() const
Returns the current fill color.
Definition: qgsrubberband.h:153
qgis_sip.h
QgsRubberBand::ICON_X
@ ICON_X
A cross is used to highlight points (x)
Definition: qgsrubberband.h:90
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsRubberBand::ICON_FULL_BOX
@ ICON_FULL_BOX
A full box is used to highlight points (■)
Definition: qgsrubberband.h:105
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsWkbTypes::LineGeometry
@ LineGeometry
Definition: qgswkbtypes.h:143
qgsgeometry.h
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsRubberBand::ICON_CROSS
@ ICON_CROSS
A cross is used to highlight points (+)
Definition: qgsrubberband.h:85
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsRubberBand::icon
IconType icon() const
Returns the current icon type to highlight point geometries.
Definition: qgsrubberband.h:210
QgsRubberBand::secondaryStrokeColor
QColor secondaryStrokeColor() const
Returns the current secondary stroke color.
Definition: qgsrubberband.h:178
QgsRubberBand::ICON_DIAMOND
@ ICON_DIAMOND
A diamond is used to highlight points (◇)
Definition: qgsrubberband.h:111
SIP_END
#define SIP_END
Definition: qgis_sip.h:194
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsRubberBand::strokeColor
QColor strokeColor() const
Returns the current stroke color.
Definition: qgsrubberband.h:165