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 );
83 mSvgRenderer = qgis::make_unique<QSvgRenderer>( path );
84 mSvgOffset = drawOffset;
94 mPen.setStyle( penStyle );
99 mBrush.setStyle( brushStyle );
105 mGeometryType = geometryType;
112 if ( geometryIndex < 0 )
114 geometryIndex = mPoints.size() - 1;
117 if ( geometryIndex < 0 || geometryIndex > mPoints.size() )
122 if ( geometryIndex == mPoints.size() )
124 mPoints.push_back( QList<QgsPointXY>() << p );
127 if ( mPoints.at( geometryIndex ).size() == 2 &&
128 mPoints.at( geometryIndex ).at( 0 ) == mPoints.at( geometryIndex ).at( 1 ) )
130 mPoints[geometryIndex].last() = p;
134 mPoints[geometryIndex] << p;
148 if ( geometryIndex < 0 || geometryIndex >= mPoints.size() )
153 if ( mPoints.at( geometryIndex ).at( 0 ) != mPoints.at( geometryIndex ).at( mPoints.at( geometryIndex ).size() - 1 ) )
155 mPoints[geometryIndex] << mPoints.at( geometryIndex ).at( 0 );
170 if ( mPoints.size() < geometryIndex + 1 )
176 if ( !mPoints[geometryIndex].isEmpty() )
181 index = mPoints.at( geometryIndex ).size() + index;
183 mPoints[geometryIndex].removeAt( index );
200 if ( mPoints.size() < geometryIndex + 1 )
205 if ( mPoints.at( geometryIndex ).empty() )
210 mPoints[geometryIndex].last() = p;
218 if ( mPoints.size() < geometryIndex + 1 )
223 if ( mPoints.at( geometryIndex ).size() < index )
228 mPoints[geometryIndex][index] = p;
238 reset( mGeometryType );
250 reset( mGeometryType );
287 int idx = mPoints.size();
326 if ( line.isEmpty() )
394 if ( mPoints.isEmpty() )
397 QVector< QVector<QPointF> > shapes;
398 shapes.reserve( mPoints.size() );
399 for (
const QList<QgsPointXY> &line : qgis::as_const( mPoints ) )
401 QVector<QPointF> pts;
402 pts.reserve( line.size() );
406 if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 )
412 int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
413 for (
int i = 0; i < iterations; ++i )
415 if ( i == 0 && iterations > 1 )
418 mSecondaryPen.setWidth( mPen.width() + 2 );
419 p->setBrush( Qt::NoBrush );
420 p->setPen( mSecondaryPen );
425 p->setBrush( mBrush );
429 for (
const QVector<QPointF> &shape : qgis::as_const( shapes ) )
438 switch ( mGeometryType )
442 p->drawPolygon( pts );
448 const auto constPts = pts;
449 for ( QPointF pt : constPts )
454 qreal s = ( mIconSize - 1 ) / 2.0;
462 p->drawLine( QLineF( x - s, y, x + s, y ) );
463 p->drawLine( QLineF( x, y - s, x, y + s ) );
467 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
468 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
472 p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
473 p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
474 p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
475 p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
479 p->drawRect( static_cast< int>( x - s ), static_cast< int >( y - s ), mIconSize, mIconSize );
483 p->drawEllipse( static_cast< int >( x - s ), static_cast< int >( y - s ), mIconSize, mIconSize );
497 p->drawPolygon( pts, 4 );
499 p->drawPolyline( pts, 4 );
504 QRectF viewBox = mSvgRenderer->viewBoxF();
505 QRectF r( mSvgOffset.x(), mSvgOffset.y(), viewBox.width(), viewBox.height() );
508 mSvgRenderer->render( p, r );
519 p->drawPolyline( pts );
527 if ( mPoints.empty() )
537 double iconSize = ( mIconSize + 1 ) / 2.;
540 QRectF viewBox = mSvgRenderer->viewBoxF();
541 iconSize = std::max( std::fabs( mSvgOffset.x() ) + .5 * viewBox.width(), std::fabs( mSvgOffset.y() ) + .5 * viewBox.height() );
545 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() );
548 for (
const QList<QgsPointXY> &ring : qgis::as_const( mPoints ) )
552 QgsPointXY p( point.x() + mTranslationOffsetX, point.y() + mTranslationOffsetY );
589 mTranslationOffsetX = dx;
590 mTranslationOffsetY = dy;
596 return mPoints.size();
601 if ( geometryIndex < 0 || geometryIndex >= mPoints.size() )
return 0;
602 return mPoints[geometryIndex].size();
608 for (
const QList<QgsPointXY> &ring : qgis::as_const( mPoints ) )
610 count += ring.size();
617 if ( i < mPoints.size() && j < mPoints[i].size() )
618 return &mPoints[i][j];
627 switch ( mGeometryType )
632 for (
const QList<QgsPointXY> &ring : qgis::as_const( mPoints ) )
634 polygon.append( getPolyline( ring ) );
644 for (
const QList<QgsPointXY> &ring : qgis::as_const( mPoints ) )
646 multiPoint += getPolyline( ring );
655 if ( !mPoints.isEmpty() )
657 if ( mPoints.size() > 1 )
660 for (
const QList<QgsPointXY> &ring : qgis::as_const( mPoints ) )
662 multiPolyline.append( getPolyline( ring ) );
677 QgsPolylineXY QgsRubberBand::getPolyline(
const QList<QgsPointXY> &points )
679 return points.toVector();
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.
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...
const QgsCoordinateReferenceSystem & crs
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 geom.
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 the contents of the geometry as a multi-linestring.
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.
void setSvgIcon(const QString &path, QPoint drawOffset)
Set the path to the svg file to use to draw points.
Type
The WKB type describes the number of dimensions a geometry has.
An svg image is used to highlight points.
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 the contents of the geometry as a polygon.
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.
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 asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
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 the contents of the geometry as a polyline.
QgsMultiPointXY asMultiPoint() const
Returns the contents of the geometry as a multi-point.
QgsWkbTypes::GeometryType type
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 the contents of the geometry as a multi-polygon.
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
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.