QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
45
47 : QObject( parent )
48{
49}
50
52{
53 qDeleteAll( mMetadata );
54}
55
57{
58 return mMetadata.value( metadataId );
59}
60
62{
63 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
64 {
65 if ( it.value()->type() == type )
66 return it.key();
67 }
68 return -1;
69}
70
72{
73 if ( !metadata )
74 return false;
75
76 const int id = mMetadata.count();
77 mMetadata[id] = metadata;
78 emit typeAdded( id );
79 return true;
80}
81
83{
84 if ( mItemGroups.contains( group.id ) )
85 return false;
86
87 mItemGroups.insert( group.id, group );
88 return true;
89}
90
92{
93 return mItemGroups[ id ];
94}
95
97{
98 if ( !mMetadata.contains( metadataId ) )
99 return nullptr;
100
101 std::unique_ptr< QgsLayoutItem > item( mMetadata.value( metadataId )->createItem( layout ) );
102 if ( item )
103 return item.release();
104
105 const int type = mMetadata.value( metadataId )->type();
106 return QgsApplication::layoutItemRegistry()->createItem( type, layout );
107}
108
109void QgsLayoutItemGuiRegistry::newItemAddedToLayout( int metadataId, QgsLayoutItem *item, const QVariantMap &properties )
110{
111 if ( !mMetadata.contains( metadataId ) )
112 return;
113
114 if ( QgsLayoutItemGuiMetadata *metadata = dynamic_cast<QgsLayoutItemGuiMetadata *>( mMetadata.value( metadataId ) ) )
115 {
116 metadata->newItemAddedToLayout( item, properties );
117 }
118 else
119 {
120 mMetadata.value( metadataId )->newItemAddedToLayout( item );
121 }
122}
123
125{
126 if ( !item )
127 return nullptr;
128
129 int type = item->type();
131 {
132 QgsLayoutMultiFrame *multiFrame = qobject_cast< QgsLayoutFrame * >( item )->multiFrame();
133 if ( multiFrame )
134 type = multiFrame->type();
135 }
136 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
137 {
138 if ( it.value()->type() == type )
139 return it.value()->createItemWidget( item );
140 }
141
142 return nullptr;
143}
144
146{
147 if ( !mMetadata.contains( metadataId ) )
148 return nullptr;
149
150 return mMetadata[metadataId]->createRubberBand( view );
151}
152
153QAbstractGraphicsShapeItem *QgsLayoutItemGuiRegistry::createNodeItemRubberBand( int metadataId, QgsLayoutView *view )
154{
155 if ( !mMetadata.contains( metadataId ) )
156 return nullptr;
157
158 return mMetadata[metadataId]->createNodeRubberBand( view );
159}
160
162{
163 return mMetadata.keys();
164}
165
170
172{
173 if ( mAddedToLayoutFunc )
174 mAddedToLayoutFunc( item, QVariantMap() );
175}
176
177void QgsLayoutItemGuiMetadata::newItemAddedToLayout( QgsLayoutItem *item, const QVariantMap &properties )
178{
179 if ( mAddedToLayoutFunc )
180 mAddedToLayoutFunc( item, properties );
181}
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.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49