QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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 "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
34typedef std::function < QgsAnnotation*() > QgsCreateAnnotationFunc;
35
41class CORE_EXPORT QgsAnnotationMetadata
42{
43 public:
44
49 QgsAnnotationMetadata( const QString &typeName, const QgsCreateAnnotationFunc &createFunc )
50 : mTypeName( typeName )
51 , mCreateFunc( createFunc )
52 {}
53
57 QString type() const { return mTypeName; }
58
62 QgsAnnotation *createAnnotation() const { return mCreateFunc ? mCreateFunc() : nullptr ; }
63
64 private:
65
66 QString mTypeName;
67 QgsCreateAnnotationFunc mCreateFunc = nullptr;
68
69 QgsAnnotationMetadata() = default;
70 friend class QMap< QString, QgsAnnotationMetadata >;
71
72};
73
79class CORE_EXPORT QgsAnnotationRegistry
80{
81
82 public:
83
88 QgsAnnotationRegistry()
89 {
90 addAnnotationType( QgsAnnotationMetadata( QStringLiteral( "TextAnnotationItem" ), QgsTextAnnotation::create ) );
91 addAnnotationType( QgsAnnotationMetadata( QStringLiteral( "HtmlAnnotationItem" ), QgsHtmlAnnotation::create ) );
92 addAnnotationType( QgsAnnotationMetadata( QStringLiteral( "SVGAnnotationItem" ), 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
123 QMap< QString, QgsAnnotationMetadata > mMetadata;
124
125};
126
128
129#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.
const QString & typeName