QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 QPaintEvent;
34class QgsSymbol;
35
36#ifdef SIP_RUN
37% ModuleHeaderCode
38// For ConvertToSubClassCode.
39#include <qgsrubberband.h>
40% End
41#endif
42
50#ifndef SIP_RUN
51class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
52{
53#else
54class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
55{
56#endif
57 Q_OBJECT
58
59#ifdef SIP_RUN
61 if ( dynamic_cast<QgsRubberBand *>( sipCpp ) )
62 {
63 sipType = sipType_QgsRubberBand;
64 // We need to tweak the pointer as sip believes it is single inheritance
65 // from QgsMapCanvasItem, but the raw address of QgsRubberBand (sipCpp)
66 // is actually a QObject
67 *sipCppRet = dynamic_cast<QgsRubberBand *>( sipCpp );
68 }
69 else
70 sipType = nullptr;
72#endif
73 public:
74
75 Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
76 Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor )
77 Q_PROPERTY( int iconSize READ iconSize WRITE setIconSize )
78 Q_PROPERTY( QColor secondaryStrokeColor READ secondaryStrokeColor WRITE setSecondaryStrokeColor )
79 Q_PROPERTY( int width READ width WRITE setWidth )
80
81
83 {
84
89
94
99
104
109
114
120
126
131 ICON_SVG
132 };
133
143 ~QgsRubberBand() override;
144
150 void setColor( const QColor &color );
151
157 void setFillColor( const QColor &color );
158
162 QColor fillColor() const { return mBrush.color(); }
163
169 void setStrokeColor( const QColor &color );
170
174 QColor strokeColor() const { return mPen.color(); }
175
182 void setSecondaryStrokeColor( const QColor &color );
183
187 QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
188
193 void setWidth( int width );
194
198 int width() const { return mPen.width(); }
199
204 void setIcon( IconType icon );
205
213 void setSvgIcon( const QString &path, QPoint drawOffset );
214
215
219 IconType icon() const { return mIconType; }
220
224 void setIconSize( int iconSize );
225
229 int iconSize() const { return mIconSize; }
230
234 void setLineStyle( Qt::PenStyle penStyle );
235
239 void setBrushStyle( Qt::BrushStyle brushStyle );
240
246 void reset( QgsWkbTypes::GeometryType geometryType = QgsWkbTypes::LineGeometry );
247
257 void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
258
267 void closePoints( bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
268
276 void removePoint( int index = 0, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
277
281 void removeLastPoint( int geometryIndex = 0, bool doUpdate = true, int ringIndex = 0 );
282
287 void movePoint( const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
288
293 void movePoint( int index, const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
294
300 int partSize( int geometryIndex ) const;
301
310 void setToGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
311
321 void setToGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
322
327 void setToCanvasRectangle( QRect rect );
328
334 void copyPointsFrom( const QgsRubberBand *other );
335
350 void addGeometry( const QgsGeometry &geometry, QgsMapLayer *layer, bool doUpdate = true );
351
363 void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), bool doUpdate = true );
364
370 void setTranslationOffset( double dx, double dy );
371
376 int size() const;
377
382 int numberOfVertices() const;
383
384 // TODO QGIS 4: rename i to geometryIndex, j to vertexIndex
385 // TODO QGIS 4: reorder parameters to geom, ring, ring
386
393 const QgsPointXY *getPoint( int i, int j = 0, int ringIndex = 0 ) const;
394
399 QgsGeometry asGeometry() const;
400
401 void updatePosition() override;
402
409 QgsSymbol *symbol() const;
410
424 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
425
426 protected:
427
432 void paint( QPainter *p ) override;
433
439 void drawShape( QPainter *p, const QVector<QPointF> &pts );
440
446 void drawShape( QPainter *p, const QVector<QPolygonF> &rings );
447
449 void updateRect();
450
451 private:
452 QBrush mBrush;
453 QPen mPen;
454 QPen mSecondaryPen;
455
457 int mIconSize = 5;
458
460 IconType mIconType = ICON_CIRCLE;
461 std::unique_ptr<QSvgRenderer> mSvgRenderer;
462 QPoint mSvgOffset;
463
464 std::unique_ptr< QgsSymbol > mSymbol;
465
469 QVector< QVector< QVector <QgsPointXY> > > mPoints;
471 double mTranslationOffsetX = 0.0;
472 double mTranslationOffsetY = 0.0;
473
475
476};
477
478#endif
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: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:98
@ 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:88
@ ICON_CROSS
A cross is used to highlight points (+)
Definition: qgsrubberband.h:93
@ 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:93
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: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