QGIS API Documentation 3.99.0-Master (752b475928d)
Loading...
Searching...
No Matches
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 <functional>
20
21#include "qgis_gui.h"
22#include "qgis_sip.h"
23#include "qgsapplication.h"
24#include "qgslayoutitem.h"
26#include "qgspathresolver.h"
27
28#include <QGraphicsItem>
29#include <QIcon>
30
31class QgsLayout;
32class QgsLayoutView;
33class QgsLayoutItem;
36
47{
48 public:
50 enum Flag SIP_ENUM_BASETYPE( IntFlag )
51 {
53 };
54 Q_DECLARE_FLAGS( Flags, Flag )
55
56
65 QgsLayoutItemAbstractGuiMetadata( int type, const QString &visibleName, const QString &groupId = QString(), bool isNodeBased = false, Flags flags = QgsLayoutItemAbstractGuiMetadata::Flags() )
66 : mType( type )
67 , mGroupId( groupId )
68 , mIsNodeBased( isNodeBased )
69 , mName( visibleName )
70 , mFlags( flags )
71 {}
72
74
78 int type() const { return mType; }
79
83 Flags flags() const { return mFlags; }
84
88 QString groupId() const { return mGroupId; }
89
93 bool isNodeBased() const { return mIsNodeBased; }
94
98 QString visibleName() const { return mName; }
99
103 virtual QIcon creationIcon() const { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ); }
104
105 /*
106 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
107 * the case.
108 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
109 *
110 * "
111 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
112 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
113 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
114 * by Python so the /Factory/ on create() would be correct.)
115 *
116 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
117 * "
118 */
119
124 {
125 Q_UNUSED( item )
126 return nullptr;
127 }
128
134 virtual QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) SIP_TRANSFERBACK;
135
141 virtual QGraphicsItem *createNodeRubberBand( QgsLayoutView *view ) SIP_TRANSFERBACK;
142
146 virtual QgsLayoutItem *createItem( QgsLayout *layout ) SIP_TRANSFERBACK;
147
154 virtual void newItemAddedToLayout( QgsLayoutItem *item );
155
163 virtual void handleDoubleClick( QgsLayoutItem *item, Qgis::MouseHandlesAction action );
164
165 private:
166 int mType = -1;
167 QString mGroupId;
168 bool mIsNodeBased = false;
169 QString mName;
170 Flags mFlags;
171};
172
175
178
180typedef std::function<QGraphicsItem *( QgsLayoutView * )> QgsLayoutNodeItemRubberBandFunc SIP_SKIP;
181
183typedef std::function<void( QgsLayoutItem *, const QVariantMap & )> QgsLayoutItemAddedToLayoutFunc SIP_SKIP;
184
187
188#ifndef SIP_RUN
189
196{
197 public:
209 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 )
212 , mWidgetFunc( pfWidget )
213 , mRubberBandFunc( pfRubberBand )
214 , mCreateFunc( pfCreateFunc )
215 {}
216
222
227 void setWidgetFunction( const QgsLayoutItemWidgetFunc &function ) { mWidgetFunc = function; }
228
234
240
246
252
258
263 void setItemCreationFunction( const QgsLayoutItemCreateFunc &function ) { mCreateFunc = function; }
264
270
276
282
288
289 void handleDoubleClick( QgsLayoutItem *item, Qgis::MouseHandlesAction action ) override;
290
291 QIcon creationIcon() const override { return mIcon.isNull() ? QgsLayoutItemAbstractGuiMetadata::creationIcon() : mIcon; }
292 QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) override { return mWidgetFunc ? mWidgetFunc( item ) : nullptr; }
294 QGraphicsItem *createNodeRubberBand( QgsLayoutView *view ) override { return mNodeRubberBandFunc ? mNodeRubberBandFunc( view ) : nullptr; }
295
296 QgsLayoutItem *createItem( QgsLayout *layout ) override;
297 void newItemAddedToLayout( QgsLayoutItem *item ) override;
298
310 void newItemAddedToLayout( QgsLayoutItem *item, const QVariantMap &properties );
311
312 protected:
313 QIcon mIcon;
320};
321
322#endif
323
335class GUI_EXPORT QgsLayoutItemGuiGroup
336{
337 public:
341 QgsLayoutItemGuiGroup( const QString &id = QString(), const QString &name = QString(), const QIcon &icon = QIcon() )
342 : id( id )
343 , name( name )
344 , icon( icon )
345 {}
346
350 QString id;
351
355 QString name;
356
360 QIcon icon;
361};
362
363
376class GUI_EXPORT QgsLayoutItemGuiRegistry : public QObject
377{
378 Q_OBJECT
379
380 public:
387 QgsLayoutItemGuiRegistry( QObject *parent = nullptr );
388
389 ~QgsLayoutItemGuiRegistry() override;
390
393
398 QgsLayoutItemAbstractGuiMetadata *itemMetadata( int metadataId ) const;
399
410 int metadataIdForItemType( int type ) const;
411
416
422 bool removeLayoutItemGuiMetadata( int type );
423
432
442 bool addItemGroup( const QgsLayoutItemGuiGroup &group );
443
452 bool removeItemGroup( const QString &id );
453
458 const QgsLayoutItemGuiGroup &itemGroup( const QString &id );
459
460 /*
461 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
462 * the case.
463 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
464 *
465 * "
466 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
467 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
468 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
469 * by Python so the /Factory/ on create() would be correct.)
470 *
471 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
472 * "
473 */
474
478 QgsLayoutItem *createItem( int metadataId, QgsLayout *layout ) const SIP_TRANSFERBACK;
479
489 void newItemAddedToLayout( int metadataId, QgsLayoutItem *item, const QVariantMap &properties = QVariantMap() );
490
491 /*
492 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
493 * the case.
494 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
495 *
496 * "
497 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
498 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
499 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
500 * by Python so the /Factory/ on create() would be correct.)
501 *
502 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
503 * "
504 */
505
510
517
524 QGraphicsItem *createNodeItemRubberBand( int metadataId, QgsLayoutView *view ) SIP_SKIP;
525
529 QList<int> itemMetadataIds() const;
530
531 signals:
532
537 void typeAdded( int metadataId );
538
545 void typeRemoved( int metadataId );
546
552 void groupRemoved( QString groupId );
553
554 private:
555#ifdef SIP_RUN
557#endif
558
559 QMap<int, QgsLayoutItemAbstractGuiMetadata *> mMetadata;
560
561 QMap<QString, QgsLayoutItemGuiGroup> mItemGroups;
562};
563
564#endif //QGSLAYOUTITEMGUIREGISTRY_H
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:6024
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Stores GUI metadata about one layout item class.
virtual ~QgsLayoutItemAbstractGuiMetadata()=default
QString groupId() const
Returns the item group ID, if set.
virtual QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item)
Creates a configuration widget for an item of this type.
bool isNodeBased() const
Returns true if the associated item is a node based item.
int type() const
Returns the unique item type code for the layout item class.
QgsLayoutItemAbstractGuiMetadata(int type, const QString &visibleName, const QString &groupId=QString(), bool isNodeBased=false, Flags flags=QgsLayoutItemAbstractGuiMetadata::Flags())
Constructor for QgsLayoutItemAbstractGuiMetadata with the specified class type.
QString visibleName() const
Returns a translated, user visible name identifying the corresponding layout item.
virtual QIcon creationIcon() const
Returns an icon representing creation of the layout item type.
Flags flags() const
Returns item flags.
Flag
Flags for controlling how a items behave in the GUI.
@ FlagNoCreationTools
Do not show item creation tools for the 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.
QgsLayoutItemGuiGroup(const QString &id=QString(), const QString &name=QString(), const QIcon &icon=QIcon())
Constructor for QgsLayoutItemGuiGroup.
QString name
Translated group name.
QgsLayoutItemCreateFunc itemCreationFunction() const
Returns the classes' item creation function.
QgsLayoutItemAddedToLayoutFunc mAddedToLayoutFunc
QgsLayoutItemAddedToLayoutFunc itemAddToLayoutFunction() const
Returns the classes' item added to layout function.
QgsLayoutItemWidgetFunc widgetFunction() const
Returns the classes' configuration widget creation function.
QgsLayoutNodeItemRubberBandFunc mNodeRubberBandFunc
QgsLayoutViewRubberBand * createRubberBand(QgsLayoutView *view) override
Creates a rubber band for use when creating layout items of this type.
void setItemAddedToLayoutFunction(const QgsLayoutItemAddedToLayoutFunc &function)
Sets the classes' item 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=QgsLayoutItemAbstractGuiMetadata::Flags(), const QgsLayoutItemCreateFunc &pfCreateFunc=nullptr)
Constructor for QgsLayoutItemGuiMetadata with the specified class type and creationIcon,...
void setItemCreationFunction(const QgsLayoutItemCreateFunc &function)
Sets the classes' item creation function.
QgsLayoutItemCreateFunc mCreateFunc
QgsLayoutNodeItemRubberBandFunc nodeRubberBandCreationFunction() const
Returns the classes' node based rubber band creation function.
QgsLayoutItemRubberBandFunc rubberBandCreationFunction() const
Returns the classes' rubber band creation function.
void setItemDoubleClickedFunction(const QgsLayoutItemDoubleClickedFunc &function)
Sets the classes' item double clicked function.
void setWidgetFunction(const QgsLayoutItemWidgetFunc &function)
Sets the classes' configuration widget creation function.
QgsLayoutItemDoubleClickedFunc mDoubleClickedFunc
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) override
Creates a configuration widget for an item of this type.
QIcon creationIcon() const override
Returns an icon representing creation of the layout item type.
QGraphicsItem * createNodeRubberBand(QgsLayoutView *view) override
Creates a rubber band for use when creating layout node based items of this type.
QgsLayoutItemWidgetFunc mWidgetFunc
void setRubberBandCreationFunction(const QgsLayoutItemRubberBandFunc &function)
Sets the classes' rubber band creation function.
QgsLayoutItemRubberBandFunc mRubberBandFunc
QgsLayoutItemDoubleClickedFunc itemDoubleClickedFunction() const
Returns the classes' item double clicked function.
void setNodeRubberBandCreationFunction(const QgsLayoutNodeItemRubberBandFunc &function)
Sets the classes' node based rubber band creation function.
bool removeItemGroup(const QString &id)
Unregisters an item group from the registry.
QgsLayoutItemBaseWidget * createItemWidget(QgsLayoutItem *item) const
Creates a new instance of a layout item configuration widget for the specified item.
bool removeLayoutItemGuiMetadata(int type)
Unregisters the GUI metadata for a layout item type.
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.
QgsLayoutItemGuiRegistry & operator=(const QgsLayoutItemGuiRegistry &rh)=delete
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.
void typeRemoved(int metadataId)
Emitted whenever an item type is removed from the registry, with the specified metadataId.
void groupRemoved(QString groupId)
Emitted whenever an item group is removed from the registry.
QgsLayoutViewRubberBand * createItemRubberBand(int metadataId, QgsLayoutView *view) const
Creates a new rubber band item for the specified item metadataId and destination view.
QgsLayoutItemGuiRegistry(const QgsLayoutItemGuiRegistry &rh)=delete
QGraphicsItem * createNodeItemRubberBand(int metadataId, QgsLayoutView *view)
Creates a rubber band 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.
void typeAdded(int metadataId)
Emitted whenever a new item type is added to the registry, with the specified metadataId.
Base class for graphical items within a QgsLayout.
An abstract base class for temporary rubber band items in various shapes, for use within QgsLayoutVie...
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:50
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition qgis_sip.h:48
std::function< void(QgsLayoutItem *, const QVariantMap &)> QgsLayoutItemAddedToLayoutFunc
Layout item added to layout callback.
std::function< void(QgsLayoutItem *, Qgis::MouseHandlesAction action)> QgsLayoutItemDoubleClickedFunc
Layout item double clicked.
std::function< QgsLayoutViewRubberBand *(QgsLayoutView *)> QgsLayoutItemRubberBandFunc
Layout rubber band creation function.
std::function< QGraphicsItem *(QgsLayoutView *)> QgsLayoutNodeItemRubberBandFunc
Layout node based rubber band creation function.
std::function< QgsLayoutItemBaseWidget *(QgsLayoutItem *)> QgsLayoutItemWidgetFunc
Layout item configuration widget creation function.
std::function< QgsLayoutItem *(QgsLayout *)> QgsLayoutItemCreateFunc
Layout item creation function.