QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
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 QString mTypeName;
70 QgsCreateAnnotationFunc mCreateFunc = nullptr;
71
72 QgsAnnotationMetadata() = default;
73 friend class QMap< QString, QgsAnnotationMetadata >;
74};
75
81class CORE_EXPORT QgsAnnotationRegistry
82{
83 public:
88 QgsAnnotationRegistry()
89 {
90 addAnnotationType( QgsAnnotationMetadata( u"TextAnnotationItem"_s, QgsTextAnnotation::create ) );
91 addAnnotationType( QgsAnnotationMetadata( u"HtmlAnnotationItem"_s, QgsHtmlAnnotation::create ) );
92 addAnnotationType( QgsAnnotationMetadata( u"SVGAnnotationItem"_s, QgsSvgAnnotation::create ) );
93 }
94
100 bool addAnnotationType( const QgsAnnotationMetadata &metadata )
101 {
102 if ( mMetadata.contains( metadata.type() ) )
103 return false;
104
105 mMetadata.insert( metadata.type(), metadata );
106 return true;
107 }
108
113 QgsAnnotation *create( const QString &typeName ) const
114 {
115 if ( !mMetadata.contains( typeName ) )
116 return nullptr;
117
118 return mMetadata.value( typeName ).createAnnotation();
119 }
120
121 private:
122 QMap< QString, QgsAnnotationMetadata > mMetadata;
123};
124
126
127#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.