QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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"
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
32class QgsVectorLayer;
33class QgsMapLayer;
34class QPaintEvent;
35class QgsSymbol;
36
37#ifdef SIP_RUN
38% ModuleHeaderCode
39// For ConvertToSubClassCode.
40#include <qgsrubberband.h>
41% End
42#endif
43
51#ifndef SIP_RUN
52class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
53{
54#else
55class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
56{
57#endif
58 Q_OBJECT
59
60#ifdef SIP_RUN
62 if ( dynamic_cast<QgsRubberBand *>( sipCpp ) )
63 {
64 sipType = sipType_QgsRubberBand;
65 // We need to tweak the pointer as sip believes it is single inheritance
66 // from QgsMapCanvasItem, but the raw address of QgsRubberBand (sipCpp)
67 // is actually a QObject
68 *sipCppRet = dynamic_cast<QgsRubberBand *>( sipCpp );
69 }
70 else
71 sipType = nullptr;
73#endif
74 public:
75
76 Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
77 Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor )
78 Q_PROPERTY( int iconSize READ iconSize WRITE setIconSize )
79 Q_PROPERTY( QColor secondaryStrokeColor READ secondaryStrokeColor WRITE setSecondaryStrokeColor )
80 Q_PROPERTY( int width READ width WRITE setWidth )
81
82
84 {
85
90
95
100
105
110
115
121
127
132 ICON_SVG
133 };
134
143 QgsRubberBand( QgsMapCanvas *mapCanvas SIP_TRANSFERTHIS, Qgis::GeometryType geometryType = Qgis::GeometryType::Line );
144 ~QgsRubberBand() override;
145
151 void setColor( const QColor &color );
152
158 void setFillColor( const QColor &color );
159
163 QColor fillColor() const { return mBrush.color(); }
164
170 void setStrokeColor( const QColor &color );
171
175 QColor strokeColor() const { return mPen.color(); }
176
183 void setSecondaryStrokeColor( const QColor &color );
184
188 QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
189
194 void setWidth( int width );
195
199 int width() const { return mPen.width(); }
200
205 void setIcon( IconType icon );
206
214 void setSvgIcon( const QString &path, QPoint drawOffset );
215
216
220 IconType icon() const { return mIconType; }
221
225 void setIconSize( int iconSize );
226
230 int iconSize() const { return mIconSize; }
231
235 void setLineStyle( Qt::PenStyle penStyle );
236
240 void setBrushStyle( Qt::BrushStyle brushStyle );
241
247 void reset( Qgis::GeometryType geometryType = Qgis::GeometryType::Line );
248
258 void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
259
268 void closePoints( bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
269
277 void removePoint( int index = 0, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
278
282 void removeLastPoint( int geometryIndex = 0, bool doUpdate = true, int ringIndex = 0 );
283
288 void movePoint( const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
289
294 void movePoint( int index, const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
295
301 int partSize( int geometryIndex ) const;
302
311 void setToGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
312
322 void setToGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
323
328 void setToCanvasRectangle( QRect rect );
329
335 void copyPointsFrom( const QgsRubberBand *other );
336
351 void addGeometry( const QgsGeometry &geometry, QgsMapLayer *layer, bool doUpdate = true );
352
364 void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), bool doUpdate = true );
365
371 void setTranslationOffset( double dx, double dy );
372
377 int size() const;
378
383 int numberOfVertices() const;
384
385 // TODO QGIS 4: rename i to geometryIndex, j to vertexIndex
386 // TODO QGIS 4: reorder parameters to geom, ring, ring
387
394 const QgsPointXY *getPoint( int i, int j = 0, int ringIndex = 0 ) const;
395
400 QgsGeometry asGeometry() const;
401
402 void updatePosition() override;
403
410 QgsSymbol *symbol() const;
411
425 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
426
427 protected:
428
433 void paint( QPainter *p ) override;
434
440 void drawShape( QPainter *p, const QVector<QPointF> &pts );
441
447 void drawShape( QPainter *p, const QVector<QPolygonF> &rings );
448
450 void updateRect();
451
452 private:
453 QBrush mBrush;
454 QPen mPen;
455 QPen mSecondaryPen;
456
458 int mIconSize = 5;
459
461 IconType mIconType = ICON_CIRCLE;
462 std::unique_ptr<QSvgRenderer> mSvgRenderer;
463 QPoint mSvgOffset;
464
465 std::unique_ptr< QgsSymbol > mSymbol;
466
470 QVector< QVector< QVector <QgsPointXY> > > mPoints;
471 Qgis::GeometryType mGeometryType = Qgis::GeometryType::Polygon;
472 double mTranslationOffsetX = 0.0;
473 double mTranslationOffsetY = 0.0;
474
476
477};
478
479#endif
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition: qgis.h:227
This class represents a coordinate reference system (CRS).
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
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:53
~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:99
@ 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:89
@ ICON_CROSS
A cross is used to highlight points (+)
Definition: qgsrubberband.h:94
@ 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 (□)
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:94
Represents a vector layer which manages a vector based data sets.
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:186
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:203
const QgsCoordinateReferenceSystem & crs