28 , mGeometryType( geometryType )
30 reset( geometryType );
31 QColor color( Qt::lightGray );
54 if ( mBrush.color() == color )
57 mBrush.setColor( color );
62 mPen.setColor( color );
67 mSecondaryPen.setColor( color );
72 mPen.setWidth( width );
87 mPen.setStyle( penStyle );
92 mBrush.setStyle( brushStyle );
98 mGeometryType = geometryType;
105 if ( geometryIndex < 0 )
107 geometryIndex = mPoints.size() - 1;
110 if ( geometryIndex < 0 || geometryIndex > mPoints.size() )
115 if ( geometryIndex == mPoints.size() )
117 mPoints.push_back( QList<QgsPointXY>() << p );
120 if ( mPoints.at( geometryIndex ).size() == 2 &&
121 mPoints.at( geometryIndex ).at( 0 ) == mPoints.at( geometryIndex ).at( 1 ) )
123 mPoints[geometryIndex].last() = p;
127 mPoints[geometryIndex] << p;
141 if ( geometryIndex < 0 || geometryIndex >= mPoints.size() )
146 if ( mPoints.at( geometryIndex ).at( 0 ) != mPoints.at( geometryIndex ).at( mPoints.at( geometryIndex ).size() - 1 ) )
148 mPoints[geometryIndex] << mPoints.at( geometryIndex ).at( 0 );
163 if ( mPoints.size() < geometryIndex + 1 )
169 if ( !mPoints[geometryIndex].isEmpty() )
174 index = mPoints.at( geometryIndex ).size() + index;
176 mPoints[geometryIndex].removeAt( index );
193 if ( mPoints.size() < geometryIndex + 1 )
198 if ( mPoints.at( geometryIndex ).empty() )
203 mPoints[geometryIndex].last() = p;
211 if ( mPoints.size() < geometryIndex + 1 )
216 if ( mPoints.at( geometryIndex ).size() < index )
221 mPoints[geometryIndex][index] = p;
231 reset( mGeometryType );
243 reset( mGeometryType );
280 int idx = mPoints.size();
319 if ( line.isEmpty() )
387 if ( mPoints.isEmpty() )
390 QVector< QVector<QPointF> > shapes;
391 for (
const QList<QgsPointXY> &line : qgis::as_const( mPoints ) )
393 QVector<QPointF> pts;
397 if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 )
403 int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
404 for (
int i = 0; i < iterations; ++i )
406 if ( i == 0 && iterations > 1 )
409 mSecondaryPen.setWidth( mPen.width() + 2 );
410 p->setBrush( Qt::NoBrush );
411 p->setPen( mSecondaryPen );
416 p->setBrush( mBrush );
420 for (
const QVector<QPointF> &shape : qgis::as_const( shapes ) )
429 switch ( mGeometryType )
433 p->drawPolygon( pts );
439 Q_FOREACH ( QPointF pt, pts )
444 qreal s = ( mIconSize - 1 ) / 2.0;
452 p->drawLine( QLineF( x - s, y, x + s, y ) );
453 p->drawLine( QLineF( x, y - s, x, y + s ) );
457 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
458 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
462 p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
463 p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
464 p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
465 p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
469 p->drawRect( x - s, y - s, mIconSize, mIconSize );
473 p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
487 p->drawPolygon( pts, 4 );
489 p->drawPolyline( pts, 4 );
499 p->drawPolyline( pts );
507 if ( mPoints.empty() )
516 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() );
519 for (
int i = 0; i < mPoints.size(); ++i )
521 QList<QgsPointXY>::const_iterator it = mPoints.at( i ).constBegin(),
522 itE = mPoints.at( i ).constEnd();
523 for ( ; it != itE; ++it )
525 QgsPointXY p( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY );
562 mTranslationOffsetX = dx;
563 mTranslationOffsetY = dy;
569 return mPoints.size();
574 if ( geometryIndex < 0 || geometryIndex >= mPoints.size() )
return 0;
575 return mPoints[geometryIndex].size();
581 QList<QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
582 for ( ; it != mPoints.constEnd(); ++it )
584 QList<QgsPointXY>::const_iterator iter = it->constBegin();
585 for ( ; iter != it->constEnd(); ++iter )
595 if ( i < mPoints.size() && j < mPoints[i].size() )
596 return &mPoints[i][j];
605 switch ( mGeometryType )
610 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
611 for ( ; it != mPoints.constEnd(); ++it )
613 polygon.append( getPolyline( *it ) );
623 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
624 for ( ; it != mPoints.constEnd(); ++it )
626 multiPoint += getPolyline( *it );
635 if ( !mPoints.isEmpty() )
637 if ( mPoints.size() > 1 )
640 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
641 for ( ; it != mPoints.constEnd(); ++it )
643 multiPolyline.append( getPolyline( *it ) );
658 QgsPolylineXY QgsRubberBand::getPolyline(
const QList<QgsPointXY> &points )
661 QList<QgsPointXY>::const_iterator iter = points.constBegin();
662 for ( ; iter != points.constEnd(); ++iter )
664 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.
bool isEmpty() const
Returns true if the rectangle is empty.
static QgsGeometry fromPolylineXY(const QgsPolylineXY &polyline)
Creates a new LineString geometry from a list of QgsPointXY points.
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.
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.
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.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
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...
int numberOfVertices() const
Returns count of vertices in all lists of mPoint.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
const QgsCoordinateReferenceSystem & crs
A cross is used to highlight points (+)
QgsRectangle rect() const
returns canvas item rectangle in map units
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
The QgsMapSettings class contains configuration for rendering of the map.
void setToGeometry(const QgsGeometry &geom, QgsVectorLayer *layer)
Sets this rubber band to geom.
QgsMultiPolygonXY asMultiPolygon() const
Returns contents of the geometry as a multi polygon if wkbType is WKBMultiPolygon, otherwise an empty list.
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.
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.
IconType icon() const
Returns the current icon type to highlight point geometries.
Type
The WKB type describes the number of dimensions a geometry has.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
const QgsPointXY * getPoint(int i, int j=0) const
Returns a vertex.
A circle is used to highlight points (○)
void removePoint(int index=0, bool doUpdate=true, int geometryIndex=0)
Removes a vertex from the rubberband and (optionally) updates canvas.
void addPoint(const QgsPointXY &p, bool doUpdate=true, int geometryIndex=0)
Adds a vertex to the rubberband and update canvas.
QgsPolylineXY asPolyline() const
Returns the contents of the geometry as a polyline.
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
double mapUnitsPerPixel() const
Returns current map units per pixel.
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
QgsMultiPolylineXY asMultiPolyline() const
Returns contents of the geometry as a multi linestring if wkbType is WKBMultiLineString, otherwise an empty list.
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
QgsMapCanvasItem(QgsMapCanvas *mapCanvas)
protected constructor: cannot be constructed directly
QgsGeometry asGeometry() const
Returns the rubberband as a Geometry.
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets the style of the brush.
static QgsGeometry fromPolygonXY(const QgsPolygonXY &polygon)
Creates a new geometry from a QgsPolygon.
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.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
A box is used to highlight points (□)
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
int size() const
Returns number of geometries.
void drawShape(QPainter *p, const QVector< QPointF > &pts)
Draws shape of the rubber band.
QgsMapCanvas * mMapCanvas
pointer to map canvas
static QgsProject * instance()
Returns the QgsProject singleton instance.
This class represents a coordinate reference system (CRS).
int partSize(int geometryIndex) const
Returns number of vertices in feature part.
A diamond is used to highlight points (◆)
int width() const
Returns the current width of the line or stroke width for polygon.
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
QgsMultiPointXY asMultiPoint() const
Returns contents of the geometry as a multi point if wkbType is WKBMultiPoint, otherwise an empty lis...
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.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Custom exception class for Coordinate Reference System related exceptions.
double width() const
Returns the width of the rectangle.
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.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point from map (world) coordinates to device coordinates.
QgsWkbTypes::GeometryType type() const
Returns type of the geometry as a QgsWkbTypes::GeometryType.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
void updateRect()
Recalculates needed rectangle.
double height() const
Returns the height of the rectangle.
A full box is used to highlight points (■)
int iconSize() const
Returns the current icon size of the point icons.
QgsPolygonXY asPolygon() const
Returns the contents of the geometry as a polygon.