QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
22  : QObject( project )
23  , mProject( project )
24 {
25 
26 }
27 
29 {
30  clear();
31 }
32 
34 {
35  if ( !annotation )
36  return false;
37 
38  if ( mAnnotations.contains( annotation ) )
39  return true;
40 
41  mAnnotations << annotation;
42  emit annotationAdded( annotation );
43  mProject->setDirty( true );
44  return true;
45 }
46 
48 {
49  if ( !annotation )
50  return false;
51 
52  if ( !mAnnotations.contains( annotation ) )
53  return false;
54 
55  emit annotationAboutToBeRemoved( annotation );
56  mAnnotations.removeAll( annotation );
57  delete annotation;
58  emit annotationRemoved();
59  mProject->setDirty( true );
60  return true;
61 }
62 
64 {
65  for ( auto *a : qgis::as_const( mAnnotations ) )
66  {
67  removeAnnotation( a );
68  }
69 }
70 
71 QList<QgsAnnotation *> QgsAnnotationManager::annotations() const
72 {
73  return mAnnotations;
74 }
75 
76 QList<QgsAnnotation *> QgsAnnotationManager::cloneAnnotations() const
77 {
78  QList<QgsAnnotation *> results;
79  for ( const auto *a : qgis::as_const( mAnnotations ) )
80  {
81  results << a->clone();
82  }
83  return results;
84 }
85 
86 bool QgsAnnotationManager::readXml( const QDomElement &element, const QgsReadWriteContext &context )
87 {
88  clear();
89  //restore each annotation
90  bool result = true;
91 
92  QDomElement annotationsElem = element.firstChildElement( QStringLiteral( "Annotations" ) );
93 
94  QDomNodeList annotationNodes = annotationsElem.elementsByTagName( QStringLiteral( "Annotation" ) );
95  for ( int i = 0; i < annotationNodes.size(); ++i )
96  {
97  createAnnotationFromXml( annotationNodes.at( i ).toElement(), context );
98  }
99 
100  // restore old (pre 3.0) project annotations
101  QDomNodeList oldItemList = element.elementsByTagName( QStringLiteral( "TextAnnotationItem" ) );
102  for ( int i = 0; i < oldItemList.size(); ++i )
103  {
104  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
105  }
106  oldItemList = element.elementsByTagName( QStringLiteral( "FormAnnotationItem" ) );
107  for ( int i = 0; i < oldItemList.size(); ++i )
108  {
109  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
110  }
111  oldItemList = element.elementsByTagName( QStringLiteral( "HtmlAnnotationItem" ) );
112  for ( int i = 0; i < oldItemList.size(); ++i )
113  {
114  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
115  }
116  oldItemList = element.elementsByTagName( QStringLiteral( "SVGAnnotationItem" ) );
117  for ( int i = 0; i < oldItemList.size(); ++i )
118  {
119  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
120  }
121 
122  return result;
123 }
124 
125 QDomElement QgsAnnotationManager::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
126 {
127  QDomElement annotationsElem = doc.createElement( QStringLiteral( "Annotations" ) );
128  QListIterator<QgsAnnotation *> i( mAnnotations );
129  // save lowermost annotation (at end of list) first
130  i.toBack();
131  while ( i.hasPrevious() )
132  {
133  QgsAnnotation *annotation = i.previous();
134 
135  if ( !annotation )
136  {
137  continue;
138  }
139 
140  annotation->writeXml( annotationsElem, doc, context );
141  }
142  return annotationsElem;
143 }
144 
145 void QgsAnnotationManager::createAnnotationFromXml( const QDomElement &element, const QgsReadWriteContext &context )
146 {
147  QString type = element.tagName();
148  QgsAnnotation *annotation = QgsApplication::annotationRegistry()->create( type );
149  if ( !annotation )
150  return;
151 
152  annotation->readXml( element, context );
153 
154  if ( !annotation->mapPositionCrs().isValid() )
155  {
156  annotation->setMapPositionCrs( mProject->crs() );
157  }
158 
159  addAnnotation( annotation );
160 }
void clear()
Removes and deletes all annotations from the manager.
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:421
The class is used as a container of context for various read/write operations on other objects...
static QgsAnnotationRegistry * annotationRegistry()
Returns the application&#39;s annotation registry, used for managing annotation types.
void annotationAdded(QgsAnnotation *annotation)
Emitted when a annotation has been added to the manager.
void annotationRemoved()
Emitted when an annotation was removed from the manager.
virtual void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const =0
Writes the annotation state to a DOM element.
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:48
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the state of the manager.
bool addAnnotation(QgsAnnotation *annotation)
Adds an annotation to the manager.
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:91
void annotationAboutToBeRemoved(QgsAnnotation *annotation)
Emitted when an annotation is about to be removed from the manager.
virtual void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context)=0
Restores the annotation&#39;s state from a DOM element.
Reads and writes project states.
Definition: qgsproject.h:85
QgsCoordinateReferenceSystem mapPositionCrs() const
Returns the CRS of the map position, or an invalid CRS if the annotation does not have a fixed map po...
QList< QgsAnnotation *> annotations() const
Returns a list of all annotations contained in the manager.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the manager&#39;s state from a DOM element, restoring all annotations present in the XML document...
QList< QgsAnnotation *> cloneAnnotations() const
Returns a list containing clones of all annotations contained in the manager.
bool removeAnnotation(QgsAnnotation *annotation)
Removes an annotation from the manager.
void setMapPositionCrs(const QgsCoordinateReferenceSystem &crs)
Sets the CRS of the map position.
QgsAnnotationManager(QgsProject *project=nullptr)
Constructor for QgsAnnotationManager.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.