QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsannotationregistry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsannotationregistry.h
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 #ifndef QGSANNOTATIONREGISTRY_H
17 #define QGSANNOTATIONREGISTRY_H
18 
19 #define SIP_NO_FILE
20 
21 #include "qgis_core.h"
22 #include "qgsannotation.h"
23 #include "qgstextannotation.h"
24 #include "qgssvgannotation.h"
25 #include "qgshtmlannotation.h"
26 #include <QString>
27 #include <functional>
28 
30 
31 // None of this is stable API!
32 
34 typedef std::function < QgsAnnotation*() > QgsCreateAnnotationFunc;
35 
42 class CORE_EXPORT QgsAnnotationMetadata
43 {
44  public:
45 
50  QgsAnnotationMetadata( const QString &typeName, const QgsCreateAnnotationFunc &createFunc )
51  : mTypeName( typeName )
52  , mCreateFunc( createFunc )
53  {}
54 
58  QString type() const { return mTypeName; }
59 
63  QgsAnnotation *createAnnotation() const { return mCreateFunc ? mCreateFunc() : nullptr ; }
64 
65  private:
66 
67  QString mTypeName;
68  QgsCreateAnnotationFunc mCreateFunc = nullptr;
69 
70  QgsAnnotationMetadata() = default;
71  friend class QMap< QString, QgsAnnotationMetadata >;
72 
73 };
74 
81 class CORE_EXPORT QgsAnnotationRegistry
82 {
83 
84  public:
85 
90  QgsAnnotationRegistry()
91  {
92  addAnnotationType( QgsAnnotationMetadata( QStringLiteral( "TextAnnotationItem" ), QgsTextAnnotation::create ) );
93  addAnnotationType( QgsAnnotationMetadata( QStringLiteral( "HtmlAnnotationItem" ), QgsHtmlAnnotation::create ) );
94  addAnnotationType( QgsAnnotationMetadata( QStringLiteral( "SVGAnnotationItem" ), QgsSvgAnnotation::create ) );
95  }
96 
102  bool addAnnotationType( const QgsAnnotationMetadata &metadata )
103  {
104  if ( mMetadata.contains( metadata.type() ) )
105  return false;
106 
107  mMetadata.insert( metadata.type(), metadata );
108  return true;
109  }
110 
115  QgsAnnotation *create( const QString &typeName ) const
116  {
117  if ( !mMetadata.contains( typeName ) )
118  return nullptr;
119 
120  return mMetadata.value( typeName ).createAnnotation();
121  }
122 
123  private:
124 
125  QMap< QString, QgsAnnotationMetadata > mMetadata;
126 
127 };
128 
130 
131 #endif // QGSANNOTATIONREGISTRY_H
static QgsTextAnnotation * create()
Returns a new QgsTextAnnotation object.
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:48
const QString & typeName
static QgsSvgAnnotation * create()
Returns a new QgsSvgAnnotation object.
static QgsHtmlAnnotation * create()
Returns a new QgsHtmlAnnotation object.