QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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 "qgis_gui.h"
19#include "qgis_sip.h"
21#include "qgsgeometry.h"
22#include "qgsmapcanvasitem.h"
23
24#include <QBrush>
25#include <QObject>
26#include <QPen>
27#include <QPolygon>
28#include <QSvgRenderer>
29#include <QVector>
30
31class QgsMapLayer;
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 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 )
79 Q_PROPERTY( int width READ width WRITE setWidth )
80
131
141 ~QgsRubberBand() override;
142
148 void setColor( const QColor &color );
149
154 void setFillColor( const QColor &color );
155
159 QColor fillColor() const { return mBrush.color(); }
160
165 void setStrokeColor( const QColor &color );
166
170 QColor strokeColor() const { return mPen.color(); }
171
177 void setSecondaryStrokeColor( const QColor &color );
178
182 QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
183
188 void setWidth( double width );
189
193 int width() const { return mPen.width(); }
194
199 void setIcon( IconType icon );
200
208 void setSvgIcon( const QString &path, QPoint drawOffset );
209
210
214 IconType icon() const { return mIconType; }
215
219 void setIconSize( double iconSize );
220
224 double iconSize() const { return mIconSize; }
225
229 void setLineStyle( Qt::PenStyle penStyle );
230
234 void setBrushStyle( Qt::BrushStyle brushStyle );
235
241 void reset( Qgis::GeometryType geometryType = Qgis::GeometryType::Line );
242
252 void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
253
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
356 void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), bool doUpdate = true );
357
363 void setTranslationOffset( double dx, double dy );
364
369 int size() const;
370
375 int numberOfVertices() const;
376
377 // TODO QGIS 4: rename i to geometryIndex, j to vertexIndex
378 // TODO QGIS 4: reorder parameters to geom, ring, ring
379
386 const QgsPointXY *getPoint( int i, int j = 0, int ringIndex = 0 ) const;
387
392 QgsGeometry asGeometry() const;
393
394 void updatePosition() override;
395
402 QgsSymbol *symbol() const;
403
417 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
418
419 protected:
424 void paint( QPainter *p ) override;
425
431 void drawShape( QPainter *p, const QVector<QPointF> &pts );
432
438 void drawShape( QPainter *p, const QVector<QPolygonF> &rings );
439
441 void updateRect();
442
443 private:
444 QBrush mBrush;
445 QPen mPen;
446 QPen mSecondaryPen;
447
449 double mIconSize = 5;
450
452 IconType mIconType = ICON_CIRCLE;
453 std::unique_ptr<QSvgRenderer> mSvgRenderer;
454 QPoint mSvgOffset;
455
456 std::unique_ptr<QgsSymbol> mSymbol;
457
461 QVector<QVector<QVector<QgsPointXY>>> mPoints;
463 double mTranslationOffsetX = 0.0;
464 double mTranslationOffsetY = 0.0;
465
467};
468
469#endif
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:358
@ Line
Lines.
Definition qgis.h:360
@ Polygon
Polygons.
Definition qgis.h:361
Represents a coordinate reference system (CRS).
A geometry is the spatial representation of a feature.
QgsMapCanvasItem(QgsMapCanvas *mapCanvas)
protected constructor: cannot be constructed directly
Map canvas is a class for displaying all GIS data types on a canvas.
Base class for all map layer types.
Definition qgsmaplayer.h:80
Represents a 2D point.
Definition qgspointxy.h:60
Responsible for drawing transient features (e.g.
void setIconSize(double iconSize)
Sets the size of the point icons.
~QgsRubberBand() override
QgsRubberBand(QgsMapCanvas *mapCanvas, Qgis::GeometryType geometryType=Qgis::GeometryType::Line)
Creates a new RubberBand.
double iconSize() const
Returns the current icon size of the point icons.
QColor fillColor() const
Returns the current fill color.
IconType icon() const
Returns the current icon type to highlight point geometries.
void setWidth(double width)
Sets the width of the line.
void setSecondaryStrokeColor(const QColor &color)
Sets a secondary stroke color for the rubberband which will be drawn under the main stroke color.
@ ICON_X
A cross is used to highlight points (x).
@ 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.
@ ICON_CROSS
A cross is used to highlight points (+).
@ ICON_SVG
An svg image is used to highlight points.
@ 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.
void setColor(const QColor &color)
Sets the color for the rubberband.
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
QColor secondaryStrokeColor
int width() const
Returns the current width of the line or stroke width for polygon.
QColor strokeColor() const
Returns the current stroke color.
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
Represents a vector layer which manages a vector based dataset.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:199
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_END
Definition qgis_sip.h:216