QGIS API Documentation 3.99.0-Master (d270888f95f)
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#define SIP_NO_FILE
20
21#include <functional>
22
23#include "qgis_core.h"
24#include "qgsannotation.h"
25#include "qgshtmlannotation.h"
26#include "qgssvgannotation.h"
27#include "qgstextannotation.h"
28
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
34
35// None of this is stable API!
36
38typedef std::function < QgsAnnotation*() > QgsCreateAnnotationFunc;
39
45class CORE_EXPORT QgsAnnotationMetadata
46{
47 public:
48
53 QgsAnnotationMetadata( const QString &typeName, const QgsCreateAnnotationFunc &createFunc )
54 : mTypeName( typeName )
55 , mCreateFunc( createFunc )
56 {}
57
61 QString type() const { return mTypeName; }
62
66 QgsAnnotation *createAnnotation() const { return mCreateFunc ? mCreateFunc() : nullptr ; }
67
68 private:
69
70 QString mTypeName;
71 QgsCreateAnnotationFunc mCreateFunc = nullptr;
72
73 QgsAnnotationMetadata() = default;
74 friend class QMap< QString, QgsAnnotationMetadata >;
75
76};
77
83class CORE_EXPORT QgsAnnotationRegistry
84{
85
86 public:
87
92 QgsAnnotationRegistry()
93 {
94 addAnnotationType( QgsAnnotationMetadata( u"TextAnnotationItem"_s, QgsTextAnnotation::create ) );
95 addAnnotationType( QgsAnnotationMetadata( u"HtmlAnnotationItem"_s, QgsHtmlAnnotation::create ) );
96 addAnnotationType( QgsAnnotationMetadata( u"SVGAnnotationItem"_s, QgsSvgAnnotation::create ) );
97 }
98
104 bool addAnnotationType( const QgsAnnotationMetadata &metadata )
105 {
106 if ( mMetadata.contains( metadata.type() ) )
107 return false;
108
109 mMetadata.insert( metadata.type(), metadata );
110 return true;
111 }
112
117 QgsAnnotation *create( const QString &typeName ) const
118 {
119 if ( !mMetadata.contains( typeName ) )
120 return nullptr;
121
122 return mMetadata.value( typeName ).createAnnotation();
123 }
124
125 private:
126
127 QMap< QString, QgsAnnotationMetadata > mMetadata;
128
129};
130
132
133#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.