QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
55
56//
57// QgsAnnotationItemGuiMetadata
58//
59
64
69
74
80
85
86
87//
88// QgsAnnotationItemGuiRegistry
89//
90
92 : QObject( parent )
93{
94}
95
96
101
103{
104 return mMetadata.value( metadataId );
105}
106
108{
109 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
110 {
111 if ( it.value()->type() == type )
112 return it.key();
113 }
114 return -1;
115}
116
118{
119 if ( !metadata )
120 return false;
121
122 const int id = mMetadata.count();
123 mMetadata[id] = metadata;
124 emit typeAdded( id );
125 return true;
126}
127
129{
130 if ( mItemGroups.contains( group.id ) )
131 return false;
132
133 mItemGroups.insert( group.id, group );
134 return true;
135}
136
138{
139 return mItemGroups[ id ];
140}
141
143{
144 if ( !mMetadata.contains( metadataId ) )
145 return nullptr;
146
147 std::unique_ptr< QgsAnnotationItem > item( mMetadata.value( metadataId )->createItem() );
148 if ( item )
149 return item.release();
150
151 const QString type = mMetadata.value( metadataId )->type();
153}
154
156{
157 if ( !mMetadata.contains( metadataId ) )
158 return;
159
160 mMetadata.value( metadataId )->newItemAddedToLayer( item, layer );
161}
162
164{
165 if ( !item )
166 return nullptr;
167
168 const QString &type = item->type();
169 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
170 {
171 if ( it.value()->type() == type )
172 return it.value()->createItemWidget( item );
173 }
174
175 return nullptr;
176}
177
179{
180 return mMetadata.keys();
181}
182
184{
185 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "polygon" ),
186 QObject::tr( "Polygon Annotation" ),
187 QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolygon.svg" ) ),
189 {
190 QgsAnnotationPolygonItemWidget *widget = new QgsAnnotationPolygonItemWidget( nullptr );
191 widget->setItem( item );
192 return widget;
193 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
195 {
196 return new QgsCreatePolygonItemMapTool( canvas, cadDockWidget );
197 } ) );
198
199 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "linestring" ),
200 QObject::tr( "Line Annotation" ),
201 QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolyline.svg" ) ),
203 {
204 QgsAnnotationLineItemWidget *widget = new QgsAnnotationLineItemWidget( nullptr );
205 widget->setItem( item );
206 return widget;
207 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
209 {
210 return new QgsCreateLineItemMapTool( canvas, cadDockWidget );
211 } ) );
212
213 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "marker" ),
214 QObject::tr( "Marker Annotation" ),
215 QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMarker.svg" ) ),
217 {
218 QgsAnnotationMarkerItemWidget *widget = new QgsAnnotationMarkerItemWidget( nullptr );
219 widget->setItem( item );
220 return widget;
221 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
223 {
224 return new QgsCreateMarkerItemMapTool( canvas, cadDockWidget );
225 } ) );
226
227 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "pointtext" ),
228 QObject::tr( "Text Annotation at Point" ),
229 QgsApplication::getThemeIcon( QStringLiteral( "/mActionText.svg" ) ),
231 {
232 QgsAnnotationPointTextItemWidget *widget = new QgsAnnotationPointTextItemWidget( nullptr );
233 widget->setItem( item );
234 return widget;
235 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
237 {
238 return new QgsCreatePointTextItemMapTool( canvas, cadDockWidget );
239 } ) );
240
241 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "linetext" ),
242 QObject::tr( "Text Annotation along Line" ),
243 QgsApplication::getThemeIcon( QStringLiteral( "/mActionTextAlongLine.svg" ) ),
245 {
246 QgsAnnotationLineTextItemWidget *widget = new QgsAnnotationLineTextItemWidget( nullptr );
247 widget->setItem( item );
248 return widget;
249 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
251 {
252 return new QgsCreateLineTextItemMapTool( canvas, cadDockWidget );
253 } ) );
254
255 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "recttext" ),
256 QObject::tr( "Text Annotation in Rectangle" ),
257 QgsApplication::getThemeIcon( QStringLiteral( "/mActionTextInsideRect.svg" ) ),
259 {
260 QgsAnnotationRectangleTextItemWidget *widget = new QgsAnnotationRectangleTextItemWidget( nullptr );
261 widget->setItem( item );
262 return widget;
263 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
265 {
266 return new QgsCreateRectangleTextItemMapTool( canvas, cadDockWidget );
267 } ) );
268
269 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "picture" ),
270 QObject::tr( "Picture" ),
271 QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddImage.svg" ) ),
273 {
274 QgsAnnotationPictureItemWidget *widget = new QgsAnnotationPictureItemWidget( nullptr );
275 widget->setItem( item );
276 return widget;
277 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
279 {
280 return new QgsCreatePictureItemMapTool( canvas, cadDockWidget );
281 } ) );
282}
QFlags< AnnotationItemGuiFlag > AnnotationItemGuiFlags
Annotation item GUI flags.
Definition qgis.h:2329
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.