QGIS API Documentation 3.99.0-Master (f78f5286a64)
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
18#include "moc_qgslayoutitemguiregistry.cpp"
21#include "qgslayoutframe.h"
22#include "qgslayoutmultiframe.h"
23#include <QPainter>
24
25
30
32{
33 return nullptr;
34}
35
40
44
48
54
55
57 : QObject( parent )
58{
59}
60
62{
63 qDeleteAll( mMetadata );
64}
65
67{
68 return mMetadata.value( metadataId );
69}
70
72{
73 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
74 {
75 if ( it.value()->type() == type )
76 return it.key();
77 }
78 return -1;
79}
80
82{
83 if ( !metadata )
84 return false;
85
86 const int id = mMetadata.count();
87 mMetadata[id] = metadata;
88 emit typeAdded( id );
89 return true;
90}
91
93{
94 int metadataId = metadataIdForItemType( typeId );
95 if ( !mMetadata.contains( metadataId ) )
96 return false;
97 mMetadata.remove( metadataId );
98 emit typeRemoved( metadataId );
99 return true;
100}
101
103{
104 if ( !metadata )
105 return false;
106 return removeLayoutItemGuiMetadata( metadata->type() );
107}
108
110{
111 if ( mItemGroups.contains( group.id ) )
112 return false;
113
114 mItemGroups.insert( group.id, group );
115 return true;
116}
117
119{
120 return mItemGroups[id];
121}
122
124{
125 if ( !mItemGroups.contains( id ) )
126 return false;
127
128 mItemGroups.remove( id );
129 emit groupRemoved( id );
130 return true;
131}
132
134{
135 if ( !mMetadata.contains( metadataId ) )
136 return nullptr;
137
138 std::unique_ptr<QgsLayoutItem> item( mMetadata.value( metadataId )->createItem( layout ) );
139 if ( item )
140 return item.release();
141
142 const int type = mMetadata.value( metadataId )->type();
143 return QgsApplication::layoutItemRegistry()->createItem( type, layout );
144}
145
146void QgsLayoutItemGuiRegistry::newItemAddedToLayout( int metadataId, QgsLayoutItem *item, const QVariantMap &properties )
147{
148 if ( !mMetadata.contains( metadataId ) )
149 return;
150
151 if ( QgsLayoutItemGuiMetadata *metadata = dynamic_cast<QgsLayoutItemGuiMetadata *>( mMetadata.value( metadataId ) ) )
152 {
153 metadata->newItemAddedToLayout( item, properties );
154 }
155 else
156 {
157 mMetadata.value( metadataId )->newItemAddedToLayout( item );
158 }
159}
160
162{
163 if ( !item )
164 return nullptr;
165
166 int type = item->type();
168 {
169 QgsLayoutMultiFrame *multiFrame = qobject_cast<QgsLayoutFrame *>( item )->multiFrame();
170 if ( multiFrame )
171 type = multiFrame->type();
172 }
173 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
174 {
175 if ( it.value()->type() == type )
176 return it.value()->createItemWidget( item );
177 }
178
179 return nullptr;
180}
181
183{
184 if ( !mMetadata.contains( metadataId ) )
185 return nullptr;
186
187 return mMetadata[metadataId]->createRubberBand( view );
188}
189
191{
192 if ( !mMetadata.contains( metadataId ) )
193 return nullptr;
194
195 return mMetadata[metadataId]->createNodeRubberBand( view );
196}
197
199{
200 return mMetadata.keys();
201}
202
207
209{
210 if ( mAddedToLayoutFunc )
211 mAddedToLayoutFunc( item, QVariantMap() );
212}
213
214void QgsLayoutItemGuiMetadata::newItemAddedToLayout( QgsLayoutItem *item, const QVariantMap &properties )
215{
216 if ( mAddedToLayoutFunc )
217 mAddedToLayoutFunc( item, properties );
218}
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:5890
static QgsLayoutItemRegistry * layoutItemRegistry()
Returns the application's layout item registry, used for layout item types.
Stores GUI metadata about one layout item class.
virtual QGraphicsItem * createNodeRubberBand(QgsLayoutView *view)
Creates a rubber band for use when creating layout node based items of this type.
virtual void handleDoubleClick(QgsLayoutItem *item, Qgis::MouseHandlesAction action)
Called when a layout item is double-clicked.
virtual QgsLayoutViewRubberBand * createRubberBand(QgsLayoutView *view)
Creates a rubber band for use when creating layout items of this type.
int type() const
Returns the unique item type code for the layout item class.
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
void handleDoubleClick(QgsLayoutItem *item, Qgis::MouseHandlesAction action) override
Called when a layout item is double-clicked.
QgsLayoutItemCreateFunc mCreateFunc
QgsLayoutItemDoubleClickedFunc mDoubleClickedFunc
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.
bool removeItemGroup(const QString &id)
Unregisters an item group from the registry.
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) const
Creates a new instance of a layout item configuration widget for the specified item.
bool removeLayoutItemGuiMetadata(int type)
Unregisters the GUI metadata for a layout item type.
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.
void typeRemoved(int metadataId)
Emitted whenever an item type is removed from the registry, with the specified metadataId.
void groupRemoved(QString groupId)
Emitted whenever an item group is removed from the registry.
QgsLayoutViewRubberBand * createItemRubberBand(int metadataId, QgsLayoutView *view) const
Creates a new rubber band item for the specified item metadataId and destination view.
QGraphicsItem * createNodeItemRubberBand(int metadataId, QgsLayoutView *view)
Creates a rubber band 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.
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.
A rectangular rubber band for use within QgsLayoutView widgets.
An abstract base class for temporary rubber band items in various shapes, for use within QgsLayoutVie...
A graphical widget to display and interact with QgsLayouts.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49