QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsLayoutItemGuiMetadata::mAddedToLayoutFunc
QgsLayoutItemAddedToLayoutFunc mAddedToLayoutFunc
Definition: qgslayoutitemguiregistry.h:298
QgsLayoutItemGuiRegistry::createItemWidget
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) const
Creates a new instance of a layout item configuration widget for the specified item.
Definition: qgslayoutitemguiregistry.cpp:123
QgsLayoutItemGuiRegistry::itemMetadata
QgsLayoutItemAbstractGuiMetadata * itemMetadata(int metadataId) const
Returns the metadata for the specified item metadataId.
Definition: qgslayoutitemguiregistry.cpp:55
QgsLayoutItemGuiGroup
Stores GUI metadata about a group of layout item classes.
Definition: qgslayoutitemguiregistry.h:316
QgsLayoutItemGuiRegistry::createItemRubberBand
QgsLayoutViewRubberBand * createItemRubberBand(int metadataId, QgsLayoutView *view) const
Creates a new rubber band item for the specified item metadataId and destination view.
Definition: qgslayoutitemguiregistry.cpp:144
QgsLayoutItemAbstractGuiMetadata::newItemAddedToLayout
virtual void newItemAddedToLayout(QgsLayoutItem *item)
Called when a newly created item of the associated type has been added to a layout.
Definition: qgslayoutitemguiregistry.cpp:40
qgslayoutmultiframe.h
QgsLayoutItemRegistry::createItem
QgsLayoutItem * createItem(int type, QgsLayout *layout) const
Creates a new instance of a layout item given the item type, and target layout.
Definition: qgslayoutitemregistry.cpp:123
QgsLayoutViewRectangularRubberBand
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets.
Definition: qgslayoutviewrubberband.h:159
QgsLayoutItemGuiRegistry::~QgsLayoutItemGuiRegistry
~QgsLayoutItemGuiRegistry() override
Definition: qgslayoutitemguiregistry.cpp:50
QgsLayoutItemGuiRegistry::newItemAddedToLayout
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.
Definition: qgslayoutitemguiregistry.cpp:108
QgsLayoutMultiFrame
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Definition: qgslayoutmultiframe.h:48
QgsLayoutItemGuiRegistry::QgsLayoutItemGuiRegistry
QgsLayoutItemGuiRegistry(QObject *parent=nullptr)
Creates a new empty item GUI registry.
Definition: qgslayoutitemguiregistry.cpp:45
QgsLayoutItem::type
int type() const override
Returns a unique graphics item type identifier.
Definition: qgslayoutitem.cpp:124
qgslayoutframe.h
QgsLayoutItemGuiRegistry::typeAdded
void typeAdded(int metadataId)
Emitted whenever a new item type is added to the registry, with the specified metadataId.
qgslayoutviewrubberband.h
QgsLayoutMultiFrame::type
virtual int type() const =0
Returns unique multiframe type id.
QgsLayoutItemGuiGroup::id
QString id
Unique (untranslated) group ID string.
Definition: qgslayoutitemguiregistry.h:332
qgslayoutitemguiregistry.h
QgsLayoutItemGuiRegistry::metadataIdForItemType
int metadataIdForItemType(int type) const
Returns the GUI item metadata ID which corresponds to the specified layout item type.
Definition: qgslayoutitemguiregistry.cpp:60
QgsLayoutItemGuiRegistry::addItemGroup
bool addItemGroup(const QgsLayoutItemGuiGroup &group)
Registers a new item group with the registry.
Definition: qgslayoutitemguiregistry.cpp:81
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsLayoutItemGuiMetadata
Convenience metadata class that uses static functions to handle layout item GUI behavior.
Definition: qgslayoutitemguiregistry.h:183
QgsLayoutItemGuiRegistry::createItem
QgsLayoutItem * createItem(int metadataId, QgsLayout *layout) const
Creates a new instance of a layout item given the item metadata metadataId, target layout.
Definition: qgslayoutitemguiregistry.cpp:95
QgsLayoutItemGuiRegistry::createNodeItemRubberBand
QAbstractGraphicsShapeItem * createNodeItemRubberBand(int metadataId, QgsLayoutView *view)
Creates a rubber band for the specified item metadataId and destination view.
Definition: qgslayoutitemguiregistry.cpp:152
QgsLayoutItemGuiMetadata::mCreateFunc
QgsLayoutItemCreateFunc mCreateFunc
Definition: qgslayoutitemguiregistry.h:297
QgsLayoutItemBaseWidget
A base class for property widgets for layout items. All layout item widgets should inherit from this ...
Definition: qgslayoutitemwidget.h:122
QgsLayoutItemAbstractGuiMetadata::createRubberBand
virtual QgsLayoutViewRubberBand * createRubberBand(QgsLayoutView *view)
Creates a rubber band for use when creating layout items of this type.
Definition: qgslayoutitemguiregistry.cpp:25
QgsLayoutItemGuiRegistry::itemGroup
const QgsLayoutItemGuiGroup & itemGroup(const QString &id)
Returns a reference to the item group with matching id.
Definition: qgslayoutitemguiregistry.cpp:90
QgsLayoutItemAbstractGuiMetadata::createNodeRubberBand
virtual QAbstractGraphicsShapeItem * createNodeRubberBand(QgsLayoutView *view)
Creates a rubber band for use when creating layout node based items of this type.
Definition: qgslayoutitemguiregistry.cpp:30
QgsApplication::layoutItemRegistry
static QgsLayoutItemRegistry * layoutItemRegistry()
Returns the application's layout item registry, used for layout item types.
Definition: qgsapplication.cpp:2395
QgsLayoutItemGuiRegistry::addLayoutItemGuiMetadata
bool addLayoutItemGuiMetadata(QgsLayoutItemAbstractGuiMetadata *metadata)
Registers the gui metadata for a new layout item type.
Definition: qgslayoutitemguiregistry.cpp:70
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutItemGuiRegistry::itemMetadataIds
QList< int > itemMetadataIds() const
Returns a list of available item metadata ids handled by the registry.
Definition: qgslayoutitemguiregistry.cpp:160
QgsLayoutItemGuiMetadata::createItem
QgsLayoutItem * createItem(QgsLayout *layout) override
Creates an instance of the corresponding item type.
Definition: qgslayoutitemguiregistry.cpp:165
QgsLayoutItemAbstractGuiMetadata
Stores GUI metadata about one layout item class.
Definition: qgslayoutitemguiregistry.h:46
QgsLayoutItemAbstractGuiMetadata::createItem
virtual QgsLayoutItem * createItem(QgsLayout *layout)
Creates an instance of the corresponding item type.
Definition: qgslayoutitemguiregistry.cpp:35
QgsLayoutItemGuiMetadata::newItemAddedToLayout
void newItemAddedToLayout(QgsLayoutItem *item) override
Called when a newly created item of the associated type has been added to a layout.
Definition: qgslayoutitemguiregistry.cpp:170
QgsLayoutView
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:49
QgsLayoutViewRubberBand
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes,...
Definition: qgslayoutviewrubberband.h:37
QgsLayoutItemRegistry::LayoutFrame
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
Definition: qgslayoutitemregistry.h:355
qgslayoutitemregistry.h