QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#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 if ( mItemGroups.contains( group.id ) )
95 return false;
96
97 mItemGroups.insert( group.id, group );
98 return true;
99}
100
102{
103 return mItemGroups[id];
104}
105
107{
108 if ( !mMetadata.contains( metadataId ) )
109 return nullptr;
110
111 std::unique_ptr<QgsLayoutItem> item( mMetadata.value( metadataId )->createItem( layout ) );
112 if ( item )
113 return item.release();
114
115 const int type = mMetadata.value( metadataId )->type();
116 return QgsApplication::layoutItemRegistry()->createItem( type, layout );
117}
118
119void QgsLayoutItemGuiRegistry::newItemAddedToLayout( int metadataId, QgsLayoutItem *item, const QVariantMap &properties )
120{
121 if ( !mMetadata.contains( metadataId ) )
122 return;
123
124 if ( QgsLayoutItemGuiMetadata *metadata = dynamic_cast<QgsLayoutItemGuiMetadata *>( mMetadata.value( metadataId ) ) )
125 {
126 metadata->newItemAddedToLayout( item, properties );
127 }
128 else
129 {
130 mMetadata.value( metadataId )->newItemAddedToLayout( item );
131 }
132}
133
135{
136 if ( !item )
137 return nullptr;
138
139 int type = item->type();
141 {
142 QgsLayoutMultiFrame *multiFrame = qobject_cast<QgsLayoutFrame *>( item )->multiFrame();
143 if ( multiFrame )
144 type = multiFrame->type();
145 }
146 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
147 {
148 if ( it.value()->type() == type )
149 return it.value()->createItemWidget( item );
150 }
151
152 return nullptr;
153}
154
156{
157 if ( !mMetadata.contains( metadataId ) )
158 return nullptr;
159
160 return mMetadata[metadataId]->createRubberBand( view );
161}
162
164{
165 if ( !mMetadata.contains( metadataId ) )
166 return nullptr;
167
168 return mMetadata[metadataId]->createNodeRubberBand( view );
169}
170
172{
173 return mMetadata.keys();
174}
175
180
182{
183 if ( mAddedToLayoutFunc )
184 mAddedToLayoutFunc( item, QVariantMap() );
185}
186
187void QgsLayoutItemGuiMetadata::newItemAddedToLayout( QgsLayoutItem *item, const QVariantMap &properties )
188{
189 if ( mAddedToLayoutFunc )
190 mAddedToLayoutFunc( item, properties );
191}
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:5685
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.
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.
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.
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.
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.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49