QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 {
150  return mCreateFunc ? mCreateFunc( layout ) : QgsLayoutItemAbstractGuiMetadata::createItem( layout );
151 }
152 
154 {
155  if ( mAddedToLayoutFunc )
156  mAddedToLayoutFunc( item );
157 }
virtual QAbstractGraphicsShapeItem * createNodeRubberBand(QgsLayoutView *view)
Creates a rubber band for use when creating layout node based items of this type. ...
QgsLayoutItemGuiRegistry(QObject *parent=nullptr)
Creates a new empty item GUI registry.
Stores GUI metadata about a group of layout item classes.
QgsLayoutItem * createItem(int metadataId, QgsLayout *layout) const
Creates a new instance of a layout item given the item metadata metadataId, target layout...
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.
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:49
Stores GUI metadata about one layout item class.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
void newItemAddedToLayout(QgsLayoutItem *item) override
Called when a newly created item of the associated type has been added to a layout.
virtual void newItemAddedToLayout(QgsLayoutItem *item)
Called when a newly created item of the associated type has been added to a layout.
static QgsLayoutItemRegistry * layoutItemRegistry()
Returns the application&#39;s layout item registry, used for layout item types.
QString id
Unique (untranslated) group ID string.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
bool addItemGroup(const QgsLayoutItemGuiGroup &group)
Registers a new item group with the registry.
Frame item, part of a QgsLayoutMultiFrame object.
virtual QgsLayoutItem * createItem(QgsLayout *layout)
Creates an instance of the corresponding 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(QgsLayout *layout) override
Creates an instance of the corresponding item type.
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets...
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) const
Creates a new instance of a layout item configuration widget for the specified item.
A base class for property widgets for layout items.
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes...
virtual int type() const =0
Returns unique multiframe type id.
QList< int > itemMetadataIds() const
Returns a list of available item metadata ids handled by the registry.
QgsLayoutItemAbstractGuiMetadata * itemMetadata(int metadataId) const
Returns the metadata for the specified item metadataId.
void newItemAddedToLayout(int metadataId, QgsLayoutItem *item)
Called when a newly created item of the associated metadata metadataId has been added to a layout...
bool addLayoutItemGuiMetadata(QgsLayoutItemAbstractGuiMetadata *metadata)
Registers the gui metadata for a new layout item type.
const QgsLayoutItemGuiGroup & itemGroup(const QString &id)
Returns a reference to the item group with matching id.
virtual QgsLayoutViewRubberBand * createRubberBand(QgsLayoutView *view)
Creates a rubber band for use when creating layout items of this type.
QAbstractGraphicsShapeItem * createNodeItemRubberBand(int metadataId, QgsLayoutView *view)
Creates a rubber band for the specified item metadataId and destination view.
Base class for frame items, which form a layout multiframe item.
void typeAdded(int metadataId)
Emitted whenever a new item type is added to the registry, with the specified metadataId.