QGIS API Documentation 4.3.0-Master (389d690bb77)
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
76 virtual QRectF boundingRect() const;
77
78 protected:
83
84 private:
85 QPointer< QgsRubberBand > mRubberBand;
86};
87
88
96#ifndef SIP_RUN
97class GUI_EXPORT QgsRubberBand : public QObject, public QgsMapCanvasItem
98{
99#else
100class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
101{
102#endif
103 Q_OBJECT
104
105#ifdef SIP_RUN
107 if ( dynamic_cast<QgsRubberBand *>( sipCpp ) )
108 {
109 sipType = sipType_QgsRubberBand;
110 // We need to tweak the pointer as sip believes it is single inheritance
111 // from QgsMapCanvasItem, but the raw address of QgsRubberBand (sipCpp)
112 // is actually a QObject
113 *sipCppRet = dynamic_cast<QgsRubberBand *>( sipCpp );
114 }
115 else
116 sipType = nullptr;
117 SIP_END
118#endif
119 public:
120 Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
121 Q_PROPERTY( QColor strokeColor READ strokeColor WRITE setStrokeColor )
122 Q_PROPERTY( int iconSize READ iconSize WRITE setIconSize )
124 Q_PROPERTY( int width READ width WRITE setWidth )
125
134 QgsRubberBand( QgsMapCanvas *mapCanvas SIP_TRANSFERTHIS, Qgis::GeometryType geometryType = Qgis::GeometryType::Line );
135 ~QgsRubberBand() override;
136
142 void setColor( const QColor &color );
143
148 void setFillColor( const QColor &color );
149
153 QColor fillColor() const { return mBrush.color(); }
154
159 void setStrokeColor( const QColor &color );
160
164 QColor strokeColor() const { return mPen.color(); }
165
171 void setSecondaryStrokeColor( const QColor &color );
172
176 QColor secondaryStrokeColor() const { return mSecondaryPen.color(); }
177
182 void setWidth( double width );
183
187 int width() const { return mPen.width(); }
188
193 void setIcon( Qgis::RubberBandIconType icon );
194
202 void setSvgIcon( const QString &path, QPoint drawOffset );
203
204
208 Qgis::RubberBandIconType icon() const { return mIconType; }
209
213 void setIconSize( double iconSize );
214
218 double iconSize() const { return mIconSize; }
219
223 void setLineStyle( Qt::PenStyle penStyle );
224
228 void setBrushStyle( Qt::BrushStyle brushStyle );
229
235 void reset( Qgis::GeometryType geometryType = Qgis::GeometryType::Line );
236
245 void addPreviewItem( QgsRubberBandPreviewItem *item SIP_TRANSFER );
246
253 void clearPreviewItems();
254
264 void addPoint( const QgsPointXY &p, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
265
273 void closePoints( bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
274
282 void removePoint( int index = 0, bool doUpdate = true, int geometryIndex = 0, int ringIndex = 0 );
283
287 void removeLastPoint( int geometryIndex = 0, bool doUpdate = true, int ringIndex = 0 );
288
293 void movePoint( const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
294
299 void movePoint( int index, const QgsPointXY &p, int geometryIndex = 0, int ringIndex = 0 );
300
306 int partSize( int geometryIndex ) const;
307
316 void setToGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
317
327 void setToGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
328
333 void setToCanvasRectangle( QRect rect );
334
340 void copyPointsFrom( const QgsRubberBand *other );
341
356 void addGeometry( const QgsGeometry &geometry, QgsMapLayer *layer, bool doUpdate = true );
357
368 void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), bool doUpdate = true );
369
375 void setTranslationOffset( double dx, double dy );
376
381 int size() const;
382
387 int numberOfVertices() const;
388
389 // TODO QGIS 5: rename i to geometryIndex, j to vertexIndex
390 // TODO QGIS 5: reorder parameters to geom, ring, ring
391
398 const QgsPointXY *getPoint( int i, int j = 0, int ringIndex = 0 ) const;
399
404 QgsGeometry asGeometry() const;
405
406 void updatePosition() override;
407
414 QgsSymbol *symbol() const;
415
429 void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
430
437 Qgis::RubberBandComponents renderedComponents() const;
438
445 void setRenderedComponents( Qgis::RubberBandComponents components );
446 QRectF boundingRect() const override;
447
448 protected:
450
455 void paint( QPainter *p ) override;
456
462 void drawShape( QPainter *p, const QVector<QPointF> &pts );
463
469 void drawShape( QPainter *p, const QVector<QPolygonF> &rings );
470
472 void updateRect();
473
474 private:
475 QBrush mBrush;
476 QPen mPen;
477 QPen mSecondaryPen;
478
480 double mIconSize = 5;
481
485 std::unique_ptr<QSvgRenderer> mSvgRenderer;
486 QPoint mSvgOffset;
487
488 std::unique_ptr<QgsSymbol> mSymbol;
489
493 QVector<QVector<QVector<QgsPointXY>>> mPoints;
495 double mTranslationOffsetX = 0.0;
496 double mTranslationOffsetY = 0.0;
497
498 std::vector<std::unique_ptr<QgsRubberBandPreviewItem>> mPreviewItems;
499
501};
502
503#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
QFlags< RubberBandComponent > RubberBandComponents
Rubber band components.
Definition qgis.h:7074
RubberBandIconType
Rubber band icon type.
Definition qgis.h:7044
@ Circle
A circle is used to highlight points (○).
Definition qgis.h:7049
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:7064
@ PreviewItems
Preview overlayer items.
Definition qgis.h:7065
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.
virtual QRectF boundingRect() const
Returns the bounding rect required for drawing the preview 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:203
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_END
Definition qgis_sip.h:220