QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsAnnotationItemBaseWidget
A base class for property widgets for annotation items.
Definition: qgsannotationitemwidget.h:35
QgsAnnotationItemGuiMetadata::mCreateFunc
QgsAnnotationItemCreateFunc mCreateFunc
Definition: qgsannotationitemguiregistry.h:240
QgsAnnotationItemGuiRegistry::metadataIdForItemType
int metadataIdForItemType(const QString &type) const
Returns the GUI item metadata ID which corresponds to the specified annotation item type.
Definition: qgsannotationitemguiregistry.cpp:103
QgsAnnotationItemAbstractGuiMetadata::createMapTool
virtual QgsCreateAnnotationItemMapToolInterface * createMapTool(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget)
Creates a map tool for a creating a new item of this type.
Definition: qgsannotationitemguiregistry.cpp:37
QgsCreateAnnotationItemMapToolInterface
An interface for map tools which create annotation items.
Definition: qgscreateannotationitemmaptool.h:102
QgsAnnotationItemGuiGroup::id
QString id
Unique (untranslated) group ID string.
Definition: qgsannotationitemguiregistry.h:276
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
qgsannotationitemguiregistry.h
QgsAnnotationItemGuiMetadata::mWidgetFunc
QgsAnnotationItemWidgetFunc mWidgetFunc
Definition: qgsannotationitemguiregistry.h:239
QgsAnnotationItemGuiMetadata::createMapTool
QgsCreateAnnotationItemMapToolInterface * createMapTool(QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget) override
Creates a map tool for a creating a new item of this type.
Definition: qgsannotationitemguiregistry.cpp:77
QgsAnnotationItemGuiMetadata::mIcon
QIcon mIcon
Definition: qgsannotationitemguiregistry.h:238
QgsAnnotationItemGuiMetadata::mAddedToLayerFunc
QgsAnnotationItemAddedToLayerFunc mAddedToLayerFunc
Definition: qgsannotationitemguiregistry.h:242
QgsAnnotationItemGuiRegistry::itemGroup
const QgsAnnotationItemGuiGroup & itemGroup(const QString &id)
Returns a reference to the item group with matching id.
Definition: qgsannotationitemguiregistry.cpp:133
QgsAnnotationItemAbstractGuiMetadata::createItemWidget
virtual QgsAnnotationItemBaseWidget * createItemWidget(QgsAnnotationItem *item)
Creates a configuration widget for an item of this type.
Definition: qgsannotationitemguiregistry.cpp:32
QgsAnnotationItemGuiMetadata::newItemAddedToLayer
void newItemAddedToLayer(QgsAnnotationItem *item, QgsAnnotationLayer *layer) override
Called when a newly created item of the associated type has been added to a layer.
Definition: qgsannotationitemguiregistry.cpp:71
QgsAnnotationItemGuiMetadata::createItemWidget
QgsAnnotationItemBaseWidget * createItemWidget(QgsAnnotationItem *item) override
Creates a configuration widget for an item of this type.
Definition: qgsannotationitemguiregistry.cpp:61
QgsAnnotationItemGuiMetadata::createItem
QgsAnnotationItem * createItem() override
Creates an instance of the corresponding item type.
Definition: qgsannotationitemguiregistry.cpp:66
QgsAnnotationItemGuiRegistry::itemMetadataIds
QList< int > itemMetadataIds() const
Returns a list of available item metadata ids handled by the registry.
Definition: qgsannotationitemguiregistry.cpp:174
QgsAnnotationItemGuiMetadata
Convenience metadata class that uses static functions to handle annotation item GUI behavior.
Definition: qgsannotationitemguiregistry.h:156
QgsAnnotationItemGuiRegistry::addAnnotationItemGuiMetadata
bool addAnnotationItemGuiMetadata(QgsAnnotationItemAbstractGuiMetadata *metadata)
Registers the gui metadata for a new annotation item type.
Definition: qgsannotationitemguiregistry.cpp:113
qgsannotationitemwidget_impl.h
QgsAnnotationItemGuiRegistry::~QgsAnnotationItemGuiRegistry
~QgsAnnotationItemGuiRegistry() override
Definition: qgsannotationitemguiregistry.cpp:93
QgsAnnotationItemRegistry::createItem
QgsAnnotationItem * createItem(const QString &type) const
Creates a new instance of a annotation item given the item type.
Definition: qgsannotationitemregistry.cpp:66
QgsAdvancedDigitizingDockWidget
The QgsAdvancedDigitizingDockWidget class is a dockable widget used to handle the CAD tools on top of...
Definition: qgsadvanceddigitizingdockwidget.h:50
QgsAnnotationItemGuiRegistry::createItem
QgsAnnotationItem * createItem(int metadataId) const
Creates a new instance of an annotation item given the item metadata metadataId.
Definition: qgsannotationitemguiregistry.cpp:138
QgsAnnotationItemGuiRegistry::typeAdded
void typeAdded(int metadataId)
Emitted whenever a new item type is added to the registry, with the specified metadataId.
QgsAnnotationItemGuiRegistry::itemMetadata
QgsAnnotationItemAbstractGuiMetadata * itemMetadata(int metadataId) const
Returns the metadata for the specified item metadataId.
Definition: qgsannotationitemguiregistry.cpp:98
qgscreateannotationitemmaptool_impl.h
QgsAnnotationItemGuiRegistry::newItemAddedToLayer
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.
Definition: qgsannotationitemguiregistry.cpp:151
QgsAnnotationItemAbstractGuiMetadata
Stores GUI metadata about one annotation item class.
Definition: qgsannotationitemguiregistry.h:45
QgsAnnotationLayer
Represents a map layer containing a set of georeferenced annotations, e.g. markers,...
Definition: qgsannotationlayer.h:46
QgsAnnotationItemGuiRegistry::QgsAnnotationItemGuiRegistry
QgsAnnotationItemGuiRegistry(QObject *parent=nullptr)
Creates a new empty item GUI registry.
Definition: qgsannotationitemguiregistry.cpp:87
QgsAnnotationItemAbstractGuiMetadata::creationIcon
virtual QIcon creationIcon() const
Returns an icon representing creation of the annotation item type.
Definition: qgsannotationitemguiregistry.cpp:27
qgsannotationitemregistry.h
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
qgsannotationitem.h
QgsAnnotationItemGuiMetadata::mCreateMapToolFunc
QgsCreateAnnotationItemMapToolFunc mCreateMapToolFunc
Definition: qgsannotationitemguiregistry.h:241
QgsAnnotationItemGuiMetadata::creationIcon
QIcon creationIcon() const override
Returns an icon representing creation of the annotation item type.
Definition: qgsannotationitemguiregistry.cpp:56
QgsAnnotationItemGuiRegistry::createItemWidget
QgsAnnotationItemBaseWidget * createItemWidget(QgsAnnotationItem *item) const
Creates a new instance of an annotation item configuration widget for the specified item.
Definition: qgsannotationitemguiregistry.cpp:159
QgsAnnotationItemAbstractGuiMetadata::newItemAddedToLayer
virtual void newItemAddedToLayer(QgsAnnotationItem *item, QgsAnnotationLayer *layer)
Called when a newly created item of the associated type has been added to a layer.
Definition: qgsannotationitemguiregistry.cpp:47
QgsAnnotationItemGuiRegistry::addItemGroup
bool addItemGroup(const QgsAnnotationItemGuiGroup &group)
Registers a new item group with the registry.
Definition: qgsannotationitemguiregistry.cpp:124
QgsAnnotationItemGuiGroup
Stores GUI metadata about a group of annotation item classes.
Definition: qgsannotationitemguiregistry.h:260
QgsAnnotationItemGuiRegistry::addDefaultItems
void addDefaultItems()
Populates the registry with default items.
Definition: qgsannotationitemguiregistry.cpp:179
QgsAnnotationItem
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
Definition: qgsannotationitem.h:42
QgsApplication::annotationItemRegistry
static QgsAnnotationItemRegistry * annotationItemRegistry()
Returns the application's annotation item registry, used for annotation item types.
Definition: qgsapplication.cpp:2400
QgsAnnotationItem::type
virtual QString type() const =0
Returns a unique (untranslated) string identifying the type of item.
QgsAnnotationItemAbstractGuiMetadata::createItem
virtual QgsAnnotationItem * createItem()
Creates an instance of the corresponding item type.
Definition: qgsannotationitemguiregistry.cpp:42