QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 = nullptr )
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 
149 typedef std::function<QgsLayoutItemBaseWidget *( QgsLayoutItem * )> QgsLayoutItemWidgetFunc SIP_SKIP;
150 
152 typedef std::function<QgsLayoutViewRubberBand *( QgsLayoutView * )> QgsLayoutItemRubberBandFunc SIP_SKIP;
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 = nullptr,
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 
virtual QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item)
Creates a configuration widget for an item of this type.
QIcon creationIcon() const override
Returns an icon representing creation of the layout item type.
Stores GUI metadata about a group of layout item classes.
Base class for graphical items within a QgsLayout.
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:49
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) override
Creates a configuration widget for an item of this type.
Convenience metadata class that uses static functions to handle layout item GUI behavior.
QgsLayoutViewRubberBand * createRubberBand(QgsLayoutView *view) override
Creates a rubber band for use when creating layout items of this type.
QString name
Translated group name.
Stores GUI metadata about one layout item class.
QIcon icon
Icon for group.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void setItemCreationFunction(const QgsLayoutItemCreateFunc &function)
Sets the classes&#39; item creation function.
Registry of available layout item GUI behavior.
QString visibleName() const
Returns a translated, user visible name identifying the corresponding layout item.
bool isNodeBased() const
Returns true if the associated item is a node based item.
virtual void newItemAddedToLayout(QgsLayoutItem *item)
Called when a newly created item of the associated type has been added to a layout.
virtual QIcon creationIcon() const
Returns an icon representing creation of the layout item type.
#define SIP_SKIP
Definition: qgis_sip.h:119
Flags flags() const
Returns item flags.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QString id
Unique (untranslated) group ID string.
std::function< QgsLayoutViewRubberBand *(QgsLayoutView *)> QgsLayoutItemRubberBandFunc
Layout rubber band creation function.
QString groupId() const
Returns the item group ID, if set.
#define SIP_FACTORY
Definition: qgis_sip.h:69
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
virtual QgsLayoutItem * createItem(QgsLayout *layout)
Creates an instance of the corresponding item type.
void setWidgetFunction(const QgsLayoutItemWidgetFunc &function)
Sets the classes&#39; configuration widget creation function.
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=nullptr, const QgsLayoutItemCreateFunc &pfCreateFunc=nullptr)
Constructor for QgsLayoutItemGuiMetadata with the specified class type and creationIcon, and function pointers for the various configuration widget creation functions.
Flag
Flags for controlling how a items behave in the GUI.
std::function< QAbstractGraphicsShapeItem *(QgsLayoutView *)> QgsLayoutNodeItemRubberBandFunc
Layout node based rubber band creation function.
void setItemAddedToLayoutFunction(const QgsLayoutItemAddedToLayoutFunc &function)
Sets the classes&#39; item creation function.
A base class for property widgets for layout items.
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes...
QAbstractGraphicsShapeItem * createNodeRubberBand(QgsLayoutView *view) override
Creates a rubber band for use when creating layout node based items of this type. ...
std::function< void(QgsLayoutItem *)> QgsLayoutItemAddedToLayoutFunc
Layout item added to layout callback.
QgsLayoutItemRubberBandFunc rubberBandCreationFunction() const
Returns the classes&#39; rubber band creation function.
QgsLayoutItemCreateFunc itemCreationFunction() const
Returns the classes&#39; item creation function.
void setNodeRubberBandCreationFunction(const QgsLayoutNodeItemRubberBandFunc &function)
Sets the classes&#39; node based rubber band creation function.
QgsLayoutItemAddedToLayoutFunc itemAddToLayoutFunction() const
Returns the classes&#39; item added to layout function.
int type() const
Returns the unique item type code for the layout item class.
QgsLayoutNodeItemRubberBandFunc nodeRubberBandCreationFunction() const
Returns the classes&#39; node based rubber band creation function.
void setRubberBandCreationFunction(const QgsLayoutItemRubberBandFunc &function)
Sets the classes&#39; rubber band creation function.
std::function< QgsLayoutItem *(QgsLayout *)> QgsLayoutItemCreateFunc
Layout item creation function.
std::function< QgsLayoutItemBaseWidget *(QgsLayoutItem *)> QgsLayoutItemWidgetFunc
Layout item configuration widget creation function.
QgsLayoutItemWidgetFunc widgetFunction() const
Returns the classes&#39; configuration widget creation function.
QgsLayoutItemGuiGroup(const QString &id=QString(), const QString &name=QString(), const QIcon &icon=QIcon())
Constructor for QgsLayoutItemGuiGroup.