27 , mGeometryType( geometryType )
29 reset( geometryType );
30 QColor color( Qt::lightGray );
52 mBrush.setColor( color );
57 mPen.setColor( color );
62 mSecondaryPen.setColor( color );
67 mPen.setWidth( width );
82 mPen.setStyle( penStyle );
87 mBrush.setStyle( brushStyle );
93 mGeometryType = geometryType;
100 if ( geometryIndex < 0 )
102 geometryIndex = mPoints.size() - 1;
105 if ( geometryIndex < 0 || geometryIndex > mPoints.size() )
110 if ( geometryIndex == mPoints.size() )
112 mPoints.push_back( QList<QgsPointXY>() << p );
115 if ( mPoints.at( geometryIndex ).size() == 2 &&
116 mPoints.at( geometryIndex ).at( 0 ) == mPoints.at( geometryIndex ).at( 1 ) )
118 mPoints[geometryIndex].last() = p;
122 mPoints[geometryIndex] << p;
136 if ( geometryIndex < 0 || geometryIndex >= mPoints.size() )
141 if ( mPoints.at( geometryIndex ).at( 0 ) != mPoints.at( geometryIndex ).at( mPoints.at( geometryIndex ).size() - 1 ) )
143 mPoints[geometryIndex] << mPoints.at( geometryIndex ).at( 0 );
158 if ( mPoints.size() < geometryIndex + 1 )
164 if ( !mPoints[geometryIndex].isEmpty() )
169 index = mPoints.at( geometryIndex ).size() + index;
171 mPoints[geometryIndex].removeAt( index );
188 if ( mPoints.size() < geometryIndex + 1 )
193 if ( mPoints.at( geometryIndex ).empty() )
198 mPoints[geometryIndex].last() = p;
206 if ( mPoints.size() < geometryIndex + 1 )
211 if ( mPoints.at( geometryIndex ).size() < index )
216 mPoints[geometryIndex][index] = p;
226 reset( mGeometryType );
263 int idx = mPoints.size();
302 if ( line.isEmpty() )
370 if ( mPoints.isEmpty() )
373 QVector< QVector<QPointF> > shapes;
374 for (
const QList<QgsPointXY> &line : qgis::as_const( mPoints ) )
376 QVector<QPointF> pts;
380 if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 )
386 int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
387 for (
int i = 0; i < iterations; ++i )
389 if ( i == 0 && iterations > 1 )
392 mSecondaryPen.setWidth( mPen.width() + 2 );
393 p->setBrush( Qt::NoBrush );
394 p->setPen( mSecondaryPen );
399 p->setBrush( mBrush );
403 for (
const QVector<QPointF> &shape : qgis::as_const( shapes ) )
412 switch ( mGeometryType )
416 p->drawPolygon( pts );
422 Q_FOREACH ( QPointF pt, pts )
427 qreal s = ( mIconSize - 1 ) / 2.0;
435 p->drawLine( QLineF( x - s, y, x + s, y ) );
436 p->drawLine( QLineF( x, y - s, x, y + s ) );
440 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
441 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
445 p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
446 p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
447 p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
448 p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
452 p->drawRect( x - s, y - s, mIconSize, mIconSize );
456 p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
470 p->drawPolygon( pts, 4 );
472 p->drawPolyline( pts, 4 );
482 p->drawPolyline( pts );
490 if ( mPoints.empty() )
499 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() );
502 for (
int i = 0; i < mPoints.size(); ++i )
504 QList<QgsPointXY>::const_iterator it = mPoints.at( i ).constBegin(),
505 itE = mPoints.at( i ).constEnd();
506 for ( ; it != itE; ++it )
508 QgsPointXY p( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY );
545 mTranslationOffsetX = dx;
546 mTranslationOffsetY = dy;
552 return mPoints.size();
557 if ( geometryIndex < 0 || geometryIndex >= mPoints.size() )
return 0;
558 return mPoints[geometryIndex].size();
564 QList<QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
565 for ( ; it != mPoints.constEnd(); ++it )
567 QList<QgsPointXY>::const_iterator iter = it->constBegin();
568 for ( ; iter != it->constEnd(); ++iter )
578 if ( i < mPoints.size() && j < mPoints[i].size() )
579 return &mPoints[i][j];
588 switch ( mGeometryType )
593 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
594 for ( ; it != mPoints.constEnd(); ++it )
596 polygon.append( getPolyline( *it ) );
606 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
607 for ( ; it != mPoints.constEnd(); ++it )
609 multiPoint += getPolyline( *it );
618 if ( !mPoints.isEmpty() )
620 if ( mPoints.size() > 1 )
623 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
624 for ( ; it != mPoints.constEnd(); ++it )
626 multiPolyline.append( getPolyline( *it ) );
641 QgsPolylineXY QgsRubberBand::getPolyline(
const QList<QgsPointXY> &points )
644 QList<QgsPointXY>::const_iterator iter = points.constBegin();
645 for ( ; iter != points.constEnd(); ++iter )
647 polyline.append( *iter );
A cross is used to highlight points (x)
void setIconSize(int iconSize)
Sets the size of the point icons.
void setWidth(int width)
Sets the width of the line.
A rectangle specified with double values.
int numberOfVertices() const
Returns count of vertices in all lists of mPoint.
static QgsGeometry fromPolylineXY(const QgsPolylineXY &polyline)
Creates a new LineString geometry from a list of QgsPointXY points.
QgsRectangle rect() const
returns canvas item rectangle in map units
void setToCanvasRectangle(QRect rect)
Sets this rubber band to a map canvas rectangle.
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
IconType icon() const
Returns the current icon type to highlight point geometries.
bool isNull() const
Returns true if the geometry is null (ie, contains no underlying geometry accessible via geometry() )...
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
void setTranslationOffset(double dx, double dy)
Adds translation to original coordinates (all in map coordinates)
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
A class to represent a 2D point.
static QgsGeometry fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Creates a new geometry from a QgsMultiPolylineXY object.
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
An abstract class for items that can be placed on the map canvas.
A geometry is the spatial representation of a feature.
void setSecondaryStrokeColor(const QColor &color)
Sets a secondary stroke color for the rubberband which will be drawn under the main stroke color...
A diamond is used to highlight points (◇)
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
void updatePosition() override
called on changed extent or resize event to update position of the item
void addGeometry(const QgsGeometry &geometry, QgsVectorLayer *layer)
Adds the geometry of an existing feature to a rubberband This is useful for multi feature highlightin...
A cross is used to highlight points (+)
const QgsPointXY * getPoint(int i, int j=0) const
Returns a vertex.
Map canvas is a class for displaying all GIS data types on a canvas.
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
void paint(QPainter *p) override
Paints the rubber band in response to an update event.
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
int width() const
Returns the current width of the line or stroke width for polygon.
The QgsMapSettings class contains configuration for rendering of the map.
void setToGeometry(const QgsGeometry &geom, QgsVectorLayer *layer)
Sets this rubber band to the geometry of an existing feature.
QgsRubberBand(QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Creates a new RubberBand.
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
QgsMultiPolylineXY asMultiPolyline() const
Returns contents of the geometry as a multi linestring if wkbType is WKBMultiLineString, otherwise an empty list.
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
void closePoints(bool doUpdate=true, int geometryIndex=0)
Ensures that a polygon geometry is closed and that the last vertex equals the first vertex...
Perform transforms between map coordinates and device coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point from map (world) coordinates to device coordinates.
Type
The WKB type describes the number of dimensions a geometry has.
QgsGeometry asGeometry() const
Returns the rubberband as a Geometry.
bool isEmpty() const
Returns true if the rectangle is empty.
A circle is used to highlight points (○)
QgsPolygonXY asPolygon() const
Returns contents of the geometry as a polygon if wkbType is WKBPolygon, otherwise an empty list...
void removePoint(int index=0, bool doUpdate=true, int geometryIndex=0)
Removes a vertex from the rubberband and (optionally) updates canvas.
double width() const
Returns the width of the rectangle.
void addPoint(const QgsPointXY &p, bool doUpdate=true, int geometryIndex=0)
Adds a vertex to the rubberband and update canvas.
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static QgsGeometry fromMultiPointXY(const QgsMultiPointXY &multipoint)
Creates a new geometry from a QgsMultiPointXY object.
void movePoint(const QgsPointXY &p, int geometryIndex=0)
Moves the rubber band point specified by index.
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
double mapUnitsPerPixel() const
Returns current map units per pixel.
QgsWkbTypes::GeometryType type() const
Returns type of the geometry as a QgsWkbTypes::GeometryType.
QgsMapCanvasItem(QgsMapCanvas *mapCanvas)
protected constructor: cannot be constructed directly
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets the style of the brush.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
static QgsGeometry fromPolygonXY(const QgsPolygonXY &polygon)
Creates a new geometry from a QgsPolygon.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle...
void setIcon(IconType icon)
Sets the icon type to highlight point geometries.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
A box is used to highlight points (□)
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
QgsPointXY toMapPoint(double x, double y) const
QgsPointXY asPoint() const
Returns contents of the geometry as a point if wkbType is WKBPoint, otherwise returns [0...
int iconSize() const
Returns the current icon size of the point icons.
void drawShape(QPainter *p, const QVector< QPointF > &pts)
Draws shape of the rubber band.
int partSize(int geometryIndex) const
Returns number of vertices in feature part.
QgsMapCanvas * mMapCanvas
pointer to map canvas
static QgsProject * instance()
Returns the QgsProject singleton instance.
This class represents a coordinate reference system (CRS).
A diamond is used to highlight points (◆)
double xMinimum() const
Returns the x minimum value (left side of rectangle).
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
void reset(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Clears all the geometries in this rubberband.
void setColor(const QColor &color)
Sets the color for the rubberband.
QgsPolylineXY asPolyline() const
Returns contents of the geometry as a polyline if wkbType is WKBLineString, otherwise an empty list...
QgsMultiPointXY asMultiPoint() const
Returns contents of the geometry as a multi point if wkbType is WKBMultiPoint, otherwise an empty lis...
Custom exception class for Coordinate Reference System related exceptions.
int size() const
Returns number of geometries.
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
Represents a vector layer which manages a vector based data sets.
void removeLastPoint(int geometryIndex=0, bool doUpdate=true)
Removes the last point.
QgsMultiPolygonXY asMultiPolygon() const
Returns contents of the geometry as a multi polygon if wkbType is WKBMultiPolygon, otherwise an empty list.
QgsPointXY toMapCoordinates(int x, int y) const
void updateRect()
Recalculates needed rectangle.
double height() const
Returns the height of the rectangle.
A full box is used to highlight points (■)
bool isValid() const
Returns whether this CRS is correctly initialized and usable.