QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgslayoutitemguiregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemregistry.cpp
3  -------------------------
4  begin : June 2017
5  copyright : (C) 2017 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 "qgslayoutitemregistry.h"
20 #include "qgslayoutframe.h"
21 #include "qgslayoutmultiframe.h"
22 #include <QPainter>
23 
24 
26 {
27  return new QgsLayoutViewRectangularRubberBand( view );
28 }
29 
31 {
32  return nullptr;
33 }
34 
36 {
37  return nullptr;
38 }
39 
41 {
42 
43 }
44 
46  : QObject( parent )
47 {
48 }
49 
51 {
52  qDeleteAll( mMetadata );
53 }
54 
56 {
57  return mMetadata.value( metadataId );
58 }
59 
61 {
62  for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
63  {
64  if ( it.value()->type() == type )
65  return it.key();
66  }
67  return -1;
68 }
69 
71 {
72  if ( !metadata )
73  return false;
74 
75  const int id = mMetadata.count();
76  mMetadata[id] = metadata;
77  emit typeAdded( id );
78  return true;
79 }
80 
82 {
83  if ( mItemGroups.contains( group.id ) )
84  return false;
85 
86  mItemGroups.insert( group.id, group );
87  return true;
88 }
89 
91 {
92  return mItemGroups[ id ];
93 }
94 
96 {
97  if ( !mMetadata.contains( metadataId ) )
98  return nullptr;
99 
100  std::unique_ptr< QgsLayoutItem > item( mMetadata.value( metadataId )->createItem( layout ) );
101  if ( item )
102  return item.release();
103 
104  const int type = mMetadata.value( metadataId )->type();
105  return QgsApplication::layoutItemRegistry()->createItem( type, layout );
106 }
107 
108 void QgsLayoutItemGuiRegistry::newItemAddedToLayout( int metadataId, QgsLayoutItem *item, const QVariantMap &properties )
109 {
110  if ( !mMetadata.contains( metadataId ) )
111  return;
112 
113  if ( QgsLayoutItemGuiMetadata *metadata = dynamic_cast<QgsLayoutItemGuiMetadata *>( mMetadata.value( metadataId ) ) )
114  {
115  metadata->newItemAddedToLayout( item, properties );
116  }
117  else
118  {
119  mMetadata.value( metadataId )->newItemAddedToLayout( item );
120  }
121 }
122 
124 {
125  if ( !item )
126  return nullptr;
127 
128  int type = item->type();
130  {
131  QgsLayoutMultiFrame *multiFrame = qobject_cast< QgsLayoutFrame * >( item )->multiFrame();
132  if ( multiFrame )
133  type = multiFrame->type();
134  }
135  for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
136  {
137  if ( it.value()->type() == type )
138  return it.value()->createItemWidget( item );
139  }
140 
141  return nullptr;
142 }
143 
145 {
146  if ( !mMetadata.contains( metadataId ) )
147  return nullptr;
148 
149  return mMetadata[metadataId]->createRubberBand( view );
150 }
151 
152 QAbstractGraphicsShapeItem *QgsLayoutItemGuiRegistry::createNodeItemRubberBand( int metadataId, QgsLayoutView *view )
153 {
154  if ( !mMetadata.contains( metadataId ) )
155  return nullptr;
156 
157  return mMetadata[metadataId]->createNodeRubberBand( view );
158 }
159 
161 {
162  return mMetadata.keys();
163 }
164 
166 {
168 }
169 
171 {
172  if ( mAddedToLayoutFunc )
173  mAddedToLayoutFunc( item, QVariantMap() );
174 }
175 
176 void QgsLayoutItemGuiMetadata::newItemAddedToLayout( QgsLayoutItem *item, const QVariantMap &properties )
177 {
178  if ( mAddedToLayoutFunc )
179  mAddedToLayoutFunc( item, properties );
180 }
static QgsLayoutItemRegistry * layoutItemRegistry()
Returns the application's layout item registry, used for layout item types.
Stores GUI metadata about one layout item class.
virtual QgsLayoutViewRubberBand * createRubberBand(QgsLayoutView *view)
Creates a rubber band for use when creating layout items of this type.
virtual QAbstractGraphicsShapeItem * createNodeRubberBand(QgsLayoutView *view)
Creates a rubber band for use when creating layout node based items of this type.
virtual void newItemAddedToLayout(QgsLayoutItem *item)
Called when a newly created item of the associated type has been added to a layout.
virtual QgsLayoutItem * createItem(QgsLayout *layout)
Creates an instance of the corresponding item type.
A base class for property widgets for layout items.
Stores GUI metadata about a group of layout item classes.
QString id
Unique (untranslated) group ID string.
Convenience metadata class that uses static functions to handle layout item GUI behavior.
QgsLayoutItemAddedToLayoutFunc mAddedToLayoutFunc
QgsLayoutItemCreateFunc mCreateFunc
QgsLayoutItem * createItem(QgsLayout *layout) override
Creates an instance of the corresponding item type.
void newItemAddedToLayout(QgsLayoutItem *item) override
Called when a newly created item of the associated type has been added to a layout.
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) const
Creates a new instance of a layout item configuration widget for the specified item.
void newItemAddedToLayout(int metadataId, QgsLayoutItem *item, const QVariantMap &properties=QVariantMap())
Called when a newly created item of the associated metadata metadataId has been added to a layout.
bool addItemGroup(const QgsLayoutItemGuiGroup &group)
Registers a new item group with the registry.
const QgsLayoutItemGuiGroup & itemGroup(const QString &id)
Returns a reference to the item group with matching id.
int metadataIdForItemType(int type) const
Returns the GUI item metadata ID which corresponds to the specified layout item type.
QgsLayoutItemGuiRegistry(QObject *parent=nullptr)
Creates a new empty item GUI registry.
QList< int > itemMetadataIds() const
Returns a list of available item metadata ids handled by the registry.
bool addLayoutItemGuiMetadata(QgsLayoutItemAbstractGuiMetadata *metadata)
Registers the gui metadata for a new layout item type.
QgsLayoutViewRubberBand * createItemRubberBand(int metadataId, QgsLayoutView *view) const
Creates a new rubber band item for the specified item metadataId and destination view.
QgsLayoutItem * createItem(int metadataId, QgsLayout *layout) const
Creates a new instance of a layout item given the item metadata metadataId, target layout.
QgsLayoutItemAbstractGuiMetadata * itemMetadata(int metadataId) const
Returns the metadata for the specified item metadataId.
QAbstractGraphicsShapeItem * createNodeItemRubberBand(int metadataId, QgsLayoutView *view)
Creates a rubber band for the specified item metadataId and destination view.
void typeAdded(int metadataId)
Emitted whenever a new item type is added to the registry, with the specified metadataId.
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
QgsLayoutItem * createItem(int type, QgsLayout *layout) const
Creates a new instance of a layout item given the item type, and target layout.
Base class for graphical items within a QgsLayout.
int type() const override
Returns a unique graphics item type identifier.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
virtual int type() const =0
Returns unique multiframe type id.
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets.
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes,...
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51