28 , mGeometryType( geometryType )
30 reset( geometryType );
31 QColor color( Qt::lightGray );
40 QgsRubberBand::QgsRubberBand()
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() )
133 if ( mPoints.at( geometryIndex ).isEmpty() )
136 ringIndex = mPoints.at( geometryIndex ).size() - 1;
139 if ( ringIndex > mPoints.at( geometryIndex ).size() )
142 if ( ringIndex == mPoints.at( geometryIndex ).size() )
145 mPoints[geometryIndex][ringIndex].append( p );
148 if ( mPoints.at( geometryIndex ).at( ringIndex ).size() == 2 &&
149 mPoints.at( geometryIndex ).at( ringIndex ).at( 0 ) == mPoints.at( geometryIndex ).at( ringIndex ).at( 1 ) )
151 mPoints[geometryIndex][ringIndex].last() = p;
155 mPoints[geometryIndex][ringIndex].append( p );
169 if ( geometryIndex < 0 || ringIndex < 0 ||
170 mPoints.size() <= geometryIndex ||
171 mPoints.at( geometryIndex ).size() <= ringIndex ||
172 mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
177 if ( mPoints.at( geometryIndex ).at( ringIndex ).constFirst() != mPoints.at( geometryIndex ).at( ringIndex ).constLast() )
179 mPoints[geometryIndex][ringIndex].append( mPoints.at( geometryIndex ).at( ringIndex ).constFirst() );
194 if ( geometryIndex < 0 || ringIndex < 0 ||
195 mPoints.size() <= geometryIndex ||
196 mPoints.at( geometryIndex ).size() <= ringIndex ||
197 mPoints.at( geometryIndex ).at( ringIndex ).size() <= index ||
198 mPoints.at( geometryIndex ).at( ringIndex ).size() < -index ||
199 mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
207 index = mPoints.at( geometryIndex ).at( ringIndex ).size() + index;
209 mPoints[geometryIndex][ringIndex].removeAt( index );
220 removePoint( -1, doUpdate, geometryIndex, ringIndex );
225 if ( geometryIndex < 0 || ringIndex < 0 ||
226 mPoints.size() <= geometryIndex ||
227 mPoints.at( geometryIndex ).size() <= ringIndex ||
228 mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
233 mPoints[geometryIndex][ringIndex].last() = p;
241 if ( geometryIndex < 0 || ringIndex < 0 || index < 0 ||
242 mPoints.size() <= geometryIndex ||
243 mPoints.at( geometryIndex ).size() <= ringIndex ||
244 mPoints.at( geometryIndex ).at( ringIndex ).size() <= index )
249 mPoints[geometryIndex][ringIndex][index] = p;
259 reset( mGeometryType );
271 reset( mGeometryType );
308 int idx = mPoints.size();
320 QgsDebugMsg( QStringLiteral(
"Could not transform rubber band geometry to map CRS" ) );
355 if ( line.isEmpty() )
374 addPoint( pt,
false, idx, ringIdx );
384 if ( poly.isEmpty() )
392 addPoint( pt,
false, idx, ringIdx );
434 if ( mPoints.isEmpty() )
437 QVector< QVector<QPolygonF> > shapes;
438 shapes.reserve( mPoints.size() );
439 for (
const QgsPolygonXY &poly : qgis::as_const( mPoints ) )
441 QVector<QPolygonF> rings;
442 rings.reserve( poly.size() );
445 QVector<QPointF> pts;
446 pts.reserve( line.size() );
450 if ( pts.isEmpty() || std::abs( pts.last().x() - cur.x() ) > 1 || std::abs( pts.last().y() - cur.y() ) > 1 )
455 shapes.append( rings );
458 int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
459 for (
int i = 0; i < iterations; ++i )
461 if ( i == 0 && iterations > 1 )
464 mSecondaryPen.setWidth( mPen.width() + 2 );
465 p->setBrush( Qt::NoBrush );
466 p->setPen( mSecondaryPen );
471 p->setBrush( mBrush );
475 for (
const QVector<QPolygonF> &shape : qgis::as_const( shapes ) )
484 if ( rings.size() == 1 )
491 for (
const QPolygonF &poly : rings )
493 path.addPolygon( poly );
501 switch ( mGeometryType )
505 p->drawPolygon( pts );
511 const auto constPts = pts;
512 for ( QPointF pt : constPts )
517 qreal s = ( mIconSize - 1 ) / 2.0;
525 p->drawLine( QLineF( x - s, y, x + s, y ) );
526 p->drawLine( QLineF( x, y - s, x, y + s ) );
530 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
531 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
535 p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
536 p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
537 p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
538 p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
542 p->drawRect(
static_cast< int>( x - s ),
static_cast< int >( y - s ), mIconSize, mIconSize );
546 p->drawEllipse(
static_cast< int >( x - s ),
static_cast< int >( y - s ), mIconSize, mIconSize );
560 p->drawPolygon( pts, 4 );
562 p->drawPolyline( pts, 4 );
568 QRectF viewBox = mSvgRenderer->viewBoxF();
569 QRectF r( mSvgOffset.x(), mSvgOffset.y(), viewBox.width(), viewBox.height() );
572 mSvgRenderer->render( p, r );
583 p->drawPolyline( pts );
591 if ( mPoints.isEmpty() )
601 double iconSize = ( mIconSize + 1 ) / 2.;
604 QRectF viewBox = mSvgRenderer->viewBoxF();
605 iconSize = std::max( std::fabs( mSvgOffset.x() ) + .5 * viewBox.width(), std::fabs( mSvgOffset.y() ) + .5 * viewBox.height() );
609 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() );
612 for (
const QgsPolygonXY &poly : qgis::as_const( mPoints ) )
614 for (
const QgsPointXY &point : poly.at( 0 ) )
616 QgsPointXY p( point.x() + mTranslationOffsetX, point.y() + mTranslationOffsetY );
653 mTranslationOffsetX = dx;
654 mTranslationOffsetY = dy;
660 return mPoints.size();
665 if ( geometryIndex < 0 ||
666 geometryIndex >= mPoints.size() ||
667 mPoints.at( geometryIndex ).isEmpty() )
669 return mPoints.at( geometryIndex ).at( 0 ).size();
675 for (
const QgsPolygonXY &poly : qgis::as_const( mPoints ) )
679 count += ring.size();
687 if ( i < 0 || ringIndex < 0 || j < 0 ||
688 mPoints.size() <= i ||
689 mPoints.at( i ).size() <= ringIndex ||
690 mPoints.at( i ).at( ringIndex ).size() <= j )
693 return &mPoints[i][ringIndex][j];
700 switch ( mGeometryType )
712 for (
const QgsPolygonXY &poly : qgis::as_const( mPoints ) )
714 if ( poly.isEmpty() )
716 multiPoint.append( poly.at( 0 ) );
725 if ( !mPoints.isEmpty() )
727 if ( mPoints.size() > 1 )
730 for (
const QgsPolygonXY &poly : qgis::as_const( mPoints ) )
732 if ( poly.isEmpty() )
734 multiPolyline.append( poly.at( 0 ) );
740 if ( !mPoints.at( 0 ).isEmpty() )
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Custom exception class for Coordinate Reference System related exceptions.
A geometry is the spatial representation of a feature.
QgsMultiPolygonXY asMultiPolygon() const
Returns the contents of the geometry as a multi-polygon.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
static QgsGeometry fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Creates a new geometry from a QgsMultiPolylineXY object.
static QgsGeometry fromPolylineXY(const QgsPolylineXY &polyline)
Creates a new LineString geometry from a list of QgsPointXY points.
QgsMultiPointXY asMultiPoint() const
Returns the contents of the geometry as a multi-point.
QgsPolygonXY asPolygon() const
Returns the contents of the geometry as a polygon.
static QgsGeometry fromMultiPointXY(const QgsMultiPointXY &multipoint)
Creates a new geometry from a QgsMultiPointXY object.
QgsPolylineXY asPolyline() const
Returns the contents of the geometry as a polyline.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
QgsWkbTypes::GeometryType type
QgsMultiPolylineXY asMultiPolyline() const
Returns the contents of the geometry as a multi-linestring.
static QgsGeometry fromMultiPolygonXY(const QgsMultiPolygonXY &multipoly)
Creates a new geometry from a QgsMultiPolygon.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
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.
An abstract class for items that can be placed on the map canvas.
QgsRectangle rect() const
returns canvas item rectangle in map units
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
QgsMapCanvas * mMapCanvas
pointer to map canvas
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
The QgsMapSettings class contains configuration for rendering of the map.
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
Perform transforms between map coordinates and device coordinates.
double mapUnitsPerPixel() const
Returns current map units per pixel.
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point p from map (world) coordinates to device coordinates.
A class to represent a 2D point.
static QgsProject * instance()
Returns the QgsProject singleton instance.
A rectangle specified with double values.
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
bool isEmpty() const
Returns true if the rectangle is empty.
QgsRubberBand(QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Creates a new RubberBand.
void closePoints(bool doUpdate=true, int geometryIndex=0, int ringIndex=0)
Ensures that a polygon geometry is closed and that the last vertex equals the first vertex.
void setWidth(int width)
Sets the width of the line.
void removeLastPoint(int geometryIndex=0, bool doUpdate=true, int ringIndex=0)
Removes the last point.
void movePoint(const QgsPointXY &p, int geometryIndex=0, int ringIndex=0)
Moves the rubber band point specified by index.
QgsGeometry asGeometry() const
Returns the rubberband as a Geometry.
int size() const
Returns number of geometries.
void paint(QPainter *p) override
Paints the rubber band in response to an update event.
IconType icon() const
Returns the current icon type to highlight point geometries.
int partSize(int geometryIndex) const
Returns number of vertices in feature part.
void setSvgIcon(const QString &path, QPoint drawOffset)
Set the path to the svg file to use to draw points.
void setSecondaryStrokeColor(const QColor &color)
Sets a secondary stroke color for the rubberband which will be drawn under the main stroke color.
@ ICON_X
A cross is used to highlight points (x)
@ ICON_FULL_DIAMOND
A diamond is used to highlight points (◆)
@ ICON_FULL_BOX
A full box is used to highlight points (■)
@ ICON_NONE
No icon is used.
@ ICON_CROSS
A cross is used to highlight points (+)
@ ICON_SVG
An svg image is used to highlight points.
@ ICON_CIRCLE
A circle is used to highlight points (○)
@ ICON_DIAMOND
A diamond is used to highlight points (◇)
@ ICON_BOX
A box is used to highlight points (□)
const QgsPointXY * getPoint(int i, int j=0, int ringIndex=0) const
Returns a vertex.
void setColor(const QColor &color)
Sets the color for the rubberband.
void setToGeometry(const QgsGeometry &geom, QgsVectorLayer *layer)
Sets this rubber band to geom.
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
void updateRect()
Recalculates needed rectangle.
void setIconSize(int iconSize)
Sets the size of the point icons.
void setToCanvasRectangle(QRect rect)
Sets this rubber band to a map canvas rectangle.
void setIcon(IconType icon)
Sets the icon type to highlight point geometries.
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets the style of the brush.
void addGeometry(const QgsGeometry &geometry, QgsVectorLayer *layer, bool doUpdate=true)
Adds the geometry of an existing feature to a rubberband This is useful for multi feature highlightin...
void reset(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Clears all the geometries in this rubberband.
void updatePosition() override
called on changed extent or resize event to update position of the item
int numberOfVertices() const
Returns count of vertices in all lists of mPoint.
void removePoint(int index=0, bool doUpdate=true, int geometryIndex=0, int ringIndex=0)
Removes a vertex from the rubberband and (optionally) updates canvas.
void setTranslationOffset(double dx, double dy)
Adds translation to original coordinates (all in map coordinates)
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
void drawShape(QPainter *p, const QVector< QPointF > &pts)
Draws shape of the rubber band.
void addPoint(const QgsPointXY &p, bool doUpdate=true, int geometryIndex=0, int ringIndex=0)
Adds a vertex to the rubberband and update canvas.
Scoped object for saving and restoring a QPainter object's state.
Represents a vector layer which manages a vector based data sets.
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Type
The WKB type describes the number of dimensions a geometry has.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item.
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
const QgsCoordinateReferenceSystem & crs