QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  if ( !metadata )
63  return false;
64 
65  int id = mMetadata.count();
66  mMetadata[id] = metadata;
67  emit typeAdded( id );
68  return true;
69 }
70 
72 {
73  if ( mItemGroups.contains( group.id ) )
74  return false;
75 
76  mItemGroups.insert( group.id, group );
77  return true;
78 }
79 
81 {
82  return mItemGroups[ id ];
83 }
84 
86 {
87  if ( !mMetadata.contains( metadataId ) )
88  return nullptr;
89 
90  std::unique_ptr< QgsLayoutItem > item( mMetadata.value( metadataId )->createItem( layout ) );
91  if ( item )
92  return item.release();
93 
94  int type = mMetadata.value( metadataId )->type();
95  return QgsApplication::layoutItemRegistry()->createItem( type, layout );
96 }
97 
99 {
100  if ( !mMetadata.contains( metadataId ) )
101  return;
102 
103  mMetadata.value( metadataId )->newItemAddedToLayout( item );
104 }
105 
107 {
108  if ( !item )
109  return nullptr;
110 
111  int type = item->type();
113  {
114  QgsLayoutMultiFrame *multiFrame = qobject_cast< QgsLayoutFrame * >( item )->multiFrame();
115  if ( multiFrame )
116  type = multiFrame->type();
117  }
118  for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
119  {
120  if ( it.value()->type() == type )
121  return it.value()->createItemWidget( item );
122  }
123 
124  return nullptr;
125 }
126 
128 {
129  if ( !mMetadata.contains( metadataId ) )
130  return nullptr;
131 
132  return mMetadata[metadataId]->createRubberBand( view );
133 }
134 
135 QAbstractGraphicsShapeItem *QgsLayoutItemGuiRegistry::createNodeItemRubberBand( int metadataId, QgsLayoutView *view )
136 {
137  if ( !mMetadata.contains( metadataId ) )
138  return nullptr;
139 
140  return mMetadata[metadataId]->createNodeRubberBand( view );
141 }
142 
144 {
145  return mMetadata.keys();
146 }
147 
149 {
151 }
152 
154 {
155  if ( mAddedToLayoutFunc )
156  mAddedToLayoutFunc( item );
157 }
QgsLayoutItemGuiRegistry::newItemAddedToLayout
void newItemAddedToLayout(int metadataId, QgsLayoutItem *item)
Called when a newly created item of the associated metadata metadataId has been added to a layout.
Definition: qgslayoutitemguiregistry.cpp:98
QgsLayoutItemGuiMetadata::mAddedToLayoutFunc
QgsLayoutItemAddedToLayoutFunc mAddedToLayoutFunc
Definition: qgslayoutitemguiregistry.h:269
QgsLayoutItemGuiRegistry::createItemWidget
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) const
Creates a new instance of a layout item configuration widget for the specified item.
Definition: qgslayoutitemguiregistry.cpp:106
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:287
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:127
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:122
QgsLayoutViewRectangularRubberBand
Definition: qgslayoutviewrubberband.h:159
QgsLayoutItemGuiRegistry::~QgsLayoutItemGuiRegistry
~QgsLayoutItemGuiRegistry() override
Definition: qgslayoutitemguiregistry.cpp:50
QgsLayoutMultiFrame
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:303
qgslayoutitemguiregistry.h
QgsLayoutItemGuiRegistry::addItemGroup
bool addItemGroup(const QgsLayoutItemGuiGroup &group)
Registers a new item group with the registry.
Definition: qgslayoutitemguiregistry.cpp:71
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
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:85
QgsLayoutItemGuiRegistry::createNodeItemRubberBand
QAbstractGraphicsShapeItem * createNodeItemRubberBand(int metadataId, QgsLayoutView *view)
Creates a rubber band for the specified item metadataId and destination view.
Definition: qgslayoutitemguiregistry.cpp:135
QgsLayoutItemGuiMetadata::mCreateFunc
QgsLayoutItemCreateFunc mCreateFunc
Definition: qgslayoutitemguiregistry.h:268
QgsLayoutItemBaseWidget
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:80
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:2159
QgsLayoutItemGuiRegistry::addLayoutItemGuiMetadata
bool addLayoutItemGuiMetadata(QgsLayoutItemAbstractGuiMetadata *metadata)
Registers the gui metadata for a new layout item type.
Definition: qgslayoutitemguiregistry.cpp:60
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QgsLayoutItemGuiRegistry::itemMetadataIds
QList< int > itemMetadataIds() const
Returns a list of available item metadata ids handled by the registry.
Definition: qgslayoutitemguiregistry.cpp:143
QgsLayoutItemGuiMetadata::createItem
QgsLayoutItem * createItem(QgsLayout *layout) override
Creates an instance of the corresponding item type.
Definition: qgslayoutitemguiregistry.cpp:148
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:153
QgsLayoutView
Definition: qgslayoutview.h:49
QgsLayoutViewRubberBand
Definition: qgslayoutviewrubberband.h:37
QgsLayoutItemRegistry::LayoutFrame
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
Definition: qgslayoutitemregistry.h:325
qgslayoutitemregistry.h