QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgslayoutitemguiregistry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemguiregistry.h
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 #ifndef QGSLAYOUTITEMGUIREGISTRY_H
17 #define QGSLAYOUTITEMGUIREGISTRY_H
18 
19 #include "qgis_gui.h"
20 #include "qgis_sip.h"
21 #include "qgsapplication.h"
22 #include "qgspathresolver.h"
23 #include "qgslayoutitemregistry.h"
24 #include <QGraphicsItem> //for QGraphicsItem::UserType
25 #include <QIcon>
26 #include <functional>
27 
28 #include "qgslayoutitem.h" // temporary
29 
30 class QgsLayout;
31 class QgsLayoutView;
32 class QgsLayoutItem;
35 
47 {
48  public:
49 
51  enum Flag
52  {
53  FlagNoCreationTools = 1 << 1,
54  };
55  Q_DECLARE_FLAGS( Flags, Flag )
56 
57 
66  QgsLayoutItemAbstractGuiMetadata( int type, const QString &visibleName, const QString &groupId = QString(), bool isNodeBased = false, Flags flags = QgsLayoutItemAbstractGuiMetadata::Flags() )
67  : mType( type )
68  , mGroupId( groupId )
69  , mIsNodeBased( isNodeBased )
70  , mName( visibleName )
71  , mFlags( flags )
72  {}
73 
74  virtual ~QgsLayoutItemAbstractGuiMetadata() = default;
75 
79  int type() const { return mType; }
80 
84  Flags flags() const { return mFlags; }
85 
89  QString groupId() const { return mGroupId; }
90 
94  bool isNodeBased() const { return mIsNodeBased; }
95 
99  QString visibleName() const { return mName; }
100 
104  virtual QIcon creationIcon() const { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ); }
105 
109  virtual QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) SIP_FACTORY { Q_UNUSED( item ) return nullptr; }
110 
116  virtual QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) SIP_FACTORY;
117 
123  virtual QAbstractGraphicsShapeItem *createNodeRubberBand( QgsLayoutView *view ) SIP_FACTORY;
124 
128  virtual QgsLayoutItem *createItem( QgsLayout *layout ) SIP_FACTORY;
129 
136  virtual void newItemAddedToLayout( QgsLayoutItem *item );
137 
138  private:
139 
140  int mType = -1;
141  QString mGroupId;
142  bool mIsNodeBased = false;
143  QString mName;
144  Flags mFlags;
145 
146 };
147 
150 
153 
155 typedef std::function<QAbstractGraphicsShapeItem *( QgsLayoutView * )> QgsLayoutNodeItemRubberBandFunc SIP_SKIP;
156 
158 typedef std::function<void ( QgsLayoutItem * )> QgsLayoutItemAddedToLayoutFunc SIP_SKIP;
159 
160 #ifndef SIP_RUN
161 
169 {
170  public:
171 
183  QgsLayoutItemGuiMetadata( int type, const QString &visibleName, const QIcon &creationIcon,
184  const QgsLayoutItemWidgetFunc &pfWidget = nullptr,
185  const QgsLayoutItemRubberBandFunc &pfRubberBand = nullptr, const QString &groupId = QString(),
186  bool isNodeBased = false,
187  QgsLayoutItemAbstractGuiMetadata::Flags flags = QgsLayoutItemAbstractGuiMetadata::Flags(),
188  const QgsLayoutItemCreateFunc &pfCreateFunc = nullptr )
189  : QgsLayoutItemAbstractGuiMetadata( type, visibleName, groupId, isNodeBased, flags )
190  , mIcon( creationIcon )
191  , mWidgetFunc( pfWidget )
192  , mRubberBandFunc( pfRubberBand )
193  , mCreateFunc( pfCreateFunc )
194  {}
195 
200  QgsLayoutItemWidgetFunc widgetFunction() const { return mWidgetFunc; }
201 
206  void setWidgetFunction( const QgsLayoutItemWidgetFunc &function ) { mWidgetFunc = function; }
207 
212  QgsLayoutItemRubberBandFunc rubberBandCreationFunction() const { return mRubberBandFunc; }
213 
218  void setRubberBandCreationFunction( const QgsLayoutItemRubberBandFunc &function ) { mRubberBandFunc = function; }
219 
224  QgsLayoutNodeItemRubberBandFunc nodeRubberBandCreationFunction() const { return mNodeRubberBandFunc; }
225 
230  void setNodeRubberBandCreationFunction( const QgsLayoutNodeItemRubberBandFunc &function ) { mNodeRubberBandFunc = function; }
231 
236  QgsLayoutItemCreateFunc itemCreationFunction() const { return mCreateFunc; }
237 
242  void setItemCreationFunction( const QgsLayoutItemCreateFunc &function ) { mCreateFunc = function; }
243 
248  QgsLayoutItemAddedToLayoutFunc itemAddToLayoutFunction() const { return mAddedToLayoutFunc; }
249 
254  void setItemAddedToLayoutFunction( const QgsLayoutItemAddedToLayoutFunc &function ) { mAddedToLayoutFunc = function; }
255 
256  QIcon creationIcon() const override { return mIcon.isNull() ? QgsLayoutItemAbstractGuiMetadata::creationIcon() : mIcon; }
257  QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) override { return mWidgetFunc ? mWidgetFunc( item ) : nullptr; }
258  QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) override { return mRubberBandFunc ? mRubberBandFunc( view ) : nullptr; }
259  QAbstractGraphicsShapeItem *createNodeRubberBand( QgsLayoutView *view ) override { return mNodeRubberBandFunc ? mNodeRubberBandFunc( view ) : nullptr; }
260  QgsLayoutItem *createItem( QgsLayout *layout ) override;
261  void newItemAddedToLayout( QgsLayoutItem *item ) override;
262 
263  protected:
264  QIcon mIcon;
265  QgsLayoutItemWidgetFunc mWidgetFunc = nullptr;
266  QgsLayoutItemRubberBandFunc mRubberBandFunc = nullptr;
267  QgsLayoutNodeItemRubberBandFunc mNodeRubberBandFunc = nullptr;
268  QgsLayoutItemCreateFunc mCreateFunc = nullptr;
269  QgsLayoutItemAddedToLayoutFunc mAddedToLayoutFunc = nullptr;
270 
271 };
272 
273 #endif
274 
287 class GUI_EXPORT QgsLayoutItemGuiGroup
288 {
289  public:
290 
294  QgsLayoutItemGuiGroup( const QString &id = QString(), const QString &name = QString(), const QIcon &icon = QIcon() )
295  : id( id )
296  , name( name )
297  , icon( icon )
298  {}
299 
303  QString id;
304 
308  QString name;
309 
313  QIcon icon;
314 
315 };
316 
317 
331 class GUI_EXPORT QgsLayoutItemGuiRegistry : public QObject
332 {
333  Q_OBJECT
334 
335  public:
336 
343  QgsLayoutItemGuiRegistry( QObject *parent = nullptr );
344 
345  ~QgsLayoutItemGuiRegistry() override;
346 
350  QgsLayoutItemGuiRegistry &operator=( const QgsLayoutItemGuiRegistry &rh ) = delete;
351 
356  QgsLayoutItemAbstractGuiMetadata *itemMetadata( int metadataId ) const;
357 
361  bool addLayoutItemGuiMetadata( QgsLayoutItemAbstractGuiMetadata *metadata SIP_TRANSFER );
362 
372  bool addItemGroup( const QgsLayoutItemGuiGroup &group );
373 
378  const QgsLayoutItemGuiGroup &itemGroup( const QString &id );
379 
383  QgsLayoutItem *createItem( int metadataId, QgsLayout *layout ) const SIP_FACTORY;
384 
391  void newItemAddedToLayout( int metadataId, QgsLayoutItem *item );
392 
396  QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) const SIP_FACTORY;
397 
403  QgsLayoutViewRubberBand *createItemRubberBand( int metadataId, QgsLayoutView *view ) const SIP_SKIP;
404 
411  QAbstractGraphicsShapeItem *createNodeItemRubberBand( int metadataId, QgsLayoutView *view ) SIP_SKIP;
412 
416  QList< int > itemMetadataIds() const;
417 
418  signals:
419 
424  void typeAdded( int metadataId );
425 
426  private:
427 #ifdef SIP_RUN
429 #endif
430 
431  QMap< int, QgsLayoutItemAbstractGuiMetadata *> mMetadata;
432 
433  QMap< QString, QgsLayoutItemGuiGroup > mItemGroups;
434 
435 };
436 
437 #endif //QGSLAYOUTITEMGUIREGISTRY_H
438 
439 
440 
QgsLayoutItemAddedToLayoutFunc
std::function< void(QgsLayoutItem *)> QgsLayoutItemAddedToLayoutFunc
Layout item added to layout callback.
Definition: qgslayoutitemguiregistry.h:158
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:605
QgsLayoutItemGuiGroup
Stores GUI metadata about a group of layout item classes.
Definition: qgslayoutitemguiregistry.h:287
QgsLayoutItemGuiGroup::icon
QIcon icon
Icon for group.
Definition: qgslayoutitemguiregistry.h:313
QgsLayoutItemGuiMetadata::widgetFunction
QgsLayoutItemWidgetFunc widgetFunction() const
Returns the classes' configuration widget creation function.
Definition: qgslayoutitemguiregistry.h:200
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
QgsLayoutItemWidgetFunc
std::function< QgsLayoutItemBaseWidget *(QgsLayoutItem *)> QgsLayoutItemWidgetFunc
Layout item configuration widget creation function.
Definition: qgslayoutitemguiregistry.h:149
qgspathresolver.h
QgsLayoutItemCreateFunc
std::function< QgsLayoutItem *(QgsLayout *)> QgsLayoutItemCreateFunc
Layout item creation function.
Definition: qgslayoutitemregistry.h:107
QgsLayoutItemGuiMetadata::creationIcon
QIcon creationIcon() const override
Returns an icon representing creation of the layout item type.
Definition: qgslayoutitemguiregistry.h:256
QgsLayoutItemGuiMetadata::QgsLayoutItemGuiMetadata
QgsLayoutItemGuiMetadata(int type, const QString &visibleName, const QIcon &creationIcon, const QgsLayoutItemWidgetFunc &pfWidget=nullptr, const QgsLayoutItemRubberBandFunc &pfRubberBand=nullptr, const QString &groupId=QString(), bool isNodeBased=false, QgsLayoutItemAbstractGuiMetadata::Flags flags=QgsLayoutItemAbstractGuiMetadata::Flags(), const QgsLayoutItemCreateFunc &pfCreateFunc=nullptr)
Constructor for QgsLayoutItemGuiMetadata with the specified class type and creationIcon,...
Definition: qgslayoutitemguiregistry.h:183
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayoutItemAbstractGuiMetadata::createItemWidget
virtual QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item)
Creates a configuration widget for an item of this type.
Definition: qgslayoutitemguiregistry.h:109
qgsapplication.h
QgsLayoutItemAbstractGuiMetadata::flags
Flags flags() const
Returns item flags.
Definition: qgslayoutitemguiregistry.h:84
QgsLayoutItemGuiMetadata::setWidgetFunction
void setWidgetFunction(const QgsLayoutItemWidgetFunc &function)
Sets the classes' configuration widget creation function.
Definition: qgslayoutitemguiregistry.h:206
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayoutItemGuiMetadata::createItemWidget
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) override
Creates a configuration widget for an item of this type.
Definition: qgslayoutitemguiregistry.h:257
QgsLayoutItemAbstractGuiMetadata::isNodeBased
bool isNodeBased() const
Returns true if the associated item is a node based item.
Definition: qgslayoutitemguiregistry.h:94
QgsLayoutItemGuiMetadata::createRubberBand
QgsLayoutViewRubberBand * createRubberBand(QgsLayoutView *view) override
Creates a rubber band for use when creating layout items of this type.
Definition: qgslayoutitemguiregistry.h:258
QgsLayoutNodeItemRubberBandFunc
std::function< QAbstractGraphicsShapeItem *(QgsLayoutView *)> QgsLayoutNodeItemRubberBandFunc
Layout node based rubber band creation function.
Definition: qgslayoutitemguiregistry.h:155
QgsLayoutItemGuiMetadata::rubberBandCreationFunction
QgsLayoutItemRubberBandFunc rubberBandCreationFunction() const
Returns the classes' rubber band creation function.
Definition: qgslayoutitemguiregistry.h:212
qgslayoutitem.h
QgsLayoutItemGuiGroup::id
QString id
Unique (untranslated) group ID string.
Definition: qgslayoutitemguiregistry.h:303
QgsLayoutItemGuiMetadata::setItemCreationFunction
void setItemCreationFunction(const QgsLayoutItemCreateFunc &function)
Sets the classes' item creation function.
Definition: qgslayoutitemguiregistry.h:242
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsLayoutItemGuiGroup::name
QString name
Translated group name.
Definition: qgslayoutitemguiregistry.h:308
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsLayoutItemGuiMetadata
Definition: qgslayoutitemguiregistry.h:168
QgsLayoutItemBaseWidget
Definition: qgslayoutitemwidget.h:122
QgsLayoutItemAbstractGuiMetadata::Flag
Flag
Flags for controlling how a items behave in the GUI.
Definition: qgslayoutitemguiregistry.h:51
QgsLayoutItemGuiGroup::QgsLayoutItemGuiGroup
QgsLayoutItemGuiGroup(const QString &id=QString(), const QString &name=QString(), const QIcon &icon=QIcon())
Constructor for QgsLayoutItemGuiGroup.
Definition: qgslayoutitemguiregistry.h:294
QgsLayoutItemGuiMetadata::setNodeRubberBandCreationFunction
void setNodeRubberBandCreationFunction(const QgsLayoutNodeItemRubberBandFunc &function)
Sets the classes' node based rubber band creation function.
Definition: qgslayoutitemguiregistry.h:230
QgsLayoutItemAbstractGuiMetadata::creationIcon
virtual QIcon creationIcon() const
Returns an icon representing creation of the layout item type.
Definition: qgslayoutitemguiregistry.h:104
QgsLayoutItemGuiMetadata::itemCreationFunction
QgsLayoutItemCreateFunc itemCreationFunction() const
Returns the classes' item creation function.
Definition: qgslayoutitemguiregistry.h:236
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QgsLayoutItemGuiMetadata::mIcon
QIcon mIcon
Definition: qgslayoutitemguiregistry.h:264
QgsLayoutItemAbstractGuiMetadata
Stores GUI metadata about one layout item class.
Definition: qgslayoutitemguiregistry.h:46
QgsLayoutItemAbstractGuiMetadata::type
int type() const
Returns the unique item type code for the layout item class.
Definition: qgslayoutitemguiregistry.h:79
QgsLayoutItemAbstractGuiMetadata::createItem
virtual QgsLayoutItem * createItem(QgsLayout *layout)
Creates an instance of the corresponding item type.
Definition: qgslayoutitemguiregistry.cpp:35
QgsLayoutItemGuiMetadata::setItemAddedToLayoutFunction
void setItemAddedToLayoutFunction(const QgsLayoutItemAddedToLayoutFunc &function)
Sets the classes' item creation function.
Definition: qgslayoutitemguiregistry.h:254
QgsLayoutItemRubberBandFunc
std::function< QgsLayoutViewRubberBand *(QgsLayoutView *)> QgsLayoutItemRubberBandFunc
Layout rubber band creation function.
Definition: qgslayoutitemguiregistry.h:152
QgsLayoutView
Definition: qgslayoutview.h:49
QgsLayoutItemGuiMetadata::createNodeRubberBand
QAbstractGraphicsShapeItem * createNodeRubberBand(QgsLayoutView *view) override
Creates a rubber band for use when creating layout node based items of this type.
Definition: qgslayoutitemguiregistry.h:259
QgsLayoutViewRubberBand
Definition: qgslayoutviewrubberband.h:37
QgsLayoutItemGuiMetadata::itemAddToLayoutFunction
QgsLayoutItemAddedToLayoutFunc itemAddToLayoutFunction() const
Returns the classes' item added to layout function.
Definition: qgslayoutitemguiregistry.h:248
qgslayoutitemregistry.h
QgsLayoutItemAbstractGuiMetadata::visibleName
QString visibleName() const
Returns a translated, user visible name identifying the corresponding layout item.
Definition: qgslayoutitemguiregistry.h:99
QgsLayoutItemGuiMetadata::setRubberBandCreationFunction
void setRubberBandCreationFunction(const QgsLayoutItemRubberBandFunc &function)
Sets the classes' rubber band creation function.
Definition: qgslayoutitemguiregistry.h:218
QgsLayoutItemGuiMetadata::nodeRubberBandCreationFunction
QgsLayoutNodeItemRubberBandFunc nodeRubberBandCreationFunction() const
Returns the classes' node based rubber band creation function.
Definition: qgslayoutitemguiregistry.h:224
QgsLayoutItemAbstractGuiMetadata::groupId
QString groupId() const
Returns the item group ID, if set.
Definition: qgslayoutitemguiregistry.h:89
QgsLayoutItemGuiRegistry
Registry of available layout item GUI behavior.
Definition: qgslayoutitemguiregistry.h:331