QGIS API Documentation 3.99.0-Master (a8f284845db)
Loading...
Searching...
No Matches
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
20#include <functional>
21
22#include "qgis_core.h"
23#include "qgsannotation.h"
24#include "qgshtmlannotation.h"
25#include "qgssvgannotation.h"
26#include "qgstextannotation.h"
27
28#include <QString>
29
30#define SIP_NO_FILE
31
32using namespace Qt::StringLiterals;
33
35
36// None of this is stable API!
37
39typedef std::function < QgsAnnotation*() > QgsCreateAnnotationFunc;
40
46class CORE_EXPORT QgsAnnotationMetadata
47{
48 public:
49
54 QgsAnnotationMetadata( const QString &typeName, const QgsCreateAnnotationFunc &createFunc )
55 : mTypeName( typeName )
56 , mCreateFunc( createFunc )
57 {}
58
62 QString type() const { return mTypeName; }
63
67 QgsAnnotation *createAnnotation() const { return mCreateFunc ? mCreateFunc() : nullptr ; }
68
69 private:
70
71 QString mTypeName;
72 QgsCreateAnnotationFunc mCreateFunc = nullptr;
73
74 QgsAnnotationMetadata() = default;
75 friend class QMap< QString, QgsAnnotationMetadata >;
76
77};
78
84class CORE_EXPORT QgsAnnotationRegistry
85{
86
87 public:
88
93 QgsAnnotationRegistry()
94 {
95 addAnnotationType( QgsAnnotationMetadata( u"TextAnnotationItem"_s, QgsTextAnnotation::create ) );
96 addAnnotationType( QgsAnnotationMetadata( u"HtmlAnnotationItem"_s, QgsHtmlAnnotation::create ) );
97 addAnnotationType( QgsAnnotationMetadata( u"SVGAnnotationItem"_s, QgsSvgAnnotation::create ) );
98 }
99
105 bool addAnnotationType( const QgsAnnotationMetadata &metadata )
106 {
107 if ( mMetadata.contains( metadata.type() ) )
108 return false;
109
110 mMetadata.insert( metadata.type(), metadata );
111 return true;
112 }
113
118 QgsAnnotation *create( const QString &typeName ) const
119 {
120 if ( !mMetadata.contains( typeName ) )
121 return nullptr;
122
123 return mMetadata.value( typeName ).createAnnotation();
124 }
125
126 private:
127
128 QMap< QString, QgsAnnotationMetadata > mMetadata;
129
130};
131
133
134#endif // QGSANNOTATIONREGISTRY_H
Abstract base class for annotation items which are drawn over a map.
static QgsHtmlAnnotation * create()
Returns a new QgsHtmlAnnotation object.
static QgsSvgAnnotation * create()
Returns a new QgsSvgAnnotation object.
static QgsTextAnnotation * create()
Returns a new QgsTextAnnotation object.