QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsannotationmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsannotationmanager.cpp
3  ------------------------
4  Date : January 2017
5  Copyright : (C) 2017 Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsannotationmanager.h"
17 #include "qgsproject.h"
18 #include "qgsannotation.h"
19 #include "qgsannotationregistry.h"
20 #include "qgsapplication.h"
21 #include "qgsstyleentityvisitor.h"
22 
24  : QObject( project )
25  , mProject( project )
26 {
27 
28 }
29 
31 {
32  clear();
33 }
34 
36 {
37  if ( !annotation )
38  return false;
39 
40  if ( mAnnotations.contains( annotation ) )
41  return true;
42 
43  mAnnotations << annotation;
44  emit annotationAdded( annotation );
45  mProject->setDirty( true );
46  return true;
47 }
48 
50 {
51  if ( !annotation )
52  return false;
53 
54  if ( !mAnnotations.contains( annotation ) )
55  return false;
56 
57  emit annotationAboutToBeRemoved( annotation );
58  mAnnotations.removeAll( annotation );
59  delete annotation;
60  emit annotationRemoved();
61  mProject->setDirty( true );
62  return true;
63 }
64 
66 {
67  for ( auto *a : qgis::as_const( mAnnotations ) )
68  {
69  removeAnnotation( a );
70  }
71 }
72 
73 QList<QgsAnnotation *> QgsAnnotationManager::annotations() const
74 {
75  return mAnnotations;
76 }
77 
78 QList<QgsAnnotation *> QgsAnnotationManager::cloneAnnotations() const
79 {
80  QList<QgsAnnotation *> results;
81  for ( const auto *a : qgis::as_const( mAnnotations ) )
82  {
83  results << a->clone();
84  }
85  return results;
86 }
87 
88 bool QgsAnnotationManager::readXml( const QDomElement &element, const QgsReadWriteContext &context )
89 {
90  clear();
91  //restore each annotation
92  bool result = true;
93 
94  QDomElement annotationsElem = element.firstChildElement( QStringLiteral( "Annotations" ) );
95 
96  QDomNodeList annotationNodes = annotationsElem.elementsByTagName( QStringLiteral( "Annotation" ) );
97  for ( int i = 0; i < annotationNodes.size(); ++i )
98  {
99  createAnnotationFromXml( annotationNodes.at( i ).toElement(), context );
100  }
101 
102  // restore old (pre 3.0) project annotations
103  QDomNodeList oldItemList = element.elementsByTagName( QStringLiteral( "TextAnnotationItem" ) );
104  for ( int i = 0; i < oldItemList.size(); ++i )
105  {
106  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
107  }
108  oldItemList = element.elementsByTagName( QStringLiteral( "FormAnnotationItem" ) );
109  for ( int i = 0; i < oldItemList.size(); ++i )
110  {
111  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
112  }
113  oldItemList = element.elementsByTagName( QStringLiteral( "HtmlAnnotationItem" ) );
114  for ( int i = 0; i < oldItemList.size(); ++i )
115  {
116  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
117  }
118  oldItemList = element.elementsByTagName( QStringLiteral( "SVGAnnotationItem" ) );
119  for ( int i = 0; i < oldItemList.size(); ++i )
120  {
121  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
122  }
123 
124  return result;
125 }
126 
127 QDomElement QgsAnnotationManager::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
128 {
129  QDomElement annotationsElem = doc.createElement( QStringLiteral( "Annotations" ) );
130  QListIterator<QgsAnnotation *> i( mAnnotations );
131  // save lowermost annotation (at end of list) first
132  i.toBack();
133  while ( i.hasPrevious() )
134  {
135  QgsAnnotation *annotation = i.previous();
136 
137  if ( !annotation )
138  {
139  continue;
140  }
141 
142  annotation->writeXml( annotationsElem, doc, context );
143  }
144  return annotationsElem;
145 }
146 
148 {
149  if ( mAnnotations.empty() )
150  return true;
151 
152  // NOTE: if visitEnter returns false it means "don't visit any annotations", not "abort all further visitations"
153  if ( !visitor->visitEnter( QgsStyleEntityVisitorInterface::Node( QgsStyleEntityVisitorInterface::NodeType::Annotations, QStringLiteral( "annotations" ), tr( "Annotations" ) ) ) )
154  return true;
155 
156  for ( QgsAnnotation *a : mAnnotations )
157  {
158  if ( !a->accept( visitor ) )
159  return false;
160  }
161 
162  if ( !visitor->visitExit( QgsStyleEntityVisitorInterface::Node( QgsStyleEntityVisitorInterface::NodeType::Annotations, QStringLiteral( "annotations" ), tr( "Annotations" ) ) ) )
163  return false;
164 
165  return true;
166 }
167 
168 void QgsAnnotationManager::createAnnotationFromXml( const QDomElement &element, const QgsReadWriteContext &context )
169 {
170  QString type = element.tagName();
171  QgsAnnotation *annotation = QgsApplication::annotationRegistry()->create( type );
172  if ( !annotation )
173  return;
174 
175  annotation->readXml( element, context );
176 
177  if ( !annotation->mapPositionCrs().isValid() )
178  {
179  annotation->setMapPositionCrs( mProject->crs() );
180  }
181 
182  addAnnotation( annotation );
183 }
QgsAnnotationManager::annotationAdded
void annotationAdded(QgsAnnotation *annotation)
Emitted when a annotation has been added to the manager.
QgsAnnotation::setMapPositionCrs
void setMapPositionCrs(const QgsCoordinateReferenceSystem &crs)
Sets the CRS of the map position.
Definition: qgsannotation.cpp:69
QgsAnnotation::writeXml
virtual void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const =0
Writes the annotation state to a DOM element.
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
qgsannotation.h
QgsApplication::annotationRegistry
static QgsAnnotationRegistry * annotationRegistry()
Returns the application's annotation registry, used for managing annotation types.
Definition: qgsapplication.cpp:2268
QgsAnnotation::readXml
virtual void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context)=0
Restores the annotation's state from a DOM element.
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g.
Definition: qgsstyleentityvisitor.h:34
QgsStyleEntityVisitorInterface::visitExit
virtual bool visitExit(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor stops visiting a node.
Definition: qgsstyleentityvisitor.h:183
QgsStyleEntityVisitorInterface::NodeType::Annotations
@ Annotations
Annotations collection.
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
qgsannotationregistry.h
qgsapplication.h
QgsAnnotationManager::cloneAnnotations
QList< QgsAnnotation * > cloneAnnotations() const
Returns a list containing clones of all annotations contained in the manager.
Definition: qgsannotationmanager.cpp:78
QgsAnnotationManager::removeAnnotation
bool removeAnnotation(QgsAnnotation *annotation)
Removes an annotation from the manager.
Definition: qgsannotationmanager.cpp:49
QgsAnnotationManager::addAnnotation
bool addAnnotation(QgsAnnotation *annotation)
Adds an annotation to the manager.
Definition: qgsannotationmanager.cpp:35
QgsAnnotation
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:50
QgsAnnotationManager::annotationAboutToBeRemoved
void annotationAboutToBeRemoved(QgsAnnotation *annotation)
Emitted when an annotation is about to be removed from the manager.
QgsAnnotationManager::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the state of the manager.
Definition: qgsannotationmanager.cpp:127
QgsCoordinateReferenceSystem::isValid
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Definition: qgscoordinatereferencesystem.cpp:924
QgsAnnotationManager::clear
void clear()
Removes and deletes all annotations from the manager.
Definition: qgsannotationmanager.cpp:65
QgsProject::setDirty
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:519
qgsannotationmanager.h
QgsStyleEntityVisitorInterface::Node
Contains information relating to a node (i.e.
Definition: qgsstyleentityvisitor.h:111
QgsStyleEntityVisitorInterface::visitEnter
virtual bool visitEnter(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor starts visiting a node.
Definition: qgsstyleentityvisitor.h:169
QgsAnnotationManager::accept
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated within ...
Definition: qgsannotationmanager.cpp:147
QgsAnnotationManager::annotations
QList< QgsAnnotation * > annotations() const
Returns a list of all annotations contained in the manager.
Definition: qgsannotationmanager.cpp:73
QgsAnnotationManager::readXml
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.
Definition: qgsannotationmanager.cpp:88
QgsAnnotationManager::annotationRemoved
void annotationRemoved()
Emitted when an annotation was removed from the manager.
QgsAnnotation::mapPositionCrs
QgsCoordinateReferenceSystem mapPositionCrs() const
Returns the CRS of the map position, or an invalid CRS if the annotation does not have a fixed map po...
Definition: qgsannotation.h:135
QgsAnnotationManager::QgsAnnotationManager
QgsAnnotationManager(QgsProject *project=nullptr)
Constructor for QgsAnnotationManager.
Definition: qgsannotationmanager.cpp:23
QgsProject::crs
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:100
qgsproject.h
qgsstyleentityvisitor.h
QgsAnnotationManager::~QgsAnnotationManager
~QgsAnnotationManager() override
Definition: qgsannotationmanager.cpp:30