17#include "moc_qgsannotationmanager.cpp"
48 if ( mAnnotations.contains( annotation ) )
51 mAnnotations << annotation;
62 if ( !mAnnotations.contains( annotation ) )
66 mAnnotations.removeAll( annotation );
75 for (
auto *a : std::as_const( mAnnotations ) )
88 QList<QgsAnnotation *> results;
89 for (
const auto *a : std::as_const( mAnnotations ) )
91 results << a->clone();
103 return readXmlPrivate( element, context, layer, transformContext );
112 auto createAnnotationFromElement = [
this, &context, layer, &transformContext](
const QDomElement & element )
114 std::unique_ptr< QgsAnnotation > annotation( createAnnotationFromXml( element, context ) );
120 std::unique_ptr< QgsAnnotationItem > annotationItem = convertToAnnotationItem( annotation.get(), layer, transformContext );
121 if ( annotationItem )
123 layer->
addItem( annotationItem.release() );
137 QDomElement annotationsElem = element.firstChildElement( QStringLiteral(
"Annotations" ) );
139 QDomElement annotationElement = annotationsElem.firstChildElement( QStringLiteral(
"Annotation" ) );
140 while ( ! annotationElement.isNull() )
142 createAnnotationFromElement( annotationElement );
143 annotationElement = annotationElement.nextSiblingElement( QStringLiteral(
"Annotation" ) );
147 if ( annotationElement.isNull() )
149 QDomNodeList oldItemList = element.elementsByTagName( QStringLiteral(
"TextAnnotationItem" ) );
150 for (
int i = 0; i < oldItemList.size(); ++i )
152 createAnnotationFromElement( oldItemList.at( i ).toElement() );
154 oldItemList = element.elementsByTagName( QStringLiteral(
"FormAnnotationItem" ) );
155 for (
int i = 0; i < oldItemList.size(); ++i )
157 createAnnotationFromElement( oldItemList.at( i ).toElement() );
159 oldItemList = element.elementsByTagName( QStringLiteral(
"HtmlAnnotationItem" ) );
160 for (
int i = 0; i < oldItemList.size(); ++i )
162 createAnnotationFromElement( oldItemList.at( i ).toElement() );
164 oldItemList = element.elementsByTagName( QStringLiteral(
"SVGAnnotationItem" ) );
165 for (
int i = 0; i < oldItemList.size(); ++i )
167 createAnnotationFromElement( oldItemList.at( i ).toElement() );
178 destination->setEnabled( source->isVisible() );
179 if ( source->hasFixedMapPosition() )
181 QgsPointXY mapPosition = source->mapPosition();
185 mapPosition = transform.transform( mapPosition );
189 QgsDebugError( QStringLiteral(
"Error transforming annotation position" ) );
195 std::unique_ptr< QgsBalloonCallout > callout = std::make_unique< QgsBalloonCallout >();
197 callout->setFillSymbol( fill->clone() );
200 callout->setMarkerSymbol( marker->clone() );
201 callout->setMargins( source->contentsMargin() );
203 destination->setCallout( callout.release() );
206 if ( source->mapLayer() )
218 transform.transform( mapPosition );
222 QgsDebugError( QStringLiteral(
"Error transforming annotation position" ) );
225 std::unique_ptr< QgsAnnotationPictureItem > item = std::make_unique< QgsAnnotationPictureItem >(
Qgis::PictureFormat::SVG,
227 if ( !setCommonProperties( annotation, item.get() ) )
230 const QgsMargins margins = svg->contentsMargin();
231 item->setFixedSize( QSizeF( svg->frameSizeMm().width() - margins.
left() - margins.
right(),
232 svg->frameSizeMm().height() - margins.
top() - margins.
bottom() ) );
235 if ( svg->hasFixedMapPosition() )
239 item->setOffsetFromCallout( QSizeF( svg->frameOffsetFromReferencePointMm().x() + margins.
left(),
240 svg->frameOffsetFromReferencePointMm().y() + margins.
top() ) );
246 item->setBounds(
QgsRectangle( svg->relativePosition().x(), svg->relativePosition().y(),
247 svg->relativePosition().x(), svg->relativePosition().y() ) );
250 item->setBackgroundEnabled(
true );
251 item->setBackgroundSymbol( fill->clone() );
263 transform.transform( mapPosition );
267 QgsDebugError( QStringLiteral(
"Error transforming annotation position" ) );
270 std::unique_ptr< QgsAnnotationRectangleTextItem > item = std::make_unique< QgsAnnotationRectangleTextItem >( text->document()->toHtml(),
QgsRectangle::fromCenterAndSize( mapPosition, 1, 1 ) );
271 if ( !setCommonProperties( annotation, item.get() ) )
276 item->setFormat( format );
278 const QgsMargins margins = text->contentsMargin();
279 item->setFixedSize( QSizeF( text->frameSizeMm().width() - margins.
left() - margins.
right(),
280 text->frameSizeMm().height() - margins.
top() - margins.
bottom() ) );
283 if ( text->hasFixedMapPosition() )
287 item->setOffsetFromCallout( QSizeF( text->frameOffsetFromReferencePointMm().x() + margins.
left(),
288 text->frameOffsetFromReferencePointMm().y() + margins.
top() ) );
290 item->setBackgroundEnabled(
false );
291 item->setFrameEnabled(
false );
296 item->setBounds(
QgsRectangle( text->relativePosition().x(), text->relativePosition().y(),
297 text->relativePosition().x(), text->relativePosition().y() ) );
300 item->setBackgroundEnabled(
true );
301 item->setBackgroundSymbol( fill->clone() );
313 QDomElement annotationsElem = doc.createElement( QStringLiteral(
"Annotations" ) );
314 QListIterator<QgsAnnotation *> i( mAnnotations );
317 while ( i.hasPrevious() )
326 annotation->
writeXml( annotationsElem, doc, context );
328 return annotationsElem;
333 if ( mAnnotations.empty() )
342 if ( !a->accept( visitor ) )
354 QString type = element.tagName();
359 annotation->
readXml( element, context );
@ Millimeters
Millimeters.
@ 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 ...
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
Represents a map layer containing a set of georeferenced annotations, e.g.
void setLinkedVisibilityLayer(QgsMapLayer *layer)
Sets a linked layer, where the items in this annotation layer will only be visible when the linked la...
QString addItem(QgsAnnotationItem *item)
Adds an item to the layer.
QList< QgsAnnotation * > annotations() const
Returns a list of all annotations contained in the manager.
QList< QgsAnnotation * > cloneAnnotations() const
Returns a list containing clones of all annotations contained in the manager.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the state of the manager.
void annotationRemoved()
Emitted when an annotation was removed from the manager.
QgsAnnotationManager(QgsProject *project=nullptr)
Constructor for QgsAnnotationManager.
void annotationAboutToBeRemoved(QgsAnnotation *annotation)
Emitted when an annotation is about to be removed from the manager.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the manager's state from a DOM element, restoring all annotations present in the XML document.
bool readXmlAndUpgradeToAnnotationLayerItems(const QDomElement &element, const QgsReadWriteContext &context, QgsAnnotationLayer *layer, const QgsCoordinateTransformContext &transformContext)
Reads the manager's state from a DOM element, restoring annotations present in the XML document.
~QgsAnnotationManager() override
bool removeAnnotation(QgsAnnotation *annotation)
Removes an annotation from the manager.
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated within ...
void clear()
Removes and deletes all annotations from the manager.
bool addAnnotation(QgsAnnotation *annotation)
Adds an annotation to the manager.
void annotationAdded(QgsAnnotation *annotation)
Emitted when a annotation has been added to the manager.
Abstract base class for annotation items which are drawn over a map.
QgsCoordinateReferenceSystem mapPositionCrs() const
Returns the CRS of the map position, or an invalid CRS if the annotation does not have a fixed map po...
void setMapPositionCrs(const QgsCoordinateReferenceSystem &crs)
Sets the CRS of the map position.
virtual void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context)=0
Restores the annotation's state from a DOM element.
virtual void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const =0
Writes the annotation state to a DOM element.
static QgsAnnotationRegistry * annotationRegistry()
Returns the application's annotation registry, used for managing annotation types.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Contains information about the context in which a coordinate transform is executed.
Custom exception class for Coordinate Reference System related exceptions.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
QgsCoordinateReferenceSystem crs
The QgsMargins class defines the four margins of a rectangle.
double top() const
Returns the top margin.
double right() const
Returns the right margin.
double bottom() const
Returns the bottom margin.
double left() const
Returns the left margin.
A marker symbol type, for rendering Point and MultiPoint geometries.
A class to represent a 2D point.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QgsCoordinateReferenceSystem crs
void setDirty(bool b=true)
Flag the project as dirty (modified).
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
static QgsRectangle fromCenterAndSize(const QgsPointXY ¢er, double width, double height)
Creates a new rectangle, given the specified center point and width and height.
An interface for classes which can visit style entity (e.g.
@ Annotations
Annotations collection.
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.
An annotation which renders the contents of an SVG file.
An annotation item that displays formatted text from a QTextDocument document.
Container for all settings relating to text rendering.
void setAllowHtmlFormatting(bool allow)
Sets whether text should be treated as a HTML document and HTML tags should be used for formatting th...
#define QgsDebugError(str)
Contains information relating to a node (i.e.