QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgslayoutitemregistry.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemregistry.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 QGSLAYOUTITEMREGISTRY_H
17#define QGSLAYOUTITEMREGISTRY_H
18
19#include <functional>
20
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgsapplication.h"
24#include "qgslayoutitem.h"
25#include "qgspathresolver.h"
26
27#include <QGraphicsItem>
28#include <QIcon>
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
33class QgsLayout;
34class QgsLayoutView;
35class QgsLayoutItem;
36class QgsFillSymbol;
38
49{
50 public:
51
58 QgsLayoutItemAbstractMetadata( int type, const QString &visibleName, const QString &visiblePluralName = QString() )
59 : mType( type )
60 , mVisibleName( visibleName )
61 , mVisibleNamePlural( visiblePluralName.isEmpty() ? visibleName : visiblePluralName )
62 {}
63
64 virtual ~QgsLayoutItemAbstractMetadata() = default;
65
69 int type() const { return mType; }
70
75 QString visibleName() const { return mVisibleName; }
76
81 QString visiblePluralName() const { return mVisibleNamePlural; }
82
83 /*
84 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
85 * the case.
86 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
87 *
88 * "
89 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
90 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
91 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
92 * by Python so the /Factory/ on create() would be correct.)
93 *
94 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
95 * "
96 */
97
102
110 virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
111 {
112 Q_UNUSED( properties )
113 Q_UNUSED( pathResolver )
114 Q_UNUSED( saving )
115 }
116
117 private:
118
119 int mType = -1;
120 QString mVisibleName;
121 QString mVisibleNamePlural;
122};
123
126
128typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutItemPathResolverFunc SIP_SKIP;
129
130#ifndef SIP_RUN
131
138{
139 public:
140
147 QgsLayoutItemMetadata( int type, const QString &visibleName, const QString &visiblePluralName,
148 const QgsLayoutItemCreateFunc &pfCreate,
149 const QgsLayoutItemPathResolverFunc &pfPathResolver = nullptr )
151 , mCreateFunc( pfCreate )
152 , mPathResolverFunc( pfPathResolver )
153 {}
154
159
164
165 QgsLayoutItem *createItem( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
166
167 void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
168 {
169 if ( mPathResolverFunc )
170 mPathResolverFunc( properties, pathResolver, saving );
171 }
172
173 protected:
176
177};
178
179#endif
180
191{
192 public:
193
199 : mType( type )
200 , mVisibleName( visibleName )
201 {}
202
204
208 int type() const { return mType; }
209
213 virtual QIcon icon() const { return QgsApplication::getThemeIcon( u"/mActionAddBasicRectangle.svg"_s ); }
214
218 QString visibleName() const { return mVisibleName; }
219
220 /*
221 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
222 * the case.
223 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
224 *
225 * "
226 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
227 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
228 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
229 * by Python so the /Factory/ on create() would be correct.)
230 *
231 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
232 * "
233 */
234
239
247 virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
248 {
249 Q_UNUSED( properties )
250 Q_UNUSED( pathResolver )
251 Q_UNUSED( saving )
252 }
253
254 private:
255
256 int mType = -1;
257 QString mVisibleName;
258};
259
262
264typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutMultiFramePathResolverFunc SIP_SKIP;
265
266#ifndef SIP_RUN
267
274{
275 public:
276
282 const QgsLayoutMultiFrameCreateFunc &pfCreate,
283 const QgsLayoutMultiFramePathResolverFunc &pfPathResolver = nullptr )
285 , mCreateFunc( pfCreate )
286 , mPathResolverFunc( pfPathResolver )
287 {}
288
293
298
299 QgsLayoutMultiFrame *createMultiFrame( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
300
301 void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
302 {
303 if ( mPathResolverFunc )
304 mPathResolverFunc( properties, pathResolver, saving );
305 }
306
307 protected:
310
311};
312
313#endif
314
315
328class CORE_EXPORT QgsLayoutItemRegistry : public QObject
329{
330 Q_OBJECT
331
332 public:
333
336 {
337 LayoutItem = QGraphicsItem::UserType + 100,
339
340 // known item types
341
342 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
343 // values, and must be updated if any additional types are added
344
355
356 // known multi-frame types
357
358 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
359 // values, and must be updated if any additional types are added
360
364
366
369
371
373
374 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
375 // values, and must be updated if any additional types are added
376
377 // item types provided by plugins
379 };
380
389 QgsLayoutItemRegistry( QObject *parent = nullptr );
390
391 ~QgsLayoutItemRegistry() override;
392
397 bool populate();
398
401
408
415
416 /*
417 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
418 * the case.
419 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
420 *
421 * "
422 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
423 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
424 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
425 * by Python so the /Factory/ on create() would be correct.)
426 *
427 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
428 * "
429 */
430
436
442 bool removeLayoutItemType( int typeId );
443
452
458 bool removeLayoutMultiFrameType( int typeId );
459
468
474
479 QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_TRANSFERBACK;
480
486
492 void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
493
497 QMap< int, QString> itemTypes() const;
498
499 signals:
500
505 void typeAdded( int type, const QString &name );
506
513 void typeRemoved( int type );
514
519 void multiFrameTypeAdded( int type, const QString &name );
520
527 void multiFrameTypeRemoved( int type );
528
529 private:
530#ifdef SIP_RUN
532#endif
533
534 QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
535 QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
536
537};
538
539#if 0
540#ifndef SIP_RUN
542//simple item for testing
543#ifdef ANDROID
544// For some reason, the Android NDK toolchain requires this to link properly.
545// Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
546class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
547#else
548class TestLayoutItem : public QgsLayoutItem
549#endif
550{
551 Q_OBJECT
552
553 public:
554
555 TestLayoutItem( QgsLayout *layout );
556 ~TestLayoutItem() = default;
557
558 //implement pure virtual methods
559 int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
560 void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
561
562 private:
563 QColor mColor;
564 QgsFillSymbol *mShapeStyleSymbol = nullptr;
565};
567#endif
568#endif
569
570#endif //QGSLAYOUTITEMREGISTRY_H
571
572
573
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Stores metadata about one layout item class.
virtual void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving)
Resolve paths in the item's properties (if there are any paths).
virtual ~QgsLayoutItemAbstractMetadata()=default
virtual QgsLayoutItem * createItem(QgsLayout *layout)=0
Creates a layout item of this class for a specified layout.
QString visibleName() const
Returns a translated, user visible name for the layout item class.
QgsLayoutItemAbstractMetadata(int type, const QString &visibleName, const QString &visiblePluralName=QString())
Constructor for QgsLayoutItemAbstractMetadata with the specified class type and visibleName.
int type() const
Returns the unique item type code for the layout item class.
QString visiblePluralName() const
Returns a translated, user visible name for plurals of the layout item class (e.g.
QgsLayoutItem * createItem(QgsLayout *layout) override
Creates a layout item of this class for a specified layout.
QgsLayoutItemPathResolverFunc pathResolverFunction() const
Returns the classes' path resolver function.
QgsLayoutItemCreateFunc createFunction() const
Returns the classes' item creation function.
void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) override
Resolve paths in the item's properties (if there are any paths).
QgsLayoutItemPathResolverFunc mPathResolverFunc
QgsLayoutItemCreateFunc mCreateFunc
QgsLayoutItemMetadata(int type, const QString &visibleName, const QString &visiblePluralName, const QgsLayoutItemCreateFunc &pfCreate, const QgsLayoutItemPathResolverFunc &pfPathResolver=nullptr)
Constructor for QgsLayoutItemMetadata with the specified class type and visibleName,...
Registry of available layout item types.
QgsLayoutItemRegistry(QObject *parent=nullptr)
Creates a new empty item registry.
void resolvePaths(int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) const
Resolve paths in properties of a particular symbol layer.
bool addLayoutItemType(QgsLayoutItemAbstractMetadata *metadata)
Registers a new layout item type.
bool removeLayoutMultiFrameType(int typeId)
Unregisters a layout multiframe type.
bool removeLayoutItemType(int typeId)
Unregisters a layout item type.
void multiFrameTypeRemoved(int type)
Emitted whenever an multiframe type is removed from the registry with the specified type.
void typeAdded(int type, const QString &name)
Emitted whenever a new item type is added to the registry, with the specified type and visible name.
QgsLayoutItemAbstractMetadata * itemMetadata(int type) const
Returns the metadata for the specified item type.
bool addLayoutMultiFrameType(QgsLayoutMultiFrameAbstractMetadata *metadata)
Registers a new layout multiframe type.
@ LayoutManualTable
Manual (fixed) table.
@ LayoutElevationProfile
Elevation profile item.
@ LayoutAttributeTable
Attribute table.
@ LayoutPolyline
Polyline shape item.
@ LayoutItem
Base class for items.
@ LayoutHtml
Html multiframe item.
@ PluginItem
Starting point for plugin item types.
@ LayoutTextTable
Preset text table.
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
@ LayoutPolygon
Polygon shape item.
QgsLayoutMultiFrameAbstractMetadata * multiFrameMetadata(int type) const
Returns the metadata for the specified multiframe type.
void multiFrameTypeAdded(int type, const QString &name)
Emitted whenever a new multiframe type is added to the registry, with the specified type and visible ...
QgsLayoutItem * createItem(int type, QgsLayout *layout) const
Creates a new instance of a layout item given the item type, and target layout.
void typeRemoved(int type)
Emitted whenever an item type is removed from the registry with the specified type.
QgsLayoutItemRegistry(const QgsLayoutItemRegistry &rh)=delete
QMap< int, QString > itemTypes() const
Returns a map of available item types to translated name.
QgsLayoutItemRegistry & operator=(const QgsLayoutItemRegistry &rh)=delete
QgsLayoutMultiFrame * createMultiFrame(int type, QgsLayout *layout) const
Creates a new instance of a layout multiframe given the multiframe type, and target layout.
Base class for graphical items within a QgsLayout.
Stores metadata about one layout multiframe class.
virtual ~QgsLayoutMultiFrameAbstractMetadata()=default
virtual QIcon icon() const
Returns an icon representing the layout multiframe type.
int type() const
Returns the unique item type code for the layout multiframe class.
QgsLayoutMultiFrameAbstractMetadata(int type, const QString &visibleName)
Constructor for QgsLayoutMultiFrameAbstractMetadata with the specified class type and visibleName.
virtual void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving)
Resolve paths in the item's properties (if there are any paths).
virtual QgsLayoutMultiFrame * createMultiFrame(QgsLayout *layout)=0
Creates a layout multiframe of this class for a specified layout.
QString visibleName() const
Returns a translated, user visible name for the layout multiframe class.
QgsLayoutMultiFramePathResolverFunc pathResolverFunction() const
Returns the classes' path resolver function.
QgsLayoutMultiFrameMetadata(int type, const QString &visibleName, const QgsLayoutMultiFrameCreateFunc &pfCreate, const QgsLayoutMultiFramePathResolverFunc &pfPathResolver=nullptr)
Constructor for QgsLayoutMultiFrameMetadata with the specified class type and visibleName,...
QgsLayoutMultiFrameCreateFunc createFunction() const
Returns the classes' multiframe creation function.
QgsLayoutMultiFrameCreateFunc mCreateFunc
void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) override
Resolve paths in the item's properties (if there are any paths).
QgsLayoutMultiFrame * createMultiFrame(QgsLayout *layout) override
Creates a layout multiframe of this class for a specified layout.
QgsLayoutMultiFramePathResolverFunc mPathResolverFunc
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
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
Resolves relative paths into absolute paths and vice versa.
#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< QgsLayoutMultiFrame *(QgsLayout *)> QgsLayoutMultiFrameCreateFunc
Layout multiframe creation function.
std::function< QgsLayoutItem *(QgsLayout *)> QgsLayoutItemCreateFunc
Layout item creation function.
std::function< void(QVariantMap &, const QgsPathResolver &, bool)> QgsLayoutMultiFramePathResolverFunc
Layout multiframe path resolver function.
std::function< void(QVariantMap &, const QgsPathResolver &, bool)> QgsLayoutItemPathResolverFunc
Layout item path resolver function.