QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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
19#include "qgsannotationitem.h"
20
23
24#include <QImageReader>
25
26//
27// QgsAnnotationItemAbstractGuiMetadata
28//
29
31{
32 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) );
33}
34
39
44
49
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" ),
185 QObject::tr( "Polygon Annotation" ),
186 QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolygon.svg" ) ),
188 {
189 QgsAnnotationPolygonItemWidget *widget = new QgsAnnotationPolygonItemWidget( nullptr );
190 widget->setItem( item );
191 return widget;
192 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
194 {
195 return new QgsCreatePolygonItemMapTool( canvas, cadDockWidget );
196 } ) );
197
198 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "linestring" ),
199 QObject::tr( "Line Annotation" ),
200 QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolyline.svg" ) ),
202 {
203 QgsAnnotationLineItemWidget *widget = new QgsAnnotationLineItemWidget( nullptr );
204 widget->setItem( item );
205 return widget;
206 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
208 {
209 return new QgsCreateLineItemMapTool( canvas, cadDockWidget );
210 } ) );
211
212 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "marker" ),
213 QObject::tr( "Marker Annotation" ),
214 QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMarker.svg" ) ),
216 {
217 QgsAnnotationMarkerItemWidget *widget = new QgsAnnotationMarkerItemWidget( nullptr );
218 widget->setItem( item );
219 return widget;
220 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
222 {
223 return new QgsCreateMarkerItemMapTool( canvas, cadDockWidget );
224 } ) );
225
226 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "pointtext" ),
227 QObject::tr( "Text Annotation at Point" ),
228 QgsApplication::getThemeIcon( QStringLiteral( "/mActionText.svg" ) ),
230 {
231 QgsAnnotationPointTextItemWidget *widget = new QgsAnnotationPointTextItemWidget( nullptr );
232 widget->setItem( item );
233 return widget;
234 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
236 {
237 return new QgsCreatePointTextItemMapTool( canvas, cadDockWidget );
238 } ) );
239
240 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "linetext" ),
241 QObject::tr( "Text Annotation along Line" ),
242 QgsApplication::getThemeIcon( QStringLiteral( "/mActionTextAlongLine.svg" ) ),
244 {
245 QgsAnnotationLineTextItemWidget *widget = new QgsAnnotationLineTextItemWidget( nullptr );
246 widget->setItem( item );
247 return widget;
248 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
250 {
251 return new QgsCreateLineTextItemMapTool( canvas, cadDockWidget );
252 } ) );
253
254 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "recttext" ),
255 QObject::tr( "Text Annotation in Rectangle" ),
256 QgsApplication::getThemeIcon( QStringLiteral( "/mActionTextInsideRect.svg" ) ),
258 {
259 QgsAnnotationRectangleTextItemWidget *widget = new QgsAnnotationRectangleTextItemWidget( nullptr );
260 widget->setItem( item );
261 return widget;
262 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
264 {
265 return new QgsCreateRectangleTextItemMapTool( canvas, cadDockWidget );
266 } ) );
267
268 addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "picture" ),
269 QObject::tr( "Picture" ),
270 QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddImage.svg" ) ),
272 {
273 QgsAnnotationPictureItemWidget *widget = new QgsAnnotationPictureItemWidget( nullptr );
274 widget->setItem( item );
275 return widget;
276 }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
278 {
279 return new QgsCreatePictureItemMapTool( canvas, cadDockWidget );
280 } ) );
281}
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.