QGIS API Documentation 4.3.0-Master (6402a64e93b)
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 <QPointer>
28#include <QPolygon>
29#include <QSvgRenderer>
30#include <QVector>
31
32class QgsMapLayer;
33class QgsVectorLayer;
34class QgsMapLayer;
35class QPaintEvent;
36class QgsSymbol;
37
38#ifdef SIP_RUN
39//%ModuleHeaderCode
40// For ConvertToSubClassCode.
41#include <qgsrubberband.h>
42//%End
43#endif
44
45class QgsRubberBand;
46
54{
55 public:
56 virtual ~QgsRubberBandPreviewItem() = default;
57
62
66 virtual void render( QgsRenderContext &context ) = 0;
67
68 protected:
73
74 private:
75 QPointer< QgsRubberBand > mRubberBand;
76};
77
78
86#ifndef SIP_RUN
87class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
88{
89#else
90class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
91{
92#endif
93 Q_OBJECT
94
95#ifdef SIP_RUN
97 if ( dynamic_cast<QgsRubberBand *>( sipCpp ) )
98 {
99 sipType = sipType_QgsRubberBand;
100 // We need to tweak the pointer as sip believes it is single inheritance
101 // from QgsMapCanvasItem, but the raw address of QgsRubberBand (sipCpp)
102 // is actually a QObject
103 *sipCppRet = dynamic_cast<QgsRubberBand *>( sipCpp );
104 }
105 else
106 sipType = nullptr;
107 SIP_END
108#endif
109 public:
110 Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
111 Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor )
112 Q_PROPERTY( int iconSize READ iconSize WRITE setIconSize )
114 Q_PROPERTY( int width READ width WRITE setWidth )
115
124 QgsRubberBand( QgsMapCanvas *mapCanvas SIP_TRANSFERTHIS, Qgis::GeometryType geometryType = Qgis::GeometryType::Line );
125 ~QgsRubberBand() override;
126
132 void setColor( const QColor &color );
133
138 void setFillColor( const QColor &color );
139
143 QColor fillColor() const { return mBrush.color(); }
144
149 void setStrokeColor( const QColor &color );
150
154 QColor strokeColor() const { return mPen.color(); }
155
161 void setSecondaryStrokeColor( const QColor &color );
162
166 QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
167
172 void setWidth( double width );
173
177 int width() const { return mPen.width(); }
178
183 void setIcon( Qgis::RubberBandIconType icon );
184
192 void setSvgIcon( const QString &path, QPoint drawOffset );
193
194
198 Qgis::RubberBandIconType icon() const { return mIconType; }
199
203 void setIconSize( double iconSize );
204
208 double iconSize() const { return mIconSize; }
209
213 void setLineStyle( Qt::PenStyle penStyle );
214
218 void setBrushStyle( Qt::BrushStyle brushStyle );
219
225 void reset( Qgis::GeometryType geometryType = Qgis::GeometryType::Line );
226
235 void addPreviewItem( QgsRubberBandPreviewItem *item SIP_TRANSFER );
236
243 void clearPreviewItems();
244
254 void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
255
263 void closePoints( bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
264
272 void removePoint( int index = 0, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
273
277 void removeLastPoint( int geometryIndex = 0, bool doUpdate = true, int ringIndex = 0 );
278
283 void movePoint( const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
284
289 void movePoint( int index, const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
290
296 int partSize( int geometryIndex ) const;
297
306 void setToGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
307
317 void setToGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
318
323 void setToCanvasRectangle( QRect rect );
324
330 void copyPointsFrom( const QgsRubberBand *other );
331
346 void addGeometry( const QgsGeometry &geometry, QgsMapLayer *layer, bool doUpdate = true );
347
358 void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), bool doUpdate = true );
359
365 void setTranslationOffset( double dx, double dy );
366
371 int size() const;
372
377 int numberOfVertices() const;
378
379 // TODO QGIS 5: rename i to geometryIndex, j to vertexIndex
380 // TODO QGIS 5: reorder parameters to geom, ring, ring
381
388 const QgsPointXY *getPoint( int i, int j = 0, int ringIndex = 0 ) const;
389
394 QgsGeometry asGeometry() const;
395
396 void updatePosition() override;
397
404 QgsSymbol *symbol() const;
405
419 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
420
427 Qgis::RubberBandComponents renderedComponents() const;
428
435 void setRenderedComponents( Qgis::RubberBandComponents components );
436
437 protected:
439
444 void paint( QPainter *p ) override;
445
451 void drawShape( QPainter *p, const QVector<QPointF> &pts );
452
458 void drawShape( QPainter *p, const QVector<QPolygonF> &rings );
459
461 void updateRect();
462
463 private:
464 QBrush mBrush;
465 QPen mPen;
466 QPen mSecondaryPen;
467
469 double mIconSize = 5;
470
474 std::unique_ptr<QSvgRenderer> mSvgRenderer;
475 QPoint mSvgOffset;
476
477 std::unique_ptr<QgsSymbol> mSymbol;
478
482 QVector<QVector<QVector<QgsPointXY>>> mPoints;
484 double mTranslationOffsetX = 0.0;
485 double mTranslationOffsetY = 0.0;
486
487 std::vector<std::unique_ptr<QgsRubberBandPreviewItem>> mPreviewItems;
488
490};
491
492#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
QFlags< RubberBandComponent > RubberBandComponents
Rubber band components.
Definition qgis.h:7030
RubberBandIconType
Rubber band icon type.
Definition qgis.h:7000
@ Circle
A circle is used to highlight points (○).
Definition qgis.h:7005
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:379
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Symbol
Base symbol component.
Definition qgis.h:7020
@ PreviewItems
Preview overlayer items.
Definition qgis.h:7021
Represents a coordinate reference system (CRS).
A geometry is the spatial representation of a feature.
virtual void paint(QPainter *painter)=0
function to be implemented by derived classes
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:83
Represents a 2D point.
Definition qgspointxy.h:62
Contains information about the context of a rendering operation.
Abstract interface for rendering custom preview overlays attached to a QgsRubberBand.
QgsRubberBandPreviewItem(QgsRubberBand *rubberBand)
Constructor for a QgsRubberBandPreviewItem, attached to the specified rubberBand.
virtual void render(QgsRenderContext &context)=0
Renders the custom preview overlay using the specified render context.
virtual ~QgsRubberBandPreviewItem()=default
QgsRubberBand * rubberBand()
Returns the rubber band associated with the item.
Responsible for drawing transient features (e.g.
void setIconSize(double iconSize)
Sets the size of the point icons.
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.
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.
QColor secondaryStrokeColor() const
Returns the current secondary stroke color.
Qgis::RubberBandIconType icon() const
Returns the current icon type to highlight point geometries.
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:227
Represents a vector layer which manages a vector based dataset.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:198
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_END
Definition qgis_sip.h:215