QGIS API Documentation 4.3.0-Master (0cfde48c85b)
Loading...
Searching...
No Matches
qgsannotationitemregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsannotationitemregistry.cpp
3 -------------------------
4 begin : October 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgsannotationitem.h"
27
28#include <QDomElement>
29#include <QString>
30
31#include "moc_qgsannotationitemregistry.cpp"
32
33using namespace Qt::StringLiterals;
34
39
41 : QObject( parent )
42{}
43
45{
46 qDeleteAll( mMetadata );
47}
48
50{
51 if ( !mMetadata.isEmpty() )
52 return false;
53
54 mMetadata.insert( u"marker"_s, new QgsAnnotationItemMetadata( u"marker"_s, QObject::tr( "Marker" ), QObject::tr( "Markers" ), QgsAnnotationMarkerItem::create ) );
55 mMetadata.insert( u"linestring"_s, new QgsAnnotationItemMetadata( u"linestring"_s, QObject::tr( "Polyline" ), QObject::tr( "Polylines" ), QgsAnnotationLineItem::create ) );
56 mMetadata.insert( u"polygon"_s, new QgsAnnotationItemMetadata( u"polygon"_s, QObject::tr( "Polygon" ), QObject::tr( "Polygons" ), QgsAnnotationPolygonItem::create ) );
57 mMetadata.insert( u"pointtext"_s, new QgsAnnotationItemMetadata( u"pointtext"_s, QObject::tr( "Text at point" ), QObject::tr( "Text at points" ), QgsAnnotationPointTextItem::create ) );
58 mMetadata.insert( u"linetext"_s, new QgsAnnotationItemMetadata( u"linetext"_s, QObject::tr( "Text along line" ), QObject::tr( "Text along lines" ), QgsAnnotationLineTextItem::create ) );
59 mMetadata.insert( u"recttext"_s, new QgsAnnotationItemMetadata( u"recttext"_s, QObject::tr( "Text in rectangle" ), QObject::tr( "Text in rectangles" ), QgsAnnotationRectangleTextItem::create ) );
60 mMetadata.insert( u"picture"_s, new QgsAnnotationItemMetadata( u"picture"_s, QObject::tr( "Picture" ), QObject::tr( "Pictures" ), QgsAnnotationPictureItem::create ) );
61 return true;
62}
63
65{
66 return mMetadata.value( type );
67}
68
70{
71 if ( !metadata || mMetadata.contains( metadata->type() ) )
72 return false;
73
74 mMetadata[metadata->type()] = metadata;
75 emit typeAdded( metadata->type(), metadata->visibleName() );
76 return true;
77}
78
79std::unique_ptr<QgsAnnotationItem> QgsAnnotationItemRegistry::createItem( const QString &type ) const
80{
81 if ( !mMetadata.contains( type ) )
82 return nullptr;
83
84 return std::unique_ptr<QgsAnnotationItem>( mMetadata[type]->createItem() );
85}
86
87QMap<QString, QString> QgsAnnotationItemRegistry::itemTypes() const
88{
89 QMap<QString, QString> types;
90 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
91 {
92 types.insert( it.key(), it.value()->visibleName() );
93 }
94 return types;
95}
Stores metadata about one annotation item class.
QString type() const
Returns the unique item type string for the annotation item class.
QString visibleName() const
Returns a translated, user visible name for the annotation item class.
Convenience metadata class that uses static functions to create annotation items and their configurat...
QgsAnnotationItem * createItem() override
Creates a new, default, annotation item of this class.
QgsAnnotationItemCreateFunc mCreateFunc
QgsAnnotationItemAbstractMetadata * itemMetadata(const QString &type) const
Returns the metadata for the specified item type.
void typeAdded(const QString &type, const QString &name)
Emitted whenever a new item type is added to the registry, with the specified type and visible name.
bool populate()
Populates the registry with standard item types.
QgsAnnotationItemRegistry(QObject *parent=nullptr)
Creates a new empty item registry.
QMap< QString, QString > itemTypes() const
Returns a map of available item types to translated name.
std::unique_ptr< QgsAnnotationItem > createItem(const QString &type) const
Creates a new instance of a annotation item given the item type.
bool addItemType(QgsAnnotationItemAbstractMetadata *metadata)
Registers a new annotation item type.
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
static std::unique_ptr< QgsAnnotationLineItem > create()
Creates a new linestring annotation item.
static std::unique_ptr< QgsAnnotationLineTextItem > create()
Creates a new linestring annotation item.
static std::unique_ptr< QgsAnnotationMarkerItem > create()
Creates a new marker annotation item.
static std::unique_ptr< QgsAnnotationPictureItem > create()
Creates a new polygon annotation item.
static std::unique_ptr< QgsAnnotationPointTextItem > create()
Creates a new text at point annotation item.
static std::unique_ptr< QgsAnnotationPolygonItem > create()
Creates a new polygon annotation item.
static std::unique_ptr< QgsAnnotationRectangleTextItem > create()
Creates a new rectangle text annotation item.