QGIS API Documentation 3.41.0-Master (cea29feecf2)
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:
49 enum Flag SIP_ENUM_BASETYPE( IntFlag )
50 {
51 FlagNoCreationTools = 1 << 1,
52 };
53 Q_DECLARE_FLAGS( Flags, Flag )
54
55
64 QgsLayoutItemAbstractGuiMetadata( int type, const QString &visibleName, const QString &groupId = QString(), bool isNodeBased = false, Flags flags = QgsLayoutItemAbstractGuiMetadata::Flags() )
65 : mType( type )
66 , mGroupId( groupId )
67 , mIsNodeBased( isNodeBased )
68 , mName( visibleName )
69 , mFlags( flags )
70 {}
71
73
77 int type() const { return mType; }
78
82 Flags flags() const { return mFlags; }
83
87 QString groupId() const { return mGroupId; }
88
92 bool isNodeBased() const { return mIsNodeBased; }
93
97 QString visibleName() const { return mName; }
98
102 virtual QIcon creationIcon() const { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ); }
103
104 /*
105 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
106 * the case.
107 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
108 *
109 * "
110 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
111 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
112 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
113 * by Python so the /Factory/ on create() would be correct.)
114 *
115 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
116 * "
117 */
118
123 {
124 Q_UNUSED( item )
125 return nullptr;
126 }
127
133 virtual QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) SIP_TRANSFERBACK;
134
140 virtual QGraphicsItem *createNodeRubberBand( QgsLayoutView *view ) SIP_TRANSFERBACK;
141
145 virtual QgsLayoutItem *createItem( QgsLayout *layout ) SIP_TRANSFERBACK;
146
153 virtual void newItemAddedToLayout( QgsLayoutItem *item );
154
162 virtual void handleDoubleClick( QgsLayoutItem *item, Qgis::MouseHandlesAction action );
163
164 private:
165 int mType = -1;
166 QString mGroupId;
167 bool mIsNodeBased = false;
168 QString mName;
169 Flags mFlags;
170};
171
174
177
179typedef std::function<QGraphicsItem *( QgsLayoutView * )> QgsLayoutNodeItemRubberBandFunc SIP_SKIP;
180
182typedef std::function<void( QgsLayoutItem *, const QVariantMap & )> QgsLayoutItemAddedToLayoutFunc SIP_SKIP;
183
186
187#ifndef SIP_RUN
188
195{
196 public:
208 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 )
209 : QgsLayoutItemAbstractGuiMetadata( type, visibleName, groupId, isNodeBased, flags )
210 , mIcon( creationIcon )
211 , mWidgetFunc( pfWidget )
212 , mRubberBandFunc( pfRubberBand )
213 , mCreateFunc( pfCreateFunc )
214 {}
215
220 QgsLayoutItemWidgetFunc widgetFunction() const { return mWidgetFunc; }
221
226 void setWidgetFunction( const QgsLayoutItemWidgetFunc &function ) { mWidgetFunc = function; }
227
232 QgsLayoutItemRubberBandFunc rubberBandCreationFunction() const { return mRubberBandFunc; }
233
238 void setRubberBandCreationFunction( const QgsLayoutItemRubberBandFunc &function ) { mRubberBandFunc = function; }
239
245
250 void setNodeRubberBandCreationFunction( const QgsLayoutNodeItemRubberBandFunc &function ) { mNodeRubberBandFunc = function; }
251
256 QgsLayoutItemCreateFunc itemCreationFunction() const { return mCreateFunc; }
257
262 void setItemCreationFunction( const QgsLayoutItemCreateFunc &function ) { mCreateFunc = function; }
263
268 QgsLayoutItemAddedToLayoutFunc itemAddToLayoutFunction() const { return mAddedToLayoutFunc; }
269
274 void setItemAddedToLayoutFunction( const QgsLayoutItemAddedToLayoutFunc &function ) { mAddedToLayoutFunc = function; }
275
280 QgsLayoutItemDoubleClickedFunc itemDoubleClickedFunction() const { return mDoubleClickedFunc; }
281
286 void setItemDoubleClickedFunction( const QgsLayoutItemDoubleClickedFunc &function ) { mDoubleClickedFunc = function; }
287
288 void handleDoubleClick( QgsLayoutItem *item, Qgis::MouseHandlesAction action ) override;
289
290 QIcon creationIcon() const override { return mIcon.isNull() ? QgsLayoutItemAbstractGuiMetadata::creationIcon() : mIcon; }
291 QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) override { return mWidgetFunc ? mWidgetFunc( item ) : nullptr; }
292 QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) override { return mRubberBandFunc ? mRubberBandFunc( view ) : nullptr; }
293 QGraphicsItem *createNodeRubberBand( QgsLayoutView *view ) override { return mNodeRubberBandFunc ? mNodeRubberBandFunc( view ) : nullptr; }
294
295 QgsLayoutItem *createItem( QgsLayout *layout ) override;
296 void newItemAddedToLayout( QgsLayoutItem *item ) override;
297
309 void newItemAddedToLayout( QgsLayoutItem *item, const QVariantMap &properties );
310
311 protected:
312 QIcon mIcon;
313 QgsLayoutItemWidgetFunc mWidgetFunc = nullptr;
314 QgsLayoutItemRubberBandFunc mRubberBandFunc = nullptr;
315 QgsLayoutNodeItemRubberBandFunc mNodeRubberBandFunc = nullptr;
316 QgsLayoutItemCreateFunc mCreateFunc = nullptr;
317 QgsLayoutItemAddedToLayoutFunc mAddedToLayoutFunc = nullptr;
318 QgsLayoutItemDoubleClickedFunc mDoubleClickedFunc = nullptr;
319};
320
321#endif
322
334class GUI_EXPORT QgsLayoutItemGuiGroup
335{
336 public:
340 QgsLayoutItemGuiGroup( const QString &id = QString(), const QString &name = QString(), const QIcon &icon = QIcon() )
341 : id( id )
342 , name( name )
343 , icon( icon )
344 {}
345
349 QString id;
350
354 QString name;
355
359 QIcon icon;
360};
361
362
375class GUI_EXPORT QgsLayoutItemGuiRegistry : public QObject
376{
377 Q_OBJECT
378
379 public:
386 QgsLayoutItemGuiRegistry( QObject *parent = nullptr );
387
388 ~QgsLayoutItemGuiRegistry() override;
389
392
397 QgsLayoutItemAbstractGuiMetadata *itemMetadata( int metadataId ) const;
398
409 int metadataIdForItemType( int type ) const;
410
414 bool addLayoutItemGuiMetadata( QgsLayoutItemAbstractGuiMetadata *metadata SIP_TRANSFER );
415
425 bool addItemGroup( const QgsLayoutItemGuiGroup &group );
426
431 const QgsLayoutItemGuiGroup &itemGroup( const QString &id );
432
433 /*
434 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
435 * the case.
436 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
437 *
438 * "
439 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
440 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
441 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
442 * by Python so the /Factory/ on create() would be correct.)
443 *
444 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
445 * "
446 */
447
451 QgsLayoutItem *createItem( int metadataId, QgsLayout *layout ) const SIP_TRANSFERBACK;
452
462 void newItemAddedToLayout( int metadataId, QgsLayoutItem *item, const QVariantMap &properties = QVariantMap() );
463
464 /*
465 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
466 * the case.
467 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
468 *
469 * "
470 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
471 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
472 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
473 * by Python so the /Factory/ on create() would be correct.)
474 *
475 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
476 * "
477 */
478
482 QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) const SIP_TRANSFERBACK;
483
489 QgsLayoutViewRubberBand *createItemRubberBand( int metadataId, QgsLayoutView *view ) const SIP_SKIP;
490
497 QGraphicsItem *createNodeItemRubberBand( int metadataId, QgsLayoutView *view ) SIP_SKIP;
498
502 QList<int> itemMetadataIds() const;
503
504 signals:
505
510 void typeAdded( int metadataId );
511
512 private:
513#ifdef SIP_RUN
515#endif
516
517 QMap<int, QgsLayoutItemAbstractGuiMetadata *> mMetadata;
518
519 QMap<QString, QgsLayoutItemGuiGroup> mItemGroups;
520};
521
522#endif //QGSLAYOUTITEMGUIREGISTRY_H
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:5667
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.
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 setItemDoubleClickedFunction(const QgsLayoutItemDoubleClickedFunc &function)
Sets the classes' item double clicked 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.
QGraphicsItem * createNodeRubberBand(QgsLayoutView *view) override
Creates a rubber band for use when creating layout node based items of this type.
void setRubberBandCreationFunction(const QgsLayoutItemRubberBandFunc &function)
Sets the classes' rubber band creation function.
QgsLayoutItemDoubleClickedFunc itemDoubleClickedFunction() const
Returns the classes' item double clicked 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< 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.