QGIS API Documentation  2.99.0-Master (7d4f81d)
Public Types | Public Member Functions | Protected Member Functions | List of all members
QgsRubberBand Class Reference

A class for drawing transient features (e.g. More...

#include <qgsrubberband.h>

Inheritance diagram for QgsRubberBand:
Inheritance graph
[legend]

Public Types

enum  IconType {
  ICON_NONE, ICON_CROSS, ICON_X, ICON_BOX,
  ICON_CIRCLE, ICON_FULL_BOX, ICON_DIAMOND, ICON_FULL_DIAMOND
}
 Icons. More...
 

Public Member Functions

 QgsRubberBand (QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
 Creates a new RubberBand. More...
 
void addGeometry (const QgsGeometry &geom, QgsVectorLayer *layer)
 Adds the geometry of an existing feature to a rubberband This is useful for multi feature highlighting. More...
 
void addPoint (const QgsPointXY &p, bool doUpdate=true, int geometryIndex=0)
 Adds a vertex to the rubberband and update canvas. More...
 
QgsGeometry asGeometry () const
 Returns the rubberband as a Geometry. More...
 
void closePoints (bool doUpdate=true, int geometryIndex=0)
 Ensures that a polygon geometry is closed and that the last vertex equals the first vertex. More...
 
QColor fillColor () const
 Returns the current fill color. More...
 
const QgsPointXYgetPoint (int i, int j=0) const
 Returns a vertex. More...
 
IconType icon () const
 Returns the current icon type to highlight point geometries. More...
 
int iconSize () const
 Returns the current icon size of the point icons. More...
 
void movePoint (const QgsPointXY &p, int geometryIndex=0)
 Moves the rubber band point specified by index. More...
 
void movePoint (int index, const QgsPointXY &p, int geometryIndex=0)
 Moves the rubber band point specified by index. More...
 
int numberOfVertices () const
 Returns count of vertices in all lists of mPoint. More...
 
int partSize (int geometryIndex) const
 Returns number of vertices in feature part. More...
 
void removeLastPoint (int geometryIndex=0, bool doUpdate=true)
 Removes the last point. More...
 
void removePoint (int index=0, bool doUpdate=true, int geometryIndex=0)
 Removes a vertex from the rubberband and (optionally) updates canvas. More...
 
void reset (QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
 Clears all the geometries in this rubberband. More...
 
QColor secondaryStrokeColor () const
 Returns the current secondary stroke color. More...
 
void setBrushStyle (Qt::BrushStyle brushStyle)
 Sets the style of the brush. More...
 
void setColor (const QColor &color)
 Sets the color for the rubberband. More...
 
void setFillColor (const QColor &color)
 Sets the fill color for the rubberband. More...
 
void setIcon (IconType icon)
 Sets the icon type to highlight point geometries. More...
 
void setIconSize (int iconSize)
 Sets the size of the point icons. More...
 
void setLineStyle (Qt::PenStyle penStyle)
 Sets the style of the line. More...
 
void setSecondaryStrokeColor (const QColor &color)
 Sets a secondary stroke color for the rubberband which will be drawn under the main stroke color. More...
 
void setStrokeColor (const QColor &color)
 Sets the stroke color for the rubberband. More...
 
void setToCanvasRectangle (QRect rect)
 Sets this rubber band to a map canvas rectangle. More...
 
void setToGeometry (const QgsGeometry &geom, QgsVectorLayer *layer)
 Sets this rubber band to the geometry of an existing feature. More...
 
void setTranslationOffset (double dx, double dy)
 Adds translation to original coordinates (all in map coordinates) More...
 
void setWidth (int width)
 Sets the width of the line. More...
 
int size () const
 Returns number of geometries. More...
 
QColor strokeColor () const
 Returns the current stroke color. More...
 
virtual void updatePosition () override
 called on changed extent or resize event to update position of the item More...
 
int width () const
 Returns the current width of the line or stroke width for polygon. More...
 
- Public Member Functions inherited from QgsMapCanvasItem
virtual QRectF boundingRect () const override
 
QgsRectangle rect () const
 returns canvas item rectangle in map units More...
 
void setRect (const QgsRectangle &r, bool resetRotation=true)
 sets canvas item rectangle in map units More...
 
QPointF toCanvasCoordinates (const QgsPointXY &point) const
 transformation from map coordinates to screen coordinates More...
 
QgsPointXY toMapCoordinates (QPoint point) const
 transformation from screen coordinates to map coordinates More...
 

Protected Member Functions

void drawShape (QPainter *p, QVector< QPointF > &pts)
 Draws shape of the rubber band. More...
 
virtual void paint (QPainter *p) override
 Paints the rubber band in response to an update event. More...
 
void updateRect ()
 Recalculates needed rectangle. More...
 
- Protected Member Functions inherited from QgsMapCanvasItem
 QgsMapCanvasItem (QgsMapCanvas *mapCanvas)
 protected constructor: cannot be constructed directly More...
 
virtual ~QgsMapCanvasItem ()
 
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) override
 
bool setRenderContextVariables (QPainter *p, QgsRenderContext &context) const
 Sets render context parameters. More...
 
void updateCanvas ()
 schedules map canvas for repaint More...
 

Additional Inherited Members

- Protected Attributes inherited from QgsMapCanvasItem
QSizeF mItemSize
 cached size of the item (to return in boundingRect()) More...
 
QgsMapCanvasmMapCanvas = nullptr
 pointer to map canvas More...
 
QgsRectangle mRect
 cached canvas item rectangle in map coordinates encodes position (xmin,ymax) and size (width/height) used to re-position and re-size the item on zoom/pan while waiting for the renderer to complete. More...
 
double mRectRotation
 

Detailed Description

A class for drawing transient features (e.g.

digitizing lines) on the map.

The QgsRubberBand class provides a transparent overlay widget for tracking the mouse while drawing polylines or polygons.

Definition at line 36 of file qgsrubberband.h.

Member Enumeration Documentation

◆ IconType

Icons.

Enumerator
ICON_NONE 

No icon is used.

ICON_CROSS 

A cross is used to highlight points (+)

ICON_X 

A cross is used to highlight points (x)

ICON_BOX 

A box is used to highlight points (□)

ICON_CIRCLE 

A circle is used to highlight points (○)

ICON_FULL_BOX 

A full box is used to highlight points (■)

ICON_DIAMOND 

A diamond is used to highlight points (◇)

Since
QGIS 3.0
ICON_FULL_DIAMOND 

A diamond is used to highlight points (◆)

Since
QGIS 3.0

Definition at line 41 of file qgsrubberband.h.

Constructor & Destructor Documentation

◆ QgsRubberBand()

QgsRubberBand::QgsRubberBand ( QgsMapCanvas mapCanvas,
QgsWkbTypes::GeometryType  geometryType = QgsWkbTypes::LineGeometry 
)

Creates a new RubberBand.

Parameters
mapCanvasThe map canvas to draw onto. It's CRS will be used map points onto screen coordinates.
geometryTypeDefines how the data should be drawn onto the screen. (Use Qgis::Line, Qgis::Polygon or Qgis::Point)

Definition at line 24 of file qgsrubberband.cpp.

Member Function Documentation

◆ addGeometry()

void QgsRubberBand::addGeometry ( const QgsGeometry geom,
QgsVectorLayer layer 
)

Adds the geometry of an existing feature to a rubberband This is useful for multi feature highlighting.

As of 2.0, this method does not change the GeometryType any more. You need to set the GeometryType of the rubberband explicitly by calling reset() or setToGeometry() with appropriate arguments. setToGeometry() is also to be preferred for backwards-compatibility.

Parameters
geomthe geometry object. Will be treated as a collection of vertices.
layerthe layer containing the feature, used for coord transformation to map crs. In case of 0 pointer, the coordinates are not going to be transformed.

Definition at line 242 of file qgsrubberband.cpp.

◆ addPoint()

void QgsRubberBand::addPoint ( const QgsPointXY p,
bool  doUpdate = true,
int  geometryIndex = 0 
)

Adds a vertex to the rubberband and update canvas.

The rendering of the vertex depends on the current GeometryType and icon. If adding more points consider using update=false for better performance

Parameters
pThe vertex/point to add
doUpdateShould the map canvas be updated immediately?
geometryIndexThe index of the feature part (in case of multipart geometries)

Definition at line 106 of file qgsrubberband.cpp.

◆ asGeometry()

QgsGeometry QgsRubberBand::asGeometry ( ) const

Returns the rubberband as a Geometry.

Returns
A geometry object which reflects the current state of the rubberband.

Definition at line 619 of file qgsrubberband.cpp.

◆ closePoints()

void QgsRubberBand::closePoints ( bool  doUpdate = true,
int  geometryIndex = 0 
)

Ensures that a polygon geometry is closed and that the last vertex equals the first vertex.

Parameters
doUpdateset to true to update the map canvas immediately
geometryIndexindex of the feature part (in case of multipart geometries)
Since
QGIS 2.16

Definition at line 142 of file qgsrubberband.cpp.

◆ drawShape()

void QgsRubberBand::drawShape ( QPainter *  p,
QVector< QPointF > &  pts 
)
protected

Draws shape of the rubber band.

Parameters
pThe QPainter object
ptsA list of points used to draw the shape

Definition at line 445 of file qgsrubberband.cpp.

◆ fillColor()

QColor QgsRubberBand::fillColor ( ) const
inline

Returns the current fill color.

Definition at line 110 of file qgsrubberband.h.

◆ getPoint()

const QgsPointXY * QgsRubberBand::getPoint ( int  i,
int  j = 0 
) const

Returns a vertex.

Parameters
iThe geometry index
jThe vertex index within geometry i

Definition at line 611 of file qgsrubberband.cpp.

◆ icon()

IconType QgsRubberBand::icon ( ) const
inline

Returns the current icon type to highlight point geometries.

Definition at line 158 of file qgsrubberband.h.

◆ iconSize()

int QgsRubberBand::iconSize ( ) const
inline

Returns the current icon size of the point icons.

Definition at line 168 of file qgsrubberband.h.

◆ movePoint() [1/2]

void QgsRubberBand::movePoint ( const QgsPointXY p,
int  geometryIndex = 0 
)

Moves the rubber band point specified by index.

Note that if the rubber band is not used to track the last mouse position, the first point of the rubber band has two vertices

Definition at line 194 of file qgsrubberband.cpp.

◆ movePoint() [2/2]

void QgsRubberBand::movePoint ( int  index,
const QgsPointXY p,
int  geometryIndex = 0 
)

Moves the rubber band point specified by index.

Note that if the rubber band is not used to track the last mouse position, the first point of the rubber band has two vertices

Definition at line 212 of file qgsrubberband.cpp.

◆ numberOfVertices()

int QgsRubberBand::numberOfVertices ( ) const

Returns count of vertices in all lists of mPoint.

Returns
The total number of vertices

Definition at line 596 of file qgsrubberband.cpp.

◆ paint()

void QgsRubberBand::paint ( QPainter *  p)
overrideprotectedvirtual

Paints the rubber band in response to an update event.

Parameters
pThe QPainter object

Implements QgsMapCanvasItem.

Definition at line 415 of file qgsrubberband.cpp.

◆ partSize()

int QgsRubberBand::partSize ( int  geometryIndex) const

Returns number of vertices in feature part.

Parameters
geometryIndexThe index of the feature part (in case of multipart geometries)
Returns
number of vertices

Definition at line 590 of file qgsrubberband.cpp.

◆ removeLastPoint()

void QgsRubberBand::removeLastPoint ( int  geometryIndex = 0,
bool  doUpdate = true 
)

Removes the last point.

Most useful in connection with undo operations

Definition at line 189 of file qgsrubberband.cpp.

◆ removePoint()

void QgsRubberBand::removePoint ( int  index = 0,
bool  doUpdate = true,
int  geometryIndex = 0 
)

Removes a vertex from the rubberband and (optionally) updates canvas.

Parameters
indexThe index of the vertex/point to remove, negative indexes start at end
doUpdateShould the map canvas be updated immediately?
geometryIndexThe index of the feature part (in case of multipart geometries)

Definition at line 163 of file qgsrubberband.cpp.

◆ reset()

void QgsRubberBand::reset ( QgsWkbTypes::GeometryType  geometryType = QgsWkbTypes::LineGeometry)

Clears all the geometries in this rubberband.

Sets the representation type according to geometryType.

Parameters
geometryTypeDefines how the data should be drawn onto the screen. (Use Qgis::Line, Qgis::Polygon or Qgis::Point)

Definition at line 98 of file qgsrubberband.cpp.

◆ secondaryStrokeColor()

QColor QgsRubberBand::secondaryStrokeColor ( ) const
inline

Returns the current secondary stroke color.

Definition at line 135 of file qgsrubberband.h.

◆ setBrushStyle()

void QgsRubberBand::setBrushStyle ( Qt::BrushStyle  brushStyle)

Sets the style of the brush.

Definition at line 93 of file qgsrubberband.cpp.

◆ setColor()

void QgsRubberBand::setColor ( const QColor &  color)

Sets the color for the rubberband.

Parameters
colorThe color used to render this rubberband

Definition at line 52 of file qgsrubberband.cpp.

◆ setFillColor()

void QgsRubberBand::setFillColor ( const QColor &  color)

Sets the fill color for the rubberband.

Parameters
colorThe color used to render this rubberband
Since
QGIS 2.6

Definition at line 58 of file qgsrubberband.cpp.

◆ setIcon()

void QgsRubberBand::setIcon ( IconType  icon)

Sets the icon type to highlight point geometries.

Parameters
iconThe icon to visualize point geometries

Definition at line 78 of file qgsrubberband.cpp.

◆ setIconSize()

void QgsRubberBand::setIconSize ( int  iconSize)

Sets the size of the point icons.

Definition at line 83 of file qgsrubberband.cpp.

◆ setLineStyle()

void QgsRubberBand::setLineStyle ( Qt::PenStyle  penStyle)

Sets the style of the line.

Definition at line 88 of file qgsrubberband.cpp.

◆ setSecondaryStrokeColor()

void QgsRubberBand::setSecondaryStrokeColor ( const QColor &  color)

Sets a secondary stroke color for the rubberband which will be drawn under the main stroke color.

Set to an invalid color to avoid drawing the secondary stroke.

Parameters
colorThe color used to render a secondary stroke color to this rubberband
Since
QGIS 3.0

Definition at line 68 of file qgsrubberband.cpp.

◆ setStrokeColor()

void QgsRubberBand::setStrokeColor ( const QColor &  color)

Sets the stroke color for the rubberband.

Parameters
colorThe color used to render this rubberband
Since
QGIS 2.6

Definition at line 63 of file qgsrubberband.cpp.

◆ setToCanvasRectangle()

void QgsRubberBand::setToCanvasRectangle ( QRect  rect)

Sets this rubber band to a map canvas rectangle.

Parameters
rectrectangle in canvas coordinates

Definition at line 395 of file qgsrubberband.cpp.

◆ setToGeometry()

void QgsRubberBand::setToGeometry ( const QgsGeometry geom,
QgsVectorLayer layer 
)

Sets this rubber band to the geometry of an existing feature.

This is useful for feature highlighting. In contrast to addGeometry(), this method does also change the geometry type of the rubberband.

Parameters
geomthe geometry object
layerthe layer containing the feature, used for coord transformation to map crs. In case of 0 pointer, the coordinates are not going to be transformed.

Definition at line 230 of file qgsrubberband.cpp.

◆ setTranslationOffset()

void QgsRubberBand::setTranslationOffset ( double  dx,
double  dy 
)

Adds translation to original coordinates (all in map coordinates)

Parameters
dxx translation
dyy translation

Definition at line 578 of file qgsrubberband.cpp.

◆ setWidth()

void QgsRubberBand::setWidth ( int  width)

Sets the width of the line.

Stroke width for polygon.

Parameters
widthThe width for any lines painted for this rubberband

Definition at line 73 of file qgsrubberband.cpp.

◆ size()

int QgsRubberBand::size ( ) const

Returns number of geometries.

Returns
number of geometries

Definition at line 585 of file qgsrubberband.cpp.

◆ strokeColor()

QColor QgsRubberBand::strokeColor ( ) const
inline

Returns the current stroke color.

Definition at line 122 of file qgsrubberband.h.

◆ updatePosition()

void QgsRubberBand::updatePosition ( )
overridevirtual

called on changed extent or resize event to update position of the item

Reimplemented from QgsMapCanvasItem.

Definition at line 568 of file qgsrubberband.cpp.

◆ updateRect()

void QgsRubberBand::updateRect ( )
protected

Recalculates needed rectangle.

Definition at line 523 of file qgsrubberband.cpp.

◆ width()

int QgsRubberBand::width ( ) const
inline

Returns the current width of the line or stroke width for polygon.

Definition at line 146 of file qgsrubberband.h.


The documentation for this class was generated from the following files: