37using namespace Qt::StringLiterals;
43 mBackgroundSymbol = std::make_unique< QgsFillSymbol >(
47 borderSymbol->setPenJoinStyle( Qt::MiterJoin );
48 mFrameSymbol = std::make_unique< QgsFillSymbol >(
QgsSymbolLayerList { borderSymbol } );
55 switch ( mPlacementMode )
69 double angle = mRotation;
99 const double widthPixels = mapBounds.
width() / mupp;
100 const double heightPixels = mapBounds.
height() / mupp;
101 const QRectF painterBounds( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
103 QTransform transform;
104 transform.translate( center.x(), center.y() );
105 transform.rotate( angle );
106 transform.translate( -center.x(), -center.y() );
108 QVector< QgsPointXY > points;
110 for (
const QPointF &corner : { painterBounds.topLeft(), painterBounds.topRight(), painterBounds.bottomRight(), painterBounds.bottomLeft() } )
111 points.append(
QgsPointXY( transform.map( corner ) ) );
112 points.append( points.constFirst() );
125 const double halfWidthMap = 0.5 * widthPixels * mupp;
126 const double halfHeightMap = 0.5 * heightPixels * mupp;
127 return QgsRectangle( centerMap.
x() - halfWidthMap, centerMap.
y() - halfHeightMap, centerMap.
x() + halfWidthMap, centerMap.
y() + halfHeightMap );
145 QRectF painterBounds;
147 switch ( mPlacementMode )
153 QPointF center =
bounds.center().toQPointF();
156 const double widthPixels =
bounds.width() / mupp;
157 const double heightPixels =
bounds.height() / mupp;
158 painterBounds = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
174 QPointF anchorPoint = anchor.
asQPointF();
177 double x = anchorPoint.x();
178 double y = anchorPoint.y();
181 anchorPoint = QPointF( x, y );
186 painterBounds = QRectF( anchorPoint.x() + calloutOffsetWidthPixels, anchorPoint.y() + calloutOffsetHeightPixels, widthPixels, heightPixels );
190 QPointF center =
bounds.center().toQPointF();
193 painterBounds = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
203 QPointF center =
bounds.center().toQPointF();
207 painterBounds = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
212 if ( painterBounds.width() < 1 || painterBounds.height() < 1 )
215 QPainter *painter = context.
painter();
218 const QPointF rotationCenter = painterBounds.center();
220 if ( mDrawBackground && mBackgroundSymbol )
227 mBackgroundSymbol->startRender( context );
228 mBackgroundSymbol->renderPolygon( painterBounds,
nullptr,
nullptr, context );
229 mBackgroundSymbol->stopRender( context );
237 renderCallout( context, painterBounds, angle, calloutContext, feedback );
248 if ( mDrawFrame && mFrameSymbol )
250 mFrameSymbol->startRender( context );
251 mFrameSymbol->renderPolygon( painterBounds,
nullptr,
nullptr, context );
252 mFrameSymbol->stopRender( context );
261 QList<QgsAnnotationItemNode> res;
262 switch ( mPlacementMode )
280 calloutNodePoint = mBounds.center();
315 switch ( operation->
type() )
322 switch ( mPlacementMode )
329 mBounds =
QgsRectangle( moveOperation->
after().
x(), moveOperation->
after().
y(), mBounds.xMaximum(), mBounds.yMaximum() );
332 mBounds =
QgsRectangle( mBounds.xMinimum(), moveOperation->
after().
y(), moveOperation->
after().
x(), mBounds.yMaximum() );
335 mBounds =
QgsRectangle( mBounds.xMinimum(), mBounds.yMinimum(), moveOperation->
after().
x(), moveOperation->
after().
y() );
338 mBounds =
QgsRectangle( moveOperation->
after().
x(), mBounds.yMinimum(), mBounds.xMaximum(), moveOperation->
after().
y() );
376 switch ( mPlacementMode )
421 mRotation = std::fmod( mRotation + rotateOperation->
angle(), 360.0 );
434 switch ( operation->
type() )
441 switch ( mPlacementMode )
494 switch ( mPlacementMode )
516 QPointF anchorPoint = anchor.
asQPointF();
519 double x = anchorPoint.x();
520 double y = anchorPoint.y();
523 anchorPoint = QPointF( x, y );
528 const double textOriginXPixels = anchorPoint.x() + calloutOffsetWidthPixels;
529 const double textOriginYPixels = anchorPoint.y() + calloutOffsetHeightPixels;
535 QVector<QgsPointXY> {
536 QgsPointXY( textOriginXPixels, textOriginYPixels ),
537 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels ),
538 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels + heightPixels ),
539 QgsPointXY( textOriginXPixels, textOriginYPixels + heightPixels ),
540 QgsPointXY( textOriginXPixels, textOriginYPixels )
580 switch ( mPlacementMode )
614 switch ( mPlacementMode )
624 QRectF boundsInPixels;
632 QPointF anchorPoint = anchor.
asQPointF();
635 double x = anchorPoint.x();
636 double y = anchorPoint.y();
639 anchorPoint = QPointF( x, y );
645 textRect( anchorPoint.x() + calloutOffsetWidthPixels, anchorPoint.y() + calloutOffsetHeightPixels, anchorPoint.x() + calloutOffsetWidthPixels + widthPixels, anchorPoint.y() + calloutOffsetHeightPixels + heightPixels );
646 QgsRectangle anchorRect( anchorPoint.x(), anchorPoint.y(), anchorPoint.x(), anchorPoint.y() );
649 boundsInPixels = anchorRect.
toRectF();
653 QPointF center = mBounds.center().toQPointF();
656 double x = center.x();
657 double y = center.y();
660 center = QPointF( x, y );
664 boundsInPixels = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
681 QRectF boundsInPixels;
685 QPointF center( centerMapX, centerMapY );
688 double x = centerMapX;
689 double y = centerMapY;
692 center = QPointF( x, y );
696 boundsInPixels = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
718 return mBackgroundSymbol.get();
723 mBackgroundSymbol.reset( symbol );
728 return mFrameSymbol.get();
733 mFrameSymbol.reset( symbol );
747 if ( otherRect->mBackgroundSymbol )
751 if ( otherRect->mFrameSymbol )
765 element.setAttribute( u
"fixedWidth"_s,
qgsDoubleToString( mFixedSize.width() ) );
766 element.setAttribute( u
"fixedHeight"_s,
qgsDoubleToString( mFixedSize.height() ) );
771 element.setAttribute( u
"backgroundEnabled"_s, mDrawBackground ? u
"1"_s : u
"0"_s );
772 if ( mBackgroundSymbol )
774 QDomElement backgroundElement = document.createElement( u
"backgroundSymbol"_s );
776 element.appendChild( backgroundElement );
779 element.setAttribute( u
"frameEnabled"_s, mDrawFrame ? u
"1"_s : u
"0"_s );
782 QDomElement frameElement = document.createElement( u
"frameSymbol"_s );
784 element.appendChild( frameElement );
792 mBounds.setXMinimum( element.attribute( u
"xMin"_s ).toDouble() );
793 mBounds.setXMaximum( element.attribute( u
"xMax"_s ).toDouble() );
794 mBounds.setYMinimum( element.attribute( u
"yMin"_s ).toDouble() );
795 mBounds.setYMaximum( element.attribute( u
"yMax"_s ).toDouble() );
799 mFixedSize = QSizeF( element.attribute( u
"fixedWidth"_s ).toDouble(), element.attribute( u
"fixedHeight"_s ).toDouble() );
801 mRotation = element.attribute( u
"rotation"_s ).toDouble();
804 mDrawBackground = element.attribute( u
"backgroundEnabled"_s, u
"0"_s ).toInt();
805 const QDomElement backgroundSymbolElem = element.firstChildElement( u
"backgroundSymbol"_s ).firstChildElement();
806 if ( !backgroundSymbolElem.isNull() )
811 mDrawFrame = element.attribute( u
"frameEnabled"_s, u
"0"_s ).toInt();
812 const QDomElement frameSymbolElem = element.firstChildElement( u
"frameSymbol"_s ).firstChildElement();
813 if ( !frameSymbolElem.isNull() )
833 return mFixedSizeUnit;
838 mFixedSizeUnit = unit;
843 return mPlacementMode;
848 mPlacementMode = mode;
@ RespectMapRotation
Entity is rotated along with the map.
@ IgnoreMapRotation
Entity ignores map rotation.
@ VertexHandle
Node is a handle for manipulating vertices.
@ CalloutHandle
Node is a handle for manipulating callouts.
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
@ SupportsCallouts
Item supports callouts.
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
@ Invalid
Operation has invalid parameters for the item, no change occurred.
@ Success
Item was modified successfully.
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
RenderUnit
Rendering size units.
AnnotationPlacementMode
Annotation item placement modes.
@ SpatialBounds
Item is rendered inside fixed spatial bounds, and size will depend on map scale.
@ FixedSize
Item is rendered at a fixed size, regardless of map scale. Item's location is georeferenced to a spat...
@ RelativeToMapFrame
Items size and placement is relative to the map's frame, and the item will always be rendered in the ...
@ Reverse
Reverse/inverse transform (from destination to source).
Abstract base class for annotation item edit operations.
@ RotateItem
Rotate an item.
@ DeleteNode
Delete a node.
@ TranslateItem
Translate (move) an item.
virtual Type type() const =0
Returns the operation type.
Encapsulates the context for an annotation item edit operation.
QgsRectangle currentItemBounds() const
Returns the current rendered bounds of the item, in the annotation layer's CRS.
QgsRenderContext renderContext() const
Returns the render context associated with the edit operation.
Annotation item edit operation consisting of moving a node.
double translationYPixels() const
Returns the y-axis translation, in pixels.
QgsPoint before() const
Returns the node position before the move occurred (in layer coordinates).
QgsPoint after() const
Returns the node position after the move occurred (in layer coordinates).
double translationXPixels() const
Returns the x-axis translation, in pixels.
QgsVertexId nodeId() const
Returns the associated node ID.
Annotation item edit operation consisting of rotating an item.
double angle() const
Returns the rotation angle value (in degrees clockwise).
Encapsulates the transient results of an in-progress annotation edit operation.
Annotation item edit operation consisting of translating (moving) an item.
double translationY() const
Returns the y-axis translation, in layer units.
double translationX() const
Returns the x-axis translation, in layer units.
double translationYPixels() const
Returns the y-axis translation, in pixels.
double translationXPixels() const
Returns the x-axis translation, in pixels.
Contains information about a node used for editing an annotation item.
virtual bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes common properties from the base class into an XML element.
void setCallout(QgsCallout *callout)
Sets the item's callout renderer, responsible for drawing item callouts.
void setOffsetFromCallout(const QSizeF &offset)
Sets the offset of the annotation item from the calloutAnchor().
QgsGeometry calloutAnchor() const
Returns the callout's anchor geometry.
Qgis::RenderUnit offsetFromCalloutUnit() const
Returns the units for the offsetFromCallout().
virtual void copyCommonProperties(const QgsAnnotationItem *other)
Copies common properties from the base class from an other item.
void setCalloutAnchor(const QgsGeometry &anchor)
Sets the callout's anchor geometry.
QSizeF offsetFromCallout() const
Returns the (optional) offset of the annotation item from the calloutAnchor().
QgsCallout * callout() const
Returns the item's callout renderer, responsible for drawing item callouts.
virtual bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common properties from the base class from the given DOM element.
void renderCallout(QgsRenderContext &context, const QRectF &rect, double angle, QgsCallout::QgsCalloutContext &calloutContext, QgsFeedback *feedback)
Renders the item's callout.
virtual void renderInBounds(QgsRenderContext &context, const QRectF &painterRect, QgsFeedback *feedback)=0
Renders the item to the specified render context.
QgsGeometry rotatedBoundsGeometry(const QgsRectangle &layerBounds, const QgsRenderContext &renderContext) const
Returns the polygon geometry (in layer coordinates) for the item occupying the given layerBounds,...
Qgis::AnnotationItemFlags flags() const override
Returns item flags.
static QgsRectangle boundsFromPixelRect(const QgsMapToPixel *mapToPixel, const QPointF ¢erPixel, double widthPixels, double heightPixels)
Returns the axis-aligned bounds, in map coordinates, of a rectangle centered on centerPixel with size...
void copyCommonProperties(const QgsAnnotationItem *other) override
Copies common properties from the base class from an other item.
void setRotationMode(Qgis::SymbolRotationMode mode)
Sets the mode used to render the item with respect to the rotation of the map.
double appliedRotation(double mapRotation) const
Returns the effective on-screen rotation of the item, in degrees clockwise.
QgsRectangle bounds() const
Returns the bounds of the item.
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
QgsAnnotationItemEditOperationTransientResults * transientEditResultsV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Retrieves the results of a transient (in progress) edit operation on the item.
Qgis::AnnotationItemEditOperationResult applyEditV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Applies an edit operation to the item.
~QgsAnnotationRectItem() override
bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context) override
Reads common properties from the base class from the given DOM element.
Qgis::RenderUnit fixedSizeUnit() const
Returns the units to use for fixed item sizes, when the placementMode() is Qgis::AnnotationPlacementM...
QSizeF fixedSize() const
Returns the fixed size to use for the item, when the placementMode() is Qgis::AnnotationPlacementMode...
void setFixedSizeUnit(Qgis::RenderUnit unit)
Sets the unit to use for fixed item sizes, when the placementMode() is Qgis::AnnotationPlacementMode:...
QgsAnnotationRectItem(const QgsRectangle &bounds)
Constructor for QgsAnnotationRectItem, rendering the annotation within the specified bounds geometry.
void setBackgroundSymbol(QgsFillSymbol *symbol)
Sets the symbol used to render the item's background.
void setFrameSymbol(QgsFillSymbol *symbol)
Sets the symbol used to render the item's frame.
void setBounds(const QgsRectangle &bounds)
Sets the bounds of the item.
void setPlacementMode(Qgis::AnnotationPlacementMode mode)
Sets the placement mode for the item.
bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes common properties from the base class into an XML element.
const QgsFillSymbol * frameSymbol() const
Returns the symbol used to render the item's frame.
void setBackgroundEnabled(bool enabled)
Sets whether the item's background should be rendered.
QList< QgsAnnotationItemNode > nodesV2(const QgsAnnotationItemEditContext &context) const override
Returns the nodes for the item, used for editing the item.
void setRotation(double rotation)
Sets the rotation of the item, in degrees clockwise.
void setFrameEnabled(bool enabled)
Sets whether the item's frame should be rendered.
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
void setFixedSize(const QSizeF &size)
Sets the fixed size to use for the item, when the placementMode() is Qgis::AnnotationPlacementMode::F...
Qgis::AnnotationPlacementMode placementMode() const
Returns the placement mode for the item.
const QgsFillSymbol * backgroundSymbol() const
Returns the symbol used to render the item's background.
static QgsCalloutRegistry * calloutRegistry()
Returns the application's callout registry, used for managing callout types.
Contains additional contextual information about the context in which a callout is being rendered.
Custom exception class for Coordinate Reference System related exceptions.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
QPointF asQPointF() const
Returns contents of the geometry as a QPointF if wkbType is WKBPoint, otherwise returns a null QPoint...
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsGeometry fromPoint(const QgsPoint &point)
Creates a new geometry from a QgsPoint object.
Line string geometry type, with support for z-dimension and m-values.
Perform transforms between map coordinates and device coordinates.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
double mapRotation() const
Returns the current map rotation in degrees (clockwise).
void transformInPlace(double &x, double &y) const
Transforms map coordinates to device coordinates.
static void rotatePainterAroundPoint(QPainter *painter, const QPointF &point, double angle)
Rotates a painter by angle degrees clockwise around point.
QPointF toQPointF() const
Converts a point to a QPointF.
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
static QgsRectangle fromCenterAndSize(const QgsPointXY ¢er, double width, double height)
Creates a new rectangle, given the specified center point and width and height.
Contains information about the context of a rendering operation.
double convertFromPainterUnits(double size, Qgis::RenderUnit unit) const
Converts a size from painter units (pixels) to the specified render unit.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
QSize outputSize() const
Returns the size of the resulting rendered image, in pixels.
QgsRectangle mapExtent() const
Returns the original extent of the map being rendered.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Renders polygons using a single fill and stroke color.
A simple line symbol layer, which renders lines using a line in a variety of styles (e....
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
Represent a 2-dimensional vector.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
#define BUILTIN_UNREACHABLE
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
QList< QgsSymbolLayer * > QgsSymbolLayerList
Utility class for identifying a unique vertex within a geometry.