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();
221 if ( mDrawBackground && mBackgroundSymbol )
228 mBackgroundSymbol->startRender( context );
229 mBackgroundSymbol->renderPolygon( painterBounds,
nullptr,
nullptr, context );
230 mBackgroundSymbol->stopRender( context );
238 renderCallout( context, painterBounds, angle, calloutContext, feedback );
249 if ( mDrawFrame && mFrameSymbol )
251 mFrameSymbol->startRender( context );
252 mFrameSymbol->renderPolygon( painterBounds,
nullptr,
nullptr, context );
253 mFrameSymbol->stopRender( context );
259 QList<QgsAnnotationItemNode> res;
260 switch ( mPlacementMode )
278 calloutNodePoint = mBounds.center();
313 switch ( operation->
type() )
320 switch ( mPlacementMode )
327 mBounds =
QgsRectangle( moveOperation->
after().
x(), moveOperation->
after().
y(), mBounds.xMaximum(), mBounds.yMaximum() );
330 mBounds =
QgsRectangle( mBounds.xMinimum(), moveOperation->
after().
y(), moveOperation->
after().
x(), mBounds.yMaximum() );
333 mBounds =
QgsRectangle( mBounds.xMinimum(), mBounds.yMinimum(), moveOperation->
after().
x(), moveOperation->
after().
y() );
336 mBounds =
QgsRectangle( moveOperation->
after().
x(), mBounds.yMinimum(), mBounds.xMaximum(), moveOperation->
after().
y() );
374 switch ( mPlacementMode )
419 mRotation = std::fmod( mRotation + rotateOperation->
angle(), 360.0 );
425 mBounds = qgis::down_cast< QgsAnnotationItemEditOperationSetItemBounds * >( operation )->bounds();
438 switch ( operation->
type() )
445 switch ( mPlacementMode )
498 switch ( mPlacementMode )
520 QPointF anchorPoint = anchor.
asQPointF();
523 double x = anchorPoint.x();
524 double y = anchorPoint.y();
527 anchorPoint = QPointF( x, y );
532 const double textOriginXPixels = anchorPoint.x() + calloutOffsetWidthPixels;
533 const double textOriginYPixels = anchorPoint.y() + calloutOffsetHeightPixels;
539 QVector<QgsPointXY> {
540 QgsPointXY( textOriginXPixels, textOriginYPixels ),
541 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels ),
542 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels + heightPixels ),
543 QgsPointXY( textOriginXPixels, textOriginYPixels + heightPixels ),
544 QgsPointXY( textOriginXPixels, textOriginYPixels )
575 const QgsRectangle newBounds = qgis::down_cast< QgsAnnotationItemEditOperationSetItemBounds * >( operation )->bounds();
590 switch ( mPlacementMode )
624 switch ( mPlacementMode )
634 QRectF boundsInPixels;
642 QPointF anchorPoint = anchor.
asQPointF();
645 double x = anchorPoint.x();
646 double y = anchorPoint.y();
649 anchorPoint = QPointF( x, y );
655 textRect( anchorPoint.x() + calloutOffsetWidthPixels, anchorPoint.y() + calloutOffsetHeightPixels, anchorPoint.x() + calloutOffsetWidthPixels + widthPixels, anchorPoint.y() + calloutOffsetHeightPixels + heightPixels );
656 QgsRectangle anchorRect( anchorPoint.x(), anchorPoint.y(), anchorPoint.x(), anchorPoint.y() );
659 boundsInPixels = anchorRect.
toRectF();
663 QPointF center = mBounds.center().toQPointF();
666 double x = center.x();
667 double y = center.y();
670 center = QPointF( x, y );
674 boundsInPixels = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
691 QRectF boundsInPixels;
695 QPointF center( centerMapX, centerMapY );
698 double x = centerMapX;
699 double y = centerMapY;
702 center = QPointF( x, y );
706 boundsInPixels = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
728 return mBackgroundSymbol.get();
733 mBackgroundSymbol.reset( symbol );
738 return mFrameSymbol.get();
743 mFrameSymbol.reset( symbol );
757 if ( otherRect->mBackgroundSymbol )
761 if ( otherRect->mFrameSymbol )
775 element.setAttribute( u
"fixedWidth"_s,
qgsDoubleToString( mFixedSize.width() ) );
776 element.setAttribute( u
"fixedHeight"_s,
qgsDoubleToString( mFixedSize.height() ) );
787 element.setAttribute( u
"backgroundEnabled"_s, mDrawBackground ? u
"1"_s : u
"0"_s );
788 if ( mBackgroundSymbol )
790 QDomElement backgroundElement = document.createElement( u
"backgroundSymbol"_s );
792 element.appendChild( backgroundElement );
795 element.setAttribute( u
"frameEnabled"_s, mDrawFrame ? u
"1"_s : u
"0"_s );
798 QDomElement frameElement = document.createElement( u
"frameSymbol"_s );
800 element.appendChild( frameElement );
808 mBounds.setXMinimum( element.attribute( u
"xMin"_s ).toDouble() );
809 mBounds.setXMaximum( element.attribute( u
"xMax"_s ).toDouble() );
810 mBounds.setYMinimum( element.attribute( u
"yMin"_s ).toDouble() );
811 mBounds.setYMaximum( element.attribute( u
"yMax"_s ).toDouble() );
815 mFixedSize = QSizeF( element.attribute( u
"fixedWidth"_s ).toDouble(), element.attribute( u
"fixedHeight"_s ).toDouble() );
817 mRotation = element.attribute( u
"rotation"_s ).toDouble();
820 mDrawBackground = element.attribute( u
"backgroundEnabled"_s, u
"0"_s ).toInt();
821 const QDomElement backgroundSymbolElem = element.firstChildElement( u
"backgroundSymbol"_s ).firstChildElement();
822 if ( !backgroundSymbolElem.isNull() )
827 mDrawFrame = element.attribute( u
"frameEnabled"_s, u
"0"_s ).toInt();
828 const QDomElement frameSymbolElem = element.firstChildElement( u
"frameSymbol"_s ).firstChildElement();
829 if ( !frameSymbolElem.isNull() )
849 return mFixedSizeUnit;
854 mFixedSizeUnit = unit;
859 return mPlacementMode;
864 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.
@ SetItemBounds
Set the bounds of an item.
@ 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.
Scoped object for saving and restoring a QPainter object's state.
@ NoSave
Do not save the current painter state.
void save()
Performs a manual save on the painter state, pushing another saved state to the painter.
void restore()
Performs a manual restore on the painter state, popping the latest state from the painter.
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.