QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
19#include "qgsannotationitem.h"
23
24#include <QImageReader>
25#include <QString>
26
27#include "moc_qgsannotationitemguiregistry.cpp"
28
29using namespace Qt::StringLiterals;
30
31//
32// QgsAnnotationItemAbstractGuiMetadata
33//
34
36{
37 return QgsApplication::getThemeIcon( u"/mActionAddBasicRectangle.svg"_s );
38}
39
44
49
54
57
58//
59// QgsAnnotationItemGuiMetadata
60//
61
66
71
76
82
87
88
89//
90// QgsAnnotationItemGuiRegistry
91//
92
94 : QObject( parent )
95{}
96
97
102
104{
105 return mMetadata.value( metadataId );
106}
107
109{
110 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
111 {
112 if ( it.value()->type() == type )
113 return it.key();
114 }
115 return -1;
116}
117
119{
120 if ( !metadata )
121 return false;
122
123 const int id = mMetadata.count();
124 mMetadata[id] = metadata;
125 emit typeAdded( id );
126 return true;
127}
128
130{
131 if ( mItemGroups.contains( group.id ) )
132 return false;
133
134 mItemGroups.insert( group.id, group );
135 return true;
136}
137
139{
140 auto iter = mItemGroups.find( id );
141 if ( iter == mItemGroups.end() )
142 {
143 static QgsAnnotationItemGuiGroup invalidGroup;
144 return invalidGroup;
145 }
146 return *iter;
147}
148
150{
151 auto it = mMetadata.constFind( metadataId );
152 if ( it == mMetadata.constEnd() )
153 return nullptr;
154
155 std::unique_ptr<QgsAnnotationItem> item( it.value()->createItem() );
156 if ( item )
157 return item.release();
158
159 const QString type = it.value()->type();
161}
162
164{
165 auto it = mMetadata.constFind( metadataId );
166 if ( it == mMetadata.constEnd() )
167 return;
168
169 it.value()->newItemAddedToLayer( item, layer );
170}
171
173{
174 if ( !item )
175 return nullptr;
176
177 const QString &type = item->type();
178 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
179 {
180 if ( it.value()->type() == type )
181 return it.value()->createItemWidget( item );
182 }
183
184 return nullptr;
185}
186
188{
189 return mMetadata.keys();
190}
191
193{
195 u"polygon"_s,
196 QObject::tr( "Polygon Annotation" ),
197 QgsApplication::getThemeIcon( u"/mActionAddPolygon.svg"_s ),
199 QgsAnnotationPolygonItemWidget *widget = new QgsAnnotationPolygonItemWidget( nullptr );
200 widget->setItem( item );
201 return widget;
202 },
203 QString(),
205 nullptr,
206 []( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreatePolygonItemMapTool( canvas, cadDockWidget ); }
207 ) );
208
210 u"linestring"_s,
211 QObject::tr( "Line Annotation" ),
212 QgsApplication::getThemeIcon( u"/mActionAddPolyline.svg"_s ),
214 QgsAnnotationLineItemWidget *widget = new QgsAnnotationLineItemWidget( nullptr );
215 widget->setItem( item );
216 return widget;
217 },
218 QString(),
220 nullptr,
221 []( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreateLineItemMapTool( canvas, cadDockWidget ); }
222 ) );
223
225 u"marker"_s,
226 QObject::tr( "Marker Annotation" ),
227 QgsApplication::getThemeIcon( u"/mActionAddMarker.svg"_s ),
229 QgsAnnotationMarkerItemWidget *widget = new QgsAnnotationMarkerItemWidget( nullptr );
230 widget->setItem( item );
231 return widget;
232 },
233 QString(),
235 nullptr,
236 []( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreateMarkerItemMapTool( canvas, cadDockWidget ); }
237 ) );
238
240 u"pointtext"_s,
241 QObject::tr( "Text Annotation at Point" ),
242 QgsApplication::getThemeIcon( u"/mActionText.svg"_s ),
244 QgsAnnotationPointTextItemWidget *widget = new QgsAnnotationPointTextItemWidget( nullptr );
245 widget->setItem( item );
246 return widget;
247 },
248 QString(),
250 nullptr,
251 []( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreatePointTextItemMapTool( canvas, cadDockWidget ); }
252 ) );
253
255 u"linetext"_s,
256 QObject::tr( "Text Annotation along Line" ),
257 QgsApplication::getThemeIcon( u"/mActionTextAlongLine.svg"_s ),
259 QgsAnnotationLineTextItemWidget *widget = new QgsAnnotationLineTextItemWidget( nullptr );
260 widget->setItem( item );
261 return widget;
262 },
263 QString(),
265 nullptr,
266 []( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreateLineTextItemMapTool( canvas, cadDockWidget ); }
267 ) );
268
270 u"recttext"_s,
271 QObject::tr( "Text Annotation in Rectangle" ),
272 QgsApplication::getThemeIcon( u"/mActionTextInsideRect.svg"_s ),
274 QgsAnnotationRectangleTextItemWidget *widget = new QgsAnnotationRectangleTextItemWidget( nullptr );
275 widget->setItem( item );
276 return widget;
277 },
278 QString(),
280 nullptr,
281 []( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreateRectangleTextItemMapTool( canvas, cadDockWidget ); }
282 ) );
283
285 u"picture"_s,
286 QObject::tr( "Picture Annotation" ),
287 QgsApplication::getThemeIcon( u"/mActionAddImage.svg"_s ),
289 QgsAnnotationPictureItemWidget *widget = new QgsAnnotationPictureItemWidget( nullptr );
290 widget->setItem( item );
291 return widget;
292 },
293 QString(),
295 nullptr,
296 []( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget ) -> QgsCreateAnnotationItemMapToolInterface * { return new QgsCreatePictureItemMapTool( canvas, cadDockWidget ); }
297 ) );
298}
QFlags< AnnotationItemGuiFlag > AnnotationItemGuiFlags
Annotation item GUI flags.
Definition qgis.h:2586
A dockable widget used to handle the CAD tools on top of a selection of map tools.
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) const
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.