QGIS API Documentation  3.6.0-Noosa (5873452)
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 
23  : QObject( project )
24  , mProject( project )
25 {
26 
27 }
28 
30 {
31  clear();
32 }
33 
35 {
36  if ( !annotation )
37  return false;
38 
39  if ( mAnnotations.contains( annotation ) )
40  return true;
41 
42  mAnnotations << annotation;
43  emit annotationAdded( annotation );
44  mProject->setDirty( true );
45  return true;
46 }
47 
49 {
50  if ( !annotation )
51  return false;
52 
53  if ( !mAnnotations.contains( annotation ) )
54  return false;
55 
56  emit annotationAboutToBeRemoved( annotation );
57  mAnnotations.removeAll( annotation );
58  delete annotation;
59  emit annotationRemoved();
60  mProject->setDirty( true );
61  return true;
62 }
63 
65 {
66  for ( auto *a : qgis::as_const( mAnnotations ) )
67  {
68  removeAnnotation( a );
69  }
70 }
71 
72 QList<QgsAnnotation *> QgsAnnotationManager::annotations() const
73 {
74  return mAnnotations;
75 }
76 
77 QList<QgsAnnotation *> QgsAnnotationManager::cloneAnnotations() const
78 {
79  QList<QgsAnnotation *> results;
80  for ( const auto *a : qgis::as_const( mAnnotations ) )
81  {
82  results << a->clone();
83  }
84  return results;
85 }
86 
87 bool QgsAnnotationManager::readXml( const QDomElement &element, const QgsReadWriteContext &context )
88 {
89  clear();
90  //restore each annotation
91  bool result = true;
92 
93  QDomElement annotationsElem = element.firstChildElement( QStringLiteral( "Annotations" ) );
94 
95  QDomNodeList annotationNodes = annotationsElem.elementsByTagName( QStringLiteral( "Annotation" ) );
96  for ( int i = 0; i < annotationNodes.size(); ++i )
97  {
98  createAnnotationFromXml( annotationNodes.at( i ).toElement(), context );
99  }
100 
101  // restore old (pre 3.0) project annotations
102  QDomNodeList oldItemList = element.elementsByTagName( QStringLiteral( "TextAnnotationItem" ) );
103  for ( int i = 0; i < oldItemList.size(); ++i )
104  {
105  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
106  }
107  oldItemList = element.elementsByTagName( QStringLiteral( "FormAnnotationItem" ) );
108  for ( int i = 0; i < oldItemList.size(); ++i )
109  {
110  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
111  }
112  oldItemList = element.elementsByTagName( QStringLiteral( "HtmlAnnotationItem" ) );
113  for ( int i = 0; i < oldItemList.size(); ++i )
114  {
115  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
116  }
117  oldItemList = element.elementsByTagName( QStringLiteral( "SVGAnnotationItem" ) );
118  for ( int i = 0; i < oldItemList.size(); ++i )
119  {
120  createAnnotationFromXml( oldItemList.at( i ).toElement(), context );
121  }
122 
123  return result;
124 }
125 
126 QDomElement QgsAnnotationManager::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
127 {
128  QDomElement annotationsElem = doc.createElement( QStringLiteral( "Annotations" ) );
129  QListIterator<QgsAnnotation *> i( mAnnotations );
130  // save lowermost annotation (at end of list) first
131  i.toBack();
132  while ( i.hasPrevious() )
133  {
134  QgsAnnotation *annotation = i.previous();
135 
136  if ( !annotation )
137  {
138  continue;
139  }
140 
141  annotation->writeXml( annotationsElem, doc, context );
142  }
143  return annotationsElem;
144 }
145 
146 void QgsAnnotationManager::createAnnotationFromXml( const QDomElement &element, const QgsReadWriteContext &context )
147 {
148  QString type = element.tagName();
149  QgsAnnotation *annotation = QgsApplication::annotationRegistry()->create( type );
150  if ( !annotation )
151  return;
152 
153  annotation->readXml( element, context );
154 
155  if ( !annotation->mapPositionCrs().isValid() )
156  {
157  annotation->setMapPositionCrs( mProject->crs() );
158  }
159 
160  addAnnotation( annotation );
161 }
void clear()
Removes and deletes all annotations from the manager.
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:460
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:49
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:95
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:89
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.