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 );
256 int idx = mPoints.size();
295 if ( line.isEmpty() )
363 if ( mPoints.isEmpty() )
366 QVector< QVector<QPointF> > shapes;
367 for (
const QList<QgsPointXY> &line : qgis::as_const( mPoints ) )
369 QVector<QPointF> pts;
373 if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 )
379 int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
380 for (
int i = 0; i < iterations; ++i )
382 if ( i == 0 && iterations > 1 )
385 mSecondaryPen.setWidth( mPen.width() + 2 );
386 p->setBrush( Qt::NoBrush );
387 p->setPen( mSecondaryPen );
392 p->setBrush( mBrush );
396 for (
const QVector<QPointF> &shape : qgis::as_const( shapes ) )
405 switch ( mGeometryType )
409 p->drawPolygon( pts );
415 Q_FOREACH ( QPointF pt, pts )
420 qreal s = ( mIconSize - 1 ) / 2.0;
428 p->drawLine( QLineF( x - s, y, x + s, y ) );
429 p->drawLine( QLineF( x, y - s, x, y + s ) );
433 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
434 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
438 p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
439 p->drawLine( QLineF( x + s, y - s, x + s, 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->drawRect( x - s, y - s, mIconSize, mIconSize );
449 p->drawEllipse( x - s, y - s, mIconSize, mIconSize );
463 p->drawPolygon( pts, 4 );
465 p->drawPolyline( pts, 4 );
475 p->drawPolyline( pts );
483 if ( mPoints.empty() )
492 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() );
495 for (
int i = 0; i < mPoints.size(); ++i )
497 QList<QgsPointXY>::const_iterator it = mPoints.at( i ).constBegin(),
498 itE = mPoints.at( i ).constEnd();
499 for ( ; it != itE; ++it )
501 QgsPointXY p( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY );
538 mTranslationOffsetX = dx;
539 mTranslationOffsetY = dy;
545 return mPoints.size();
550 if ( geometryIndex < 0 || geometryIndex >= mPoints.size() )
return 0;
551 return mPoints[geometryIndex].size();
557 QList<QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
558 for ( ; it != mPoints.constEnd(); ++it )
560 QList<QgsPointXY>::const_iterator iter = it->constBegin();
561 for ( ; iter != it->constEnd(); ++iter )
571 if ( i < mPoints.size() && j < mPoints[i].size() )
572 return &mPoints[i][j];
581 switch ( mGeometryType )
586 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
587 for ( ; it != mPoints.constEnd(); ++it )
589 polygon.append( getPolyline( *it ) );
599 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
600 for ( ; it != mPoints.constEnd(); ++it )
602 multiPoint += getPolyline( *it );
611 if ( !mPoints.isEmpty() )
613 if ( mPoints.size() > 1 )
616 QList< QList<QgsPointXY> >::const_iterator it = mPoints.constBegin();
617 for ( ; it != mPoints.constEnd(); ++it )
619 multiPolyline.append( getPolyline( *it ) );
634 QgsPolylineXY QgsRubberBand::getPolyline(
const QList<QgsPointXY> &points )
637 QList<QgsPointXY>::const_iterator iter = points.constBegin();
638 for ( ; iter != points.constEnd(); ++iter )
640 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() )...
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
Return 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
Return current map units per pixel.
QgsWkbTypes::GeometryType type() const
Returns type of the geometry as a QgsWkbTypes::GeometryType.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
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
Get access to properties used for map rendering.
void combineExtentWith(const QgsRectangle &rect)
Expand the rectangle so that 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()
Get 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...
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.