QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsannotationitemguiregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsannotationitemguiregistry.cpp
3 --------------------------
4 begin : September 2021
5 copyright : (C) 2021 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#include "moc_qgsannotationitemguiregistry.cpp"
20#include "qgsannotationitem.h"
21
24
25#include <QImageReader>
26
27//
28// QgsAnnotationItemAbstractGuiMetadata
29//
30
32{
33 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) );
34}
35
40
45
50
54
55//
56// QgsAnnotationItemGuiMetadata
57//
58
63
68
73
79
84
85
86//
87// QgsAnnotationItemGuiRegistry
88//
89
91 : QObject( parent )
92{
93}
94
95
100
102{
103 return mMetadata.value( metadataId );
104}
105
107{
108 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
109 {
110 if ( it.value()->type() == type )
111 return it.key();
112 }
113 return -1;
114}
115
117{
118 if ( !metadata )
119 return false;
120
121 const int id = mMetadata.count();
122 mMetadata[id] = metadata;
123 emit typeAdded( id );
124 return true;
125}
126
128{
129 if ( mItemGroups.contains( group.id ) )
130 return false;
131
132 mItemGroups.insert( group.id, group );
133 return true;
134}
135
137{
138 return mItemGroups[id];
139}
140
142{
143 if ( !mMetadata.contains( metadataId ) )
144 return nullptr;
145
146 std::unique_ptr<QgsAnnotationItem> item( mMetadata.value( metadataId )->createItem() );
147 if ( item )
148 return item.release();
149
150 const QString type = mMetadata.value( metadataId )->type();
152}
153
155{
156 if ( !mMetadata.contains( metadataId ) )
157 return;
158
159 mMetadata.value( metadataId )->newItemAddedToLayer( item, layer );
160}
161
163{
164 if ( !item )
165 return nullptr;
166
167 const QString &type = item->type();
168 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
169 {
170 if ( it.value()->type() == type )
171 return it.value()->createItemWidget( item );
172 }
173
174 return nullptr;
175}
176
178{
179 return mMetadata.keys();
180}
181
183{
184 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "polygon" ), QObject::tr( "Polygon Annotation" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolygon.svg" ) ), [=]( QgsAnnotationItem *item ) -> QgsAnnotationItemBaseWidget * {
185 QgsAnnotationPolygonItemWidget *widget = new QgsAnnotationPolygonItemWidget( nullptr );
186 widget->setItem( item );
187 return widget; }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr, [=]( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreatePolygonItemMapTool( canvas, cadDockWidget ); } ) );
188
189 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "linestring" ), QObject::tr( "Line Annotation" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolyline.svg" ) ), [=]( QgsAnnotationItem *item ) -> QgsAnnotationItemBaseWidget * {
190 QgsAnnotationLineItemWidget *widget = new QgsAnnotationLineItemWidget( nullptr );
191 widget->setItem( item );
192 return widget; }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr, [=]( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreateLineItemMapTool( canvas, cadDockWidget ); } ) );
193
194 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "marker" ), QObject::tr( "Marker Annotation" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMarker.svg" ) ), [=]( QgsAnnotationItem *item ) -> QgsAnnotationItemBaseWidget * {
195 QgsAnnotationMarkerItemWidget *widget = new QgsAnnotationMarkerItemWidget( nullptr );
196 widget->setItem( item );
197 return widget; }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr, [=]( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreateMarkerItemMapTool( canvas, cadDockWidget ); } ) );
198
199 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "pointtext" ), QObject::tr( "Text Annotation at Point" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionText.svg" ) ), [=]( QgsAnnotationItem *item ) -> QgsAnnotationItemBaseWidget * {
200 QgsAnnotationPointTextItemWidget *widget = new QgsAnnotationPointTextItemWidget( nullptr );
201 widget->setItem( item );
202 return widget; }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr, [=]( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreatePointTextItemMapTool( canvas, cadDockWidget ); } ) );
203
204 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "linetext" ), QObject::tr( "Text Annotation along Line" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionTextAlongLine.svg" ) ), [=]( QgsAnnotationItem *item ) -> QgsAnnotationItemBaseWidget * {
205 QgsAnnotationLineTextItemWidget *widget = new QgsAnnotationLineTextItemWidget( nullptr );
206 widget->setItem( item );
207 return widget; }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr, [=]( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreateLineTextItemMapTool( canvas, cadDockWidget ); } ) );
208
209 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "recttext" ), QObject::tr( "Text Annotation in Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionTextInsideRect.svg" ) ), [=]( QgsAnnotationItem *item ) -> QgsAnnotationItemBaseWidget * {
210 QgsAnnotationRectangleTextItemWidget *widget = new QgsAnnotationRectangleTextItemWidget( nullptr );
211 widget->setItem( item );
212 return widget; }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr, [=]( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreateRectangleTextItemMapTool( canvas, cadDockWidget ); } ) );
213
214 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "picture" ), QObject::tr( "Picture Annotation" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddImage.svg" ) ), [=]( QgsAnnotationItem *item ) -> QgsAnnotationItemBaseWidget * {
215 QgsAnnotationPictureItemWidget *widget = new QgsAnnotationPictureItemWidget( nullptr );
216 widget->setItem( item );
217 return widget; }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr, [=]( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreatePictureItemMapTool( canvas, cadDockWidget ); } ) );
218}
QFlags< AnnotationItemGuiFlag > AnnotationItemGuiFlags
Annotation item GUI flags.
Definition qgis.h:2392
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
Stores GUI metadata about one annotation item class.
virtual QgsCreateAnnotationItemMapToolInterface * createMapTool(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget)
Creates a map tool for a creating a new item of this type.
virtual QgsAnnotationItemBaseWidget * createItemWidget(QgsAnnotationItem *item)
Creates a configuration widget for an item of this type.
virtual void newItemAddedToLayer(QgsAnnotationItem *item, QgsAnnotationLayer *layer)
Called when a newly created item of the associated type has been added to a layer.
virtual QIcon creationIcon() const
Returns an icon representing creation of the annotation item type.
virtual QgsAnnotationItem * createItem()
Creates an instance of the corresponding item type.
A base class for property widgets for annotation items.
Stores GUI metadata about a group of annotation item classes.
QString id
Unique (untranslated) group ID string.
Convenience metadata class that uses static functions to handle annotation item GUI behavior.
QgsAnnotationItem * createItem() override
Creates an instance of the corresponding item type.
QgsAnnotationItemAddedToLayerFunc mAddedToLayerFunc
QgsAnnotationItemBaseWidget * createItemWidget(QgsAnnotationItem *item) override
Creates a configuration widget for an item of this type.
QgsAnnotationItemCreateFunc mCreateFunc
QIcon creationIcon() const override
Returns an icon representing creation of the annotation item type.
QgsAnnotationItemWidgetFunc mWidgetFunc
void newItemAddedToLayer(QgsAnnotationItem *item, QgsAnnotationLayer *layer) override
Called when a newly created item of the associated type has been added to a layer.
QgsCreateAnnotationItemMapToolInterface * createMapTool(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget) override
Creates a map tool for a creating a new item of this type.
QgsCreateAnnotationItemMapToolFunc mCreateMapToolFunc
QgsAnnotationItemBaseWidget * createItemWidget(QgsAnnotationItem *item) const
Creates a new instance of an annotation item configuration widget for the specified item.
bool addItemGroup(const QgsAnnotationItemGuiGroup &group)
Registers a new item group with the registry.
QgsAnnotationItemAbstractGuiMetadata * itemMetadata(int metadataId) const
Returns the metadata for the specified item metadataId.
bool addAnnotationItemGuiMetadata(QgsAnnotationItemAbstractGuiMetadata *metadata)
Registers the gui metadata for a new annotation item type.
void addDefaultItems()
Populates the registry with default items.
QList< int > itemMetadataIds() const
Returns a list of available item metadata ids handled by the registry.
int metadataIdForItemType(const QString &type) const
Returns the GUI item metadata ID which corresponds to the specified annotation item type.
const QgsAnnotationItemGuiGroup & itemGroup(const QString &id)
Returns a reference to the item group with matching id.
void typeAdded(int metadataId)
Emitted whenever a new item type is added to the registry, with the specified metadataId.
QgsAnnotationItem * createItem(int metadataId) const
Creates a new instance of an annotation item given the item metadata metadataId.
void newItemAddedToLayer(int metadataId, QgsAnnotationItem *item, QgsAnnotationLayer *layer)
Called when a newly created item of the associated metadata metadataId has been added to a layer.
QgsAnnotationItemGuiRegistry(QObject *parent=nullptr)
Creates a new empty item GUI registry.
QgsAnnotationItem * createItem(const QString &type) const
Creates a new instance of a annotation item given the item type.
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
virtual QString type() const =0
Returns a unique (untranslated) string identifying the type of item.
Represents a map layer containing a set of georeferenced annotations, e.g.
static QgsAnnotationItemRegistry * annotationItemRegistry()
Returns the application's annotation item registry, used for annotation item types.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
An interface for map tools which create annotation items.
A map tool to digitize line items.
A map tool to digitize line text items.
A map tool to digitize marker items.
A map tool to digitize picture items.
A map tool to digitize point text items.
A map tool to digitize polygon items.
A map tool to digitize rectangle text items.
Map canvas is a class for displaying all GIS data types on a canvas.