QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
34 , mGeometryType( geometryType )
36 reset( geometryType );
37 QColor color( Qt::lightGray );
46 QgsRubberBand::QgsRubberBand()
62 if ( mBrush.color() == color )
65 mBrush.setColor( color );
70 mPen.setColor( color );
75 mSecondaryPen.setColor( color );
80 mPen.setWidth(
width );
91 mSvgRenderer = std::make_unique<QSvgRenderer>( path );
92 mSvgOffset = drawOffset;
102 mPen.setStyle( penStyle );
107 mBrush.setStyle( brushStyle );
113 mGeometryType = geometryType;
120 if ( geometryIndex < 0 )
122 geometryIndex = mPoints.size() - 1;
125 if ( geometryIndex < 0 || geometryIndex > mPoints.size() )
130 if ( geometryIndex == mPoints.size() )
141 if ( mPoints.at( geometryIndex ).isEmpty() )
144 ringIndex = mPoints.at( geometryIndex ).size() - 1;
147 if ( ringIndex > mPoints.at( geometryIndex ).size() )
150 if ( ringIndex == mPoints.at( geometryIndex ).size() )
154 mPoints[geometryIndex][ringIndex].append( p );
157 if ( mPoints.at( geometryIndex ).at( ringIndex ).size() == 2 &&
158 mPoints.at( geometryIndex ).at( ringIndex ).at( 0 ) == mPoints.at( geometryIndex ).at( ringIndex ).at( 1 ) )
160 mPoints[geometryIndex][ringIndex].last() = p;
164 mPoints[geometryIndex][ringIndex].append( p );
178 if ( geometryIndex < 0 || ringIndex < 0 ||
179 mPoints.size() <= geometryIndex ||
180 mPoints.at( geometryIndex ).size() <= ringIndex ||
181 mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
186 if ( mPoints.at( geometryIndex ).at( ringIndex ).constFirst() != mPoints.at( geometryIndex ).at( ringIndex ).constLast() )
188 mPoints[geometryIndex][ringIndex].append( mPoints.at( geometryIndex ).at( ringIndex ).constFirst() );
203 if ( geometryIndex < 0 || ringIndex < 0 ||
204 mPoints.size() <= geometryIndex ||
205 mPoints.at( geometryIndex ).size() <= ringIndex ||
206 mPoints.at( geometryIndex ).at( ringIndex ).size() <= index ||
207 mPoints.at( geometryIndex ).at( ringIndex ).size() < -index ||
208 mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
216 index = mPoints.at( geometryIndex ).at( ringIndex ).size() + index;
218 mPoints[geometryIndex][ringIndex].removeAt( index );
229 removePoint( -1, doUpdate, geometryIndex, ringIndex );
234 if ( geometryIndex < 0 || ringIndex < 0 ||
235 mPoints.size() <= geometryIndex ||
236 mPoints.at( geometryIndex ).size() <= ringIndex ||
237 mPoints.at( geometryIndex ).at( ringIndex ).isEmpty() )
242 mPoints[geometryIndex][ringIndex].last() = p;
250 if ( geometryIndex < 0 || ringIndex < 0 || index < 0 ||
251 mPoints.size() <= geometryIndex ||
252 mPoints.at( geometryIndex ).size() <= ringIndex ||
253 mPoints.at( geometryIndex ).at( ringIndex ).size() <= index )
258 mPoints[geometryIndex][ringIndex][index] = p;
268 reset( mGeometryType );
280 reset( mGeometryType );
317 int idx = mPoints.size();
329 QgsDebugMsg( QStringLiteral(
"Could not transform rubber band geometry to map CRS" ) );
362 if ( line.isEmpty() )
381 addPoint( pt,
false, idx, ringIdx );
391 if ( poly.isEmpty() )
399 addPoint( pt,
false, idx, ringIdx );
441 reset( other->mGeometryType );
442 mPoints = other->mPoints;
449 if ( mPoints.isEmpty() )
452 QVector< QVector<QPolygonF> > shapes;
453 shapes.reserve( mPoints.size() );
454 for (
const QgsPolygonXY &poly : std::as_const( mPoints ) )
456 QVector<QPolygonF> rings;
457 rings.reserve( poly.size() );
460 QVector<QPointF> pts;
461 pts.reserve( line.size() );
465 if ( pts.isEmpty() || std::abs( pts.last().x() - cur.x() ) > 1 || std::abs( pts.last().y() - cur.y() ) > 1 )
470 shapes.append( rings );
478 lineSymbol->startRender( context );
479 for (
const QVector<QPolygonF> &shape : std::as_const( shapes ) )
481 for (
const QPolygonF &ring : shape )
483 lineSymbol->renderPolyline( ring,
nullptr, context );
486 lineSymbol->stopRender( context );
493 fillSymbol->startRender( context );
494 for (
const QVector<QPolygonF> &shape : std::as_const( shapes ) )
496 for (
const QPolygonF &ring : shape )
498 fillSymbol->renderPolygon( ring,
nullptr,
nullptr, context );
501 fillSymbol->stopRender( context );
505 int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
506 for (
int i = 0; i < iterations; ++i )
508 if ( i == 0 && iterations > 1 )
512 p->setBrush( Qt::NoBrush );
513 p->setPen( mSecondaryPen );
518 p->setBrush( mBrush );
522 for (
const QVector<QPolygonF> &shape : std::as_const( shapes ) )
532 if ( rings.size() == 1 )
539 for (
const QPolygonF &poly : rings )
541 path.addPolygon( poly );
549 switch ( mGeometryType )
553 p->drawPolygon( pts );
559 const auto constPts = pts;
560 for ( QPointF pt : constPts )
565 qreal s = ( mIconSize - 1 ) / 2.0;
573 p->drawLine( QLineF( x - s, y, x + s, y ) );
574 p->drawLine( QLineF( x, y - s, x, y + s ) );
578 p->drawLine( QLineF( x - s, y - s, x + s, y + s ) );
579 p->drawLine( QLineF( x - s, y + s, x + s, y - s ) );
583 p->drawLine( QLineF( x - s, y - s, x + s, y - s ) );
584 p->drawLine( QLineF( x + s, y - s, x + s, y + s ) );
585 p->drawLine( QLineF( x + s, y + s, x - s, y + s ) );
586 p->drawLine( QLineF( x - s, y + s, x - s, y - s ) );
590 p->drawRect(
static_cast< int>( x - s ),
static_cast< int >( y - s ), mIconSize, mIconSize );
594 p->drawEllipse(
static_cast< int >( x - s ),
static_cast< int >( y - s ), mIconSize, mIconSize );
608 p->drawPolygon( pts, 4 );
610 p->drawPolyline( pts, 4 );
616 QRectF viewBox = mSvgRenderer->viewBoxF();
617 QRectF r( mSvgOffset.x(), mSvgOffset.y(), viewBox.width(), viewBox.height() );
620 mSvgRenderer->render( p, r );
631 p->drawPolyline( pts );
639 if ( mPoints.isEmpty() )
649 double iconSize = ( mIconSize + 1 ) / 2.;
652 QRectF viewBox = mSvgRenderer->viewBoxF();
653 iconSize = std::max( std::fabs( mSvgOffset.x() ) + .5 * viewBox.width(), std::fabs( mSvgOffset.y() ) + .5 * viewBox.height() );
657 qreal w = ( ( mIconSize - 1 ) / 2 + mPen.width() );
660 for (
const QgsPolygonXY &poly : std::as_const( mPoints ) )
662 for (
const QgsPointXY &point : poly.at( 0 ) )
664 QgsPointXY p( point.x() + mTranslationOffsetX, point.y() + mTranslationOffsetY );
683 return mSymbol.get();
703 mTranslationOffsetX = dx;
704 mTranslationOffsetY = dy;
710 return mPoints.size();
715 if ( geometryIndex < 0 ||
716 geometryIndex >= mPoints.size() ||
717 mPoints.at( geometryIndex ).isEmpty() )
719 return mPoints.at( geometryIndex ).at( 0 ).size();
725 for (
const QgsPolygonXY &poly : std::as_const( mPoints ) )
729 count += ring.size();
737 if ( i < 0 || ringIndex < 0 || j < 0 ||
738 mPoints.size() <= i ||
739 mPoints.at( i ).size() <= ringIndex ||
740 mPoints.at( i ).at( ringIndex ).size() <= j )
743 return &mPoints[i][ringIndex][j];
750 switch ( mGeometryType )
762 for (
const QgsPolygonXY &poly : std::as_const( mPoints ) )
764 if ( poly.isEmpty() )
766 multiPoint.append( poly.at( 0 ) );
775 if ( !mPoints.isEmpty() )
777 if ( mPoints.size() > 1 )
780 for (
const QgsPolygonXY &poly : std::as_const( mPoints ) )
782 if ( poly.isEmpty() )
784 multiPolyline.append( poly.at( 0 ) );
790 if ( !mPoints.at( 0 ).isEmpty() )
QgsSymbol * symbol() const
Returns the symbol used for rendering the rubberband, if set.
int size() const
Returns number of geometries.
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
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 setSvgIcon(const QString &path, QPoint drawOffset)
Set the path to the svg file to use to draw points.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
@ ICON_CIRCLE
A circle is used to highlight points (○)
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item.
static QgsGeometry fromPolylineXY(const QgsPolylineXY &polyline)
Creates a new LineString geometry from a list of QgsPointXY points.
QgsMapCanvas * mMapCanvas
pointer to map canvas
void updatePosition() override
called on changed extent or resize event to update position of the item
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
static QgsGeometry fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Creates a new geometry from a QgsMultiPolylineXY object.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
void removeLastPoint(int geometryIndex=0, bool doUpdate=true, int ringIndex=0)
Removes the last point.
A class for drawing transient features (e.g. digitizing lines) on the map.
void setIconSize(int iconSize)
Sets the size of the point icons.
const QgsCoordinateReferenceSystem & crs
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsPointXY * getPoint(int i, int j=0, int ringIndex=0) const
Returns a vertex.
Contains information about the context of a rendering operation.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void setStrokeColor(const QColor &color)
Sets the stroke color for the rubberband.
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
void setFillColor(const QColor &color)
Sets the fill color for the rubberband.
@ ICON_BOX
A box is used to highlight points (□)
Type
The WKB type describes the number of dimensions a geometry has.
Abstract base class for all rendered symbols.
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
void movePoint(const QgsPointXY &p, int geometryIndex=0, int ringIndex=0)
Moves the rubber band point specified by index.
A rectangle specified with double values.
QgsMultiPolylineXY asMultiPolyline() const
Returns the contents of the geometry as a multi-linestring.
@ ICON_NONE
No icon is used.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
void reset(QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Clears all the geometries in this rubberband.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
void setRect(const QgsRectangle &r, bool resetRotation=true)
sets canvas item rectangle in map units
@ ICON_FULL_DIAMOND
A diamond is used to highlight points (◆)
QgsPolygonXY asPolygon() const
Returns the contents of the geometry as a polygon.
Custom exception class for Coordinate Reference System related exceptions.
An abstract class for items that can be placed on the map canvas.
void addGeometry(const QgsGeometry &geometry, QgsMapLayer *layer, bool doUpdate=true)
Adds the geometry of an existing feature to a rubberband This is useful for multi feature highlightin...
QPointF toCanvasCoordinates(const QgsPointXY &point) const
transformation from map coordinates to screen coordinates
static QgsGeometry fromMultiPointXY(const QgsMultiPointXY &multipoint)
Creates a new geometry from a QgsMultiPointXY object.
~QgsRubberBand() override
void setTranslationOffset(double dx, double dy)
Adds translation to original coordinates (all in map coordinates)
void updateRect()
Recalculates needed rectangle.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
void setColor(const QColor &color)
Sets the color for the rubberband.
A line symbol type, for rendering LineString and MultiLineString geometries.
void setSymbol(QgsSymbol *symbol)
Sets the symbol used for rendering the rubberband.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
int numberOfVertices() const
Returns count of vertices in all lists of mPoint.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
@ ICON_X
A cross is used to highlight points (x)
void addPoint(const QgsPointXY &p, bool doUpdate=true, int geometryIndex=0, int ringIndex=0)
Adds a vertex to the rubberband and update canvas.
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Scoped object for saving and restoring a QPainter object's state.
const QgsMapToPixel * getCoordinateTransform()
Gets the current coordinate transform.
This class represents a coordinate reference system (CRS).
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
void drawShape(QPainter *p, const QVector< QPointF > &pts)
Draws shape of the rubber band.
@ ICON_FULL_BOX
A full box is used to highlight points (■)
void paint(QPainter *p) override
Paints the rubber band in response to an update event.
void setLineStyle(Qt::PenStyle penStyle)
Sets the style of the line.
A class to represent a 2D point.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
QgsRubberBand(QgsMapCanvas *mapCanvas, QgsWkbTypes::GeometryType geometryType=QgsWkbTypes::LineGeometry)
Creates a new RubberBand.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
@ Antialiasing
Use antialiasing while drawing.
void setWidth(int width)
Sets the width of the line.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
QgsRectangle rect() const
returns canvas item rectangle in map units
A geometry is the spatial representation of a feature.
Perform transforms between map coordinates and device coordinates.
@ ICON_CROSS
A cross is used to highlight points (+)
Represents a vector layer which manages a vector based data sets.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Base class for all map layer types. This is the base class for all map layer types (vector,...
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
void setToCanvasRectangle(QRect rect)
Sets this rubber band to a map canvas rectangle.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
IconType icon() const
Returns the current icon type to highlight point geometries.
void setIcon(IconType icon)
Sets the icon type to highlight point geometries.
QgsPolylineXY asPolyline() const
Returns the contents of the geometry as a polyline.
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...
QgsMultiPointXY asMultiPoint() const
Returns the contents of the geometry as a multi-point.
void setToGeometry(const QgsGeometry &geom, QgsVectorLayer *layer)
Sets this rubber band to geom.
@ ICON_SVG
An svg image is used to highlight points.
int partSize(int geometryIndex) const
Returns number of vertices in feature part.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
@ ICON_DIAMOND
A diamond is used to highlight points (◇)
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
QgsWkbTypes::GeometryType type
void setSecondaryStrokeColor(const QColor &color)
Sets a secondary stroke color for the rubberband which will be drawn under the main stroke color.
void copyPointsFrom(const QgsRubberBand *other)
Copies the points from another rubber band.
void setBrushStyle(Qt::BrushStyle brushStyle)
Sets the style of the brush.
QgsGeometry asGeometry() const
Returns the rubberband as a Geometry.
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.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
static QgsGeometry fromMultiPolygonXY(const QgsMultiPolygonXY &multipoly)
Creates a new geometry from a QgsMultiPolygonXY.
QgsMultiPolygonXY asMultiPolygon() const
Returns the contents of the geometry as a multi-polygon.