QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 "qgis_gui.h"
20#include "qgis_sip.h"
21#include "qgsapplication.h"
22#include "qgspathresolver.h"
24#include <QGraphicsItem> //for QGraphicsItem::UserType
25#include <QIcon>
26#include <functional>
27
28#include "qgslayoutitem.h" // temporary
29
30class QgsLayout;
31class QgsLayoutView;
32class QgsLayoutItem;
35
46{
47 public:
48
50 enum Flag SIP_ENUM_BASETYPE( IntFlag )
51 {
52 FlagNoCreationTools = 1 << 1,
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
123 virtual QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) SIP_TRANSFERBACK { Q_UNUSED( item ) return nullptr; }
124
130 virtual QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) SIP_TRANSFERBACK;
131
137 virtual QAbstractGraphicsShapeItem *createNodeRubberBand( QgsLayoutView *view ) SIP_TRANSFERBACK;
138
142 virtual QgsLayoutItem *createItem( QgsLayout *layout ) SIP_TRANSFERBACK;
143
150 virtual void newItemAddedToLayout( QgsLayoutItem *item );
151
152 private:
153
154 int mType = -1;
155 QString mGroupId;
156 bool mIsNodeBased = false;
157 QString mName;
158 Flags mFlags;
159
160};
161
164
167
169typedef std::function<QAbstractGraphicsShapeItem *( QgsLayoutView * )> QgsLayoutNodeItemRubberBandFunc SIP_SKIP;
170
172typedef std::function<void ( QgsLayoutItem *, const QVariantMap & )> QgsLayoutItemAddedToLayoutFunc SIP_SKIP;
173
174#ifndef SIP_RUN
175
182{
183 public:
184
196 QgsLayoutItemGuiMetadata( int type, const QString &visibleName, const QIcon &creationIcon,
197 const QgsLayoutItemWidgetFunc &pfWidget = nullptr,
198 const QgsLayoutItemRubberBandFunc &pfRubberBand = nullptr, const QString &groupId = QString(),
199 bool isNodeBased = false,
201 const QgsLayoutItemCreateFunc &pfCreateFunc = nullptr )
202 : QgsLayoutItemAbstractGuiMetadata( type, visibleName, groupId, isNodeBased, flags )
203 , mIcon( creationIcon )
204 , mWidgetFunc( pfWidget )
205 , mRubberBandFunc( pfRubberBand )
206 , mCreateFunc( pfCreateFunc )
207 {}
208
213 QgsLayoutItemWidgetFunc widgetFunction() const { return mWidgetFunc; }
214
219 void setWidgetFunction( const QgsLayoutItemWidgetFunc &function ) { mWidgetFunc = function; }
220
225 QgsLayoutItemRubberBandFunc rubberBandCreationFunction() const { return mRubberBandFunc; }
226
231 void setRubberBandCreationFunction( const QgsLayoutItemRubberBandFunc &function ) { mRubberBandFunc = function; }
232
238
243 void setNodeRubberBandCreationFunction( const QgsLayoutNodeItemRubberBandFunc &function ) { mNodeRubberBandFunc = function; }
244
249 QgsLayoutItemCreateFunc itemCreationFunction() const { return mCreateFunc; }
250
255 void setItemCreationFunction( const QgsLayoutItemCreateFunc &function ) { mCreateFunc = function; }
256
261 QgsLayoutItemAddedToLayoutFunc itemAddToLayoutFunction() const { return mAddedToLayoutFunc; }
262
267 void setItemAddedToLayoutFunction( const QgsLayoutItemAddedToLayoutFunc &function ) { mAddedToLayoutFunc = function; }
268
269 QIcon creationIcon() const override { return mIcon.isNull() ? QgsLayoutItemAbstractGuiMetadata::creationIcon() : mIcon; }
270 QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) override { return mWidgetFunc ? mWidgetFunc( item ) : nullptr; }
271 QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) override { return mRubberBandFunc ? mRubberBandFunc( view ) : nullptr; }
272 QAbstractGraphicsShapeItem *createNodeRubberBand( QgsLayoutView *view ) override { return mNodeRubberBandFunc ? mNodeRubberBandFunc( view ) : nullptr; }
273
274 QgsLayoutItem *createItem( QgsLayout *layout ) override;
275 void newItemAddedToLayout( QgsLayoutItem *item ) override;
276
288 void newItemAddedToLayout( QgsLayoutItem *item, const QVariantMap &properties );
289
290 protected:
291 QIcon mIcon;
292 QgsLayoutItemWidgetFunc mWidgetFunc = nullptr;
293 QgsLayoutItemRubberBandFunc mRubberBandFunc = nullptr;
294 QgsLayoutNodeItemRubberBandFunc mNodeRubberBandFunc = nullptr;
295 QgsLayoutItemCreateFunc mCreateFunc = nullptr;
296 QgsLayoutItemAddedToLayoutFunc mAddedToLayoutFunc = nullptr;
297
298};
299
300#endif
301
313class GUI_EXPORT QgsLayoutItemGuiGroup
314{
315 public:
316
320 QgsLayoutItemGuiGroup( const QString &id = QString(), const QString &name = QString(), const QIcon &icon = QIcon() )
321 : id( id )
322 , name( name )
323 , icon( icon )
324 {}
325
329 QString id;
330
334 QString name;
335
339 QIcon icon;
340
341};
342
343
356class GUI_EXPORT QgsLayoutItemGuiRegistry : public QObject
357{
358 Q_OBJECT
359
360 public:
361
368 QgsLayoutItemGuiRegistry( QObject *parent = nullptr );
369
370 ~QgsLayoutItemGuiRegistry() override;
371
374
379 QgsLayoutItemAbstractGuiMetadata *itemMetadata( int metadataId ) const;
380
391 int metadataIdForItemType( int type ) const;
392
396 bool addLayoutItemGuiMetadata( QgsLayoutItemAbstractGuiMetadata *metadata SIP_TRANSFER );
397
407 bool addItemGroup( const QgsLayoutItemGuiGroup &group );
408
413 const QgsLayoutItemGuiGroup &itemGroup( const QString &id );
414
415 /*
416 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
417 * the case.
418 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
419 *
420 * "
421 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
422 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
423 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
424 * by Python so the /Factory/ on create() would be correct.)
425 *
426 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
427 * "
428 */
429
433 QgsLayoutItem *createItem( int metadataId, QgsLayout *layout ) const SIP_TRANSFERBACK;
434
444 void newItemAddedToLayout( int metadataId, QgsLayoutItem *item, const QVariantMap &properties = QVariantMap() );
445
446 /*
447 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
448 * the case.
449 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
450 *
451 * "
452 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
453 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
454 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
455 * by Python so the /Factory/ on create() would be correct.)
456 *
457 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
458 * "
459 */
460
464 QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) const SIP_TRANSFERBACK;
465
471 QgsLayoutViewRubberBand *createItemRubberBand( int metadataId, QgsLayoutView *view ) const SIP_SKIP;
472
479 QAbstractGraphicsShapeItem *createNodeItemRubberBand( int metadataId, QgsLayoutView *view ) SIP_SKIP;
480
484 QList< int > itemMetadataIds() const;
485
486 signals:
487
492 void typeAdded( int metadataId );
493
494 private:
495#ifdef SIP_RUN
497#endif
498
499 QMap< int, QgsLayoutItemAbstractGuiMetadata *> mMetadata;
500
501 QMap< QString, QgsLayoutItemGuiGroup > mItemGroups;
502
503};
504
505#endif //QGSLAYOUTITEMGUIREGISTRY_H
506
507
508
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.
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.
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.
Convenience metadata class that uses static functions to handle layout item GUI behavior.
QgsLayoutItemCreateFunc itemCreationFunction() const
Returns the classes' item creation function.
QgsLayoutItemAddedToLayoutFunc itemAddToLayoutFunction() const
Returns the classes' item added to layout function.
QgsLayoutItemWidgetFunc widgetFunction() const
Returns the classes' configuration widget creation function.
QAbstractGraphicsShapeItem * createNodeRubberBand(QgsLayoutView *view) override
Creates a rubber band for use when creating layout node based items of this type.
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.
QgsLayoutNodeItemRubberBandFunc nodeRubberBandCreationFunction() const
Returns the classes' node based rubber band creation function.
QgsLayoutItemRubberBandFunc rubberBandCreationFunction() const
Returns the classes' rubber band creation function.
void setWidgetFunction(const QgsLayoutItemWidgetFunc &function)
Sets the classes' configuration widget creation function.
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.
void setRubberBandCreationFunction(const QgsLayoutItemRubberBandFunc &function)
Sets the classes' rubber band creation function.
void setNodeRubberBandCreationFunction(const QgsLayoutNodeItemRubberBandFunc &function)
Sets the classes' node based rubber band creation function.
Registry of available layout item GUI behavior.
QgsLayoutItemGuiRegistry & operator=(const QgsLayoutItemGuiRegistry &rh)=delete
QgsLayoutItemGuiRegistry(const QgsLayoutItemGuiRegistry &rh)=delete
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.
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes,...
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:49
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#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< QgsLayoutViewRubberBand *(QgsLayoutView *)> QgsLayoutItemRubberBandFunc
Layout rubber band creation function.
std::function< QAbstractGraphicsShapeItem *(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.