QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 // QgsAnnotationItemAbstractGuiMetadata
25 //
26 
28 {
29  return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) );
30 }
31 
33 {
34  return nullptr;
35 }
36 
38 {
39  return nullptr;
40 }
41 
43 {
44  return nullptr;
45 }
46 
48 {
49 
50 }
51 
52 //
53 // QgsAnnotationItemGuiMetadata
54 //
55 
57 {
59 }
60 
62 {
63  return mWidgetFunc ? mWidgetFunc( item ) : nullptr;
64 }
65 
67 {
69 }
70 
72 {
73  if ( mAddedToLayerFunc )
74  mAddedToLayerFunc( item, layer );
75 }
76 
78 {
79  return mCreateMapToolFunc ? mCreateMapToolFunc( canvas, cadDockWidget ) : nullptr;
80 }
81 
82 
83 //
84 // QgsAnnotationItemGuiRegistry
85 //
86 
88  : QObject( parent )
89 {
90 }
91 
92 
94 {
95  qDeleteAll( mMetadata );
96 }
97 
99 {
100  return mMetadata.value( metadataId );
101 }
102 
104 {
105  for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
106  {
107  if ( it.value()->type() == type )
108  return it.key();
109  }
110  return -1;
111 }
112 
114 {
115  if ( !metadata )
116  return false;
117 
118  const int id = mMetadata.count();
119  mMetadata[id] = metadata;
120  emit typeAdded( id );
121  return true;
122 }
123 
125 {
126  if ( mItemGroups.contains( group.id ) )
127  return false;
128 
129  mItemGroups.insert( group.id, group );
130  return true;
131 }
132 
134 {
135  return mItemGroups[ id ];
136 }
137 
139 {
140  if ( !mMetadata.contains( metadataId ) )
141  return nullptr;
142 
143  std::unique_ptr< QgsAnnotationItem > item( mMetadata.value( metadataId )->createItem() );
144  if ( item )
145  return item.release();
146 
147  const QString type = mMetadata.value( metadataId )->type();
149 }
150 
152 {
153  if ( !mMetadata.contains( metadataId ) )
154  return;
155 
156  mMetadata.value( metadataId )->newItemAddedToLayer( item, layer );
157 }
158 
160 {
161  if ( !item )
162  return nullptr;
163 
164  const QString &type = item->type();
165  for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
166  {
167  if ( it.value()->type() == type )
168  return it.value()->createItemWidget( item );
169  }
170 
171  return nullptr;
172 }
173 
175 {
176  return mMetadata.keys();
177 }
178 
180 {
181  addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "polygon" ),
182  QObject::tr( "Polygon" ),
183  QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolygon.svg" ) ),
185  {
186  QgsAnnotationPolygonItemWidget *widget = new QgsAnnotationPolygonItemWidget( nullptr );
187  widget->setItem( item );
188  return widget;
189  }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
191  {
192  return new QgsCreatePolygonItemMapTool( canvas, cadDockWidget );
193  } ) );
194 
195  addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "linestring" ),
196  QObject::tr( "Line" ),
197  QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolyline.svg" ) ),
199  {
200  QgsAnnotationLineItemWidget *widget = new QgsAnnotationLineItemWidget( nullptr );
201  widget->setItem( item );
202  return widget;
203  }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
205  {
206  return new QgsCreateLineItemMapTool( canvas, cadDockWidget );
207  } ) );
208 
209  addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "marker" ),
210  QObject::tr( "Marker" ),
211  QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMarker.svg" ) ),
213  {
214  QgsAnnotationMarkerItemWidget *widget = new QgsAnnotationMarkerItemWidget( nullptr );
215  widget->setItem( item );
216  return widget;
217  }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
219  {
220  return new QgsCreateMarkerItemMapTool( canvas, cadDockWidget );
221  } ) );
222 
223  addAnnotationItemGuiMetadata( new QgsAnnotationItemGuiMetadata( QStringLiteral( "pointtext" ),
224  QObject::tr( "Text at Point" ),
225  QgsApplication::getThemeIcon( QStringLiteral( "/mActionText.svg" ) ),
227  {
228  QgsAnnotationPointTextItemWidget *widget = new QgsAnnotationPointTextItemWidget( nullptr );
229  widget->setItem( item );
230  return widget;
231  }, QString(), Qgis::AnnotationItemGuiFlags(), nullptr,
233  {
234  return new QgsCreatePointTextItemMapTool( canvas, cadDockWidget );
235  } ) );
236 }
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.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89