34#include "moc_qgsannotation.cpp"
36using namespace Qt::StringLiterals;
43 props.insert( u
"color"_s, u
"white"_s );
44 props.insert( u
"style"_s, u
"solid"_s );
45 props.insert( u
"style_border"_s, u
"solid"_s );
46 props.insert( u
"color_border"_s, u
"black"_s );
47 props.insert( u
"width_border"_s, u
"0.3"_s );
48 props.insert( u
"joinstyle"_s, u
"miter"_s );
65 if ( mHasFixedMapPosition == fixed )
68 mHasFixedMapPosition = fixed;
74 mMapPosition = position;
80 mMapPositionCrs = crs;
86 mRelativePosition = position;
98 return mOffsetFromReferencePoint / 3.7795275;
103 mOffsetFromReferencePoint = offset;
117 return mFrameSize / 3.7795275;
130 mContentsMargins = margins;
136 mFillSymbol.reset( symbol );
142 return mFillSymbol.get();
147 QPainter *painter = context.
painter();
156 drawFrame( context );
157 if ( mHasFixedMapPosition )
159 drawMarkerSymbol( context );
161 if ( mHasFixedMapPosition )
183 mMarkerSymbol.reset( symbol );
226 return QSizeF( 0, 0 );
234 auto scaleSize = [&context](
double size )->
double
239 const QRectF frameRect( mHasFixedMapPosition ? scaleSize( mOffsetFromReferencePoint.x() ) : 0,
240 mHasFixedMapPosition ? scaleSize( mOffsetFromReferencePoint.y() ) : 0,
241 scaleSize( mFrameSize.width() ),
242 scaleSize( mFrameSize.height() ) );
243 const QgsPointXY origin = mHasFixedMapPosition ? QgsPointXY( 0, 0 ) : QgsPointXY( frameRect.center().x(), frameRect.center().y() );
247 mFillSymbol->startRender( context );
248 const QVector<QPolygonF> rings;
249 mFillSymbol->renderPolygon( poly, &rings,
nullptr, context );
250 mFillSymbol->stopRender( context );
262 mMarkerSymbol->startRender( context );
263 mMarkerSymbol->renderPoint( QPointF( 0, 0 ),
nullptr, context );
264 mMarkerSymbol->stopRender( context );
270 if ( itemElem.isNull() )
274 QDomElement annotationElem = doc.createElement( u
"AnnotationItem"_s );
275 annotationElem.setAttribute( u
"mapPositionFixed"_s, mHasFixedMapPosition );
276 annotationElem.setAttribute( u
"mapPosX"_s,
qgsDoubleToString( mMapPosition.x() ) );
277 annotationElem.setAttribute( u
"mapPosY"_s,
qgsDoubleToString( mMapPosition.y() ) );
278 if ( mMapPositionCrs.isValid() )
279 mMapPositionCrs.writeXml( annotationElem, doc );
280 annotationElem.setAttribute( u
"offsetXMM"_s,
qgsDoubleToString( mOffsetFromReferencePoint.x() ) );
281 annotationElem.setAttribute( u
"offsetYMM"_s,
qgsDoubleToString( mOffsetFromReferencePoint.y() ) );
282 annotationElem.setAttribute( u
"frameWidthMM"_s,
qgsDoubleToString( mFrameSize.width() ) );
283 annotationElem.setAttribute( u
"frameHeightMM"_s,
qgsDoubleToString( mFrameSize.height() ) );
284 annotationElem.setAttribute( u
"canvasPosX"_s,
qgsDoubleToString( mRelativePosition.x() ) );
285 annotationElem.setAttribute( u
"canvasPosY"_s,
qgsDoubleToString( mRelativePosition.y() ) );
286 annotationElem.setAttribute( u
"contentsMargin"_s, mContentsMargins.toString() );
287 annotationElem.setAttribute( u
"visible"_s,
isVisible() );
290 annotationElem.setAttribute( u
"mapLayer"_s, mMapLayer->id() );
295 if ( !symbolElem.isNull() )
297 annotationElem.appendChild( symbolElem );
302 QDomElement fillElem = doc.createElement( u
"fillSymbol"_s );
304 if ( !symbolElem.isNull() )
306 fillElem.appendChild( symbolElem );
307 annotationElem.appendChild( fillElem );
310 itemElem.appendChild( annotationElem );
315 if ( annotationElem.isNull() )
320 pos.setX( annotationElem.attribute( u
"canvasPosX"_s, u
"0"_s ).toDouble() );
321 pos.setY( annotationElem.attribute( u
"canvasPosY"_s, u
"0"_s ).toDouble() );
322 if ( pos.x() >= 1 || pos.x() < 0 || pos.y() < 0 || pos.y() >= 1 )
323 mRelativePosition = QPointF();
325 mRelativePosition = pos;
327 mapPos.
setX( annotationElem.attribute( u
"mapPosX"_s, u
"0"_s ).toDouble() );
328 mapPos.
setY( annotationElem.attribute( u
"mapPosY"_s, u
"0"_s ).toDouble() );
329 mMapPosition = mapPos;
331 if ( !mMapPositionCrs.readXml( annotationElem ) )
338 if ( annotationElem.hasAttribute( u
"frameWidthMM"_s ) )
339 mFrameSize.setWidth( annotationElem.attribute( u
"frameWidthMM"_s, u
"5"_s ).toDouble() );
341 mFrameSize.setWidth( dpiScale * annotationElem.attribute( u
"frameWidth"_s, u
"50"_s ).toDouble() );
342 if ( annotationElem.hasAttribute( u
"frameHeightMM"_s ) )
343 mFrameSize.setHeight( annotationElem.attribute( u
"frameHeightMM"_s, u
"3"_s ).toDouble() );
345 mFrameSize.setHeight( dpiScale * annotationElem.attribute( u
"frameHeight"_s, u
"50"_s ).toDouble() );
347 if ( annotationElem.hasAttribute( u
"offsetXMM"_s ) )
348 mOffsetFromReferencePoint.setX( annotationElem.attribute( u
"offsetXMM"_s, u
"0"_s ).toDouble() );
350 mOffsetFromReferencePoint.setX( dpiScale * annotationElem.attribute( u
"offsetX"_s, u
"0"_s ).toDouble() );
351 if ( annotationElem.hasAttribute( u
"offsetYMM"_s ) )
352 mOffsetFromReferencePoint.setY( annotationElem.attribute( u
"offsetYMM"_s, u
"0"_s ).toDouble() );
354 mOffsetFromReferencePoint.setY( dpiScale * annotationElem.attribute( u
"offsetY"_s, u
"0"_s ).toDouble() );
356 mHasFixedMapPosition = annotationElem.attribute( u
"mapPositionFixed"_s, u
"1"_s ).toInt();
357 mVisible = annotationElem.attribute( u
"visible"_s, u
"1"_s ).toInt();
358 if ( annotationElem.hasAttribute( u
"mapLayer"_s ) )
365 const QDomElement symbolElem = annotationElem.firstChildElement( u
"symbol"_s );
366 if ( !symbolElem.isNull() )
371 mMarkerSymbol = std::move( symbol );
376 mFillSymbol.reset(
nullptr );
377 const QDomElement fillElem = annotationElem.firstChildElement( u
"fillSymbol"_s );
378 if ( !fillElem.isNull() )
380 const QDomElement symbolElem = fillElem.firstChildElement( u
"symbol"_s );
381 if ( !symbolElem.isNull() )
386 mFillSymbol = std::move( symbol );
393 frameColor.setNamedColor( annotationElem.attribute( u
"frameColor"_s, u
"#000000"_s ) );
394 frameColor.setAlpha( annotationElem.attribute( u
"frameColorAlpha"_s, u
"255"_s ).toInt() );
395 QColor frameBackgroundColor;
396 frameBackgroundColor.setNamedColor( annotationElem.attribute( u
"frameBackgroundColor"_s ) );
397 frameBackgroundColor.setAlpha( annotationElem.attribute( u
"frameBackgroundColorAlpha"_s, u
"255"_s ).toInt() );
398 double frameBorderWidth = annotationElem.attribute( u
"frameBorderWidth"_s, u
"0.5"_s ).toDouble();
400 frameBorderWidth = frameBorderWidth * 25.4 / 96.0;
402 props.insert( u
"color"_s, frameBackgroundColor.name() );
403 props.insert( u
"style"_s, u
"solid"_s );
404 props.insert( u
"style_border"_s, u
"solid"_s );
405 props.insert( u
"color_border"_s, frameColor.name() );
406 props.insert( u
"width_border"_s, QString::number( frameBorderWidth ) );
407 props.insert( u
"joinstyle"_s, u
"miter"_s );
416 target->mVisible = mVisible;
417 target->mHasFixedMapPosition = mHasFixedMapPosition;
418 target->mMapPosition = mMapPosition;
419 target->mMapPositionCrs = mMapPositionCrs;
420 target->mRelativePosition = mRelativePosition;
421 target->mOffsetFromReferencePoint = mOffsetFromReferencePoint;
422 target->mFrameSize = mFrameSize;
423 target->mMarkerSymbol.reset( mMarkerSymbol ? mMarkerSymbol->clone() :
nullptr );
424 target->mContentsMargins = mContentsMargins;
425 target->mFillSymbol.reset( mFillSymbol ? mFillSymbol->clone() :
nullptr );
426 target->mSegmentPointWidthMm = mSegmentPointWidthMm;
427 target->mMapLayer = mMapLayer;
428 target->mFeature = mFeature;
@ Millimeters
Millimeters.
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
Q_DECL_DEPRECATED void setFrameSize(QSizeF size)
Sets the size (in pixels) of the annotation's frame (the main area in which the annotation's content ...
void setFillSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used for rendering the annotation frame.
Q_DECL_DEPRECATED void setFrameOffsetFromReferencePoint(QPointF offset)
Sets the annotation's frame's offset (in pixels) from the mapPosition() reference point.
void setRelativePosition(QPointF position)
Sets the relative position of the annotation, if it is not attached to a fixed map position.
virtual void renderAnnotation(QgsRenderContext &context, QSizeF size) const =0
Renders the annotation's contents to a target /a context at the specified /a size.
void setMapPosition(const QgsPointXY &position)
Sets the map position of the annotation, if it is attached to a fixed map position.
void moved()
Emitted when the annotation's position has changed and items need to be moved to reflect this.
Q_DECL_DEPRECATED QPointF frameOffsetFromReferencePoint() const
Returns the annotation's frame's offset (in pixels) from the mapPosition() reference point.
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated within ...
void setContentsMargin(const QgsMargins &margins)
Sets the margins (in millimeters) between the outside of the frame and the annotation content.
void setFrameSizeMm(QSizeF size)
Sets the size (in millimeters) of the annotation's frame (the main area in which the annotation's con...
virtual void setAssociatedFeature(const QgsFeature &feature)
Sets the feature associated with the annotation.
void setFrameOffsetFromReferencePointMm(QPointF offset)
Sets the annotation's frame's offset (in millimeters) from the mapPosition() reference point.
void setMapPositionCrs(const QgsCoordinateReferenceSystem &crs)
Sets the CRS of the map position.
~QgsAnnotation() override
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
void render(QgsRenderContext &context) const
Renders the annotation to a target render context.
virtual QSizeF minimumFrameSize() const
Returns the minimum frame size for the annotation.
bool isVisible() const
Returns true if the annotation is visible and should be rendered.
void setHasFixedMapPosition(bool fixed)
Sets whether the annotation is attached to a fixed map position, or uses a position relative to the c...
QgsAnnotation(QObject *parent=nullptr)
Constructor for QgsAnnotation.
void setMarkerSymbol(QgsMarkerSymbol *symbol)
Sets the symbol that is drawn at the annotation's map position.
QgsFillSymbol * fillSymbol() const
Returns the symbol that is used for rendering the annotation frame.
void setVisible(bool visible)
Sets whether the annotation is visible and should be rendered.
void mapLayerChanged()
Emitted when the map layer associated with the annotation changes.
void setMapLayer(QgsMapLayer *layer)
Sets the map layer associated with the annotation.
Represents a coordinate reference system (CRS).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isCanceled() const
Tells whether the operation has been canceled already.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
Base class for all map layer types.
Defines the four margins of a rectangle.
static QgsMargins fromString(const QString &string)
Returns a QgsMargins object decoded from a string, or a null QgsMargins if the string could not be in...
A marker symbol type, for rendering Point and MultiPoint geometries.
static int qtDefaultDpiX()
Returns the default Qt horizontal DPI.
void setY(double y)
Sets the y value of the point.
void setX(double x)
Sets the x value of the point.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
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.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
Scoped object for saving and restoring a QPainter object's state.
static QPolygonF createBalloon(const QgsPointXY &origin, const QRectF &rect, double wedgeWidth)
Generates a "balloon"/"talking bubble" style shape (as a QPolygonF).
An interface for classes which can visit style entity (e.g.
@ Annotation
An individual annotation.
virtual bool visitExit(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor stops visiting a node.
virtual bool visitEnter(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor starts visiting a node.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A symbol entity for QgsStyle databases.
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.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Contains information relating to a node (i.e.
Contains information relating to the style entity currently being visited.