QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
19#include "qgslayoutframe.h"
21#include "qgslayoutmultiframe.h"
23
24#include <QPainter>
25
26#include "moc_qgslayoutitemguiregistry.cpp"
27
32
34{
35 return nullptr;
36}
37
42
46
50
56
57
59 : QObject( parent )
60{
61}
62
64{
65 qDeleteAll( mMetadata );
66}
67
69{
70 return mMetadata.value( metadataId );
71}
72
74{
75 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
76 {
77 if ( it.value()->type() == type )
78 return it.key();
79 }
80 return -1;
81}
82
84{
85 if ( !metadata )
86 return false;
87
88 const int id = mMetadata.count();
89 mMetadata[id] = metadata;
90 emit typeAdded( id );
91 return true;
92}
93
95{
96 int metadataId = metadataIdForItemType( typeId );
97 auto it = mMetadata.find( metadataId );
98 if ( it == mMetadata.end() )
99 return false;
100 mMetadata.erase( it );
101 emit typeRemoved( metadataId );
102 return true;
103}
104
106{
107 if ( !metadata )
108 return false;
109 return removeLayoutItemGuiMetadata( metadata->type() );
110}
111
113{
114 if ( mItemGroups.contains( group.id ) )
115 return false;
116
117 mItemGroups.insert( group.id, group );
118 return true;
119}
120
122{
123 return mItemGroups[id];
124}
125
127{
128 auto it = mItemGroups.find( id );
129 if ( it == mItemGroups.end() )
130 return false;
131
132 mItemGroups.erase( it );
133 emit groupRemoved( id );
134 return true;
135}
136
138{
139 auto it = mMetadata.constFind( metadataId );
140 if ( it == mMetadata.constEnd() )
141 return nullptr;
142
143 std::unique_ptr<QgsLayoutItem> item( it.value()->createItem( layout ) );
144 if ( item )
145 return item.release();
146
147 const int type = it.value()->type();
148 return QgsApplication::layoutItemRegistry()->createItem( type, layout );
149}
150
151void QgsLayoutItemGuiRegistry::newItemAddedToLayout( int metadataId, QgsLayoutItem *item, const QVariantMap &properties )
152{
153 auto it = mMetadata.constFind( metadataId );
154 if ( it == mMetadata.constEnd() )
155 return;
156
157 if ( QgsLayoutItemGuiMetadata *metadata = dynamic_cast<QgsLayoutItemGuiMetadata *>( it.value() ) )
158 {
159 metadata->newItemAddedToLayout( item, properties );
160 }
161 else
162 {
163 it.value()->newItemAddedToLayout( item );
164 }
165}
166
168{
169 if ( !item )
170 return nullptr;
171
172 int type = item->type();
174 {
175 QgsLayoutMultiFrame *multiFrame = qobject_cast<QgsLayoutFrame *>( item )->multiFrame();
176 if ( multiFrame )
177 type = multiFrame->type();
178 }
179 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
180 {
181 if ( it.value()->type() == type )
182 return it.value()->createItemWidget( item );
183 }
184
185 return nullptr;
186}
187
189{
190 auto it = mMetadata.constFind( metadataId );
191 if ( it == mMetadata.constEnd() )
192 return nullptr;
193
194 return it.value()->createRubberBand( view );
195}
196
198{
199 auto it = mMetadata.constFind( metadataId );
200 if ( it == mMetadata.constEnd() )
201 return nullptr;
202
203 return it.value()->createNodeRubberBand( view );
204}
205
207{
208 return mMetadata.keys();
209}
210
215
217{
218 if ( mAddedToLayoutFunc )
219 mAddedToLayoutFunc( item, QVariantMap() );
220}
221
222void QgsLayoutItemGuiMetadata::newItemAddedToLayout( QgsLayoutItem *item, const QVariantMap &properties )
223{
224 if ( mAddedToLayoutFunc )
225 mAddedToLayoutFunc( item, properties );
226}
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:6024
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:50