QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
30class QgsLayout;
31class QgsLayoutView;
32class QgsLayoutItem;
33class QgsFillSymbol;
35
46{
47 public:
48
55 QgsLayoutItemAbstractMetadata( int type, const QString &visibleName, const QString &visiblePluralName = QString() )
56 : mType( type )
57 , mVisibleName( visibleName )
58 , mVisibleNamePlural( visiblePluralName.isEmpty() ? visibleName : visiblePluralName )
59 {}
60
61 virtual ~QgsLayoutItemAbstractMetadata() = default;
62
66 int type() const { return mType; }
67
72 QString visibleName() const { return mVisibleName; }
73
78 QString visiblePluralName() const { return mVisibleNamePlural; }
79
80 /*
81 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
82 * the case.
83 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
84 *
85 * "
86 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
87 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
88 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
89 * by Python so the /Factory/ on create() would be correct.)
90 *
91 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
92 * "
93 */
94
99
107 virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
108 {
109 Q_UNUSED( properties )
110 Q_UNUSED( pathResolver )
111 Q_UNUSED( saving )
112 }
113
114 private:
115
116 int mType = -1;
117 QString mVisibleName;
118 QString mVisibleNamePlural;
119};
120
123
125typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutItemPathResolverFunc SIP_SKIP;
126
127#ifndef SIP_RUN
128
135{
136 public:
137
144 QgsLayoutItemMetadata( int type, const QString &visibleName, const QString &visiblePluralName,
145 const QgsLayoutItemCreateFunc &pfCreate,
146 const QgsLayoutItemPathResolverFunc &pfPathResolver = nullptr )
148 , mCreateFunc( pfCreate )
149 , mPathResolverFunc( pfPathResolver )
150 {}
151
156
161
162 QgsLayoutItem *createItem( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
163
164 void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
165 {
166 if ( mPathResolverFunc )
167 mPathResolverFunc( properties, pathResolver, saving );
168 }
169
170 protected:
173
174};
175
176#endif
177
188{
189 public:
190
196 : mType( type )
197 , mVisibleName( visibleName )
198 {}
199
201
205 int type() const { return mType; }
206
210 virtual QIcon icon() const { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ); }
211
215 QString visibleName() const { return mVisibleName; }
216
217 /*
218 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
219 * the case.
220 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
221 *
222 * "
223 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
224 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
225 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
226 * by Python so the /Factory/ on create() would be correct.)
227 *
228 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
229 * "
230 */
231
236
244 virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
245 {
246 Q_UNUSED( properties )
247 Q_UNUSED( pathResolver )
248 Q_UNUSED( saving )
249 }
250
251 private:
252
253 int mType = -1;
254 QString mVisibleName;
255};
256
259
261typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutMultiFramePathResolverFunc SIP_SKIP;
262
263#ifndef SIP_RUN
264
271{
272 public:
273
279 const QgsLayoutMultiFrameCreateFunc &pfCreate,
280 const QgsLayoutMultiFramePathResolverFunc &pfPathResolver = nullptr )
282 , mCreateFunc( pfCreate )
283 , mPathResolverFunc( pfPathResolver )
284 {}
285
290
295
296 QgsLayoutMultiFrame *createMultiFrame( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
297
298 void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
299 {
300 if ( mPathResolverFunc )
301 mPathResolverFunc( properties, pathResolver, saving );
302 }
303
304 protected:
307
308};
309
310#endif
311
312
325class CORE_EXPORT QgsLayoutItemRegistry : public QObject
326{
327 Q_OBJECT
328
329 public:
330
333 {
334 LayoutItem = QGraphicsItem::UserType + 100,
336
337 // known item types
338
339 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
340 // values, and must be updated if any additional types are added
341
352
353 // known multi-frame types
354
355 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
356 // values, and must be updated if any additional types are added
357
361
363
366
368
370
371 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
372 // values, and must be updated if any additional types are added
373
374 // item types provided by plugins
376 };
377
386 QgsLayoutItemRegistry( QObject *parent = nullptr );
387
388 ~QgsLayoutItemRegistry() override;
389
394 bool populate();
395
398
405
412
413 /*
414 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
415 * the case.
416 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
417 *
418 * "
419 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
420 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
421 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
422 * by Python so the /Factory/ on create() would be correct.)
423 *
424 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
425 * "
426 */
427
433
439 bool removeLayoutItemType( int typeId );
440
449
455 bool removeLayoutMultiFrameType( int typeId );
456
465
471
476 QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_TRANSFERBACK;
477
483
489 void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
490
494 QMap< int, QString> itemTypes() const;
495
496 signals:
497
502 void typeAdded( int type, const QString &name );
503
510 void typeRemoved( int type );
511
516 void multiFrameTypeAdded( int type, const QString &name );
517
524 void multiFrameTypeRemoved( int type );
525
526 private:
527#ifdef SIP_RUN
529#endif
530
531 QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
532 QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
533
534};
535
536#if 0
537#ifndef SIP_RUN
539//simple item for testing
540#ifdef ANDROID
541// For some reason, the Android NDK toolchain requires this to link properly.
542// Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
543class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
544#else
545class TestLayoutItem : public QgsLayoutItem
546#endif
547{
548 Q_OBJECT
549
550 public:
551
552 TestLayoutItem( QgsLayout *layout );
553 ~TestLayoutItem() = default;
554
555 //implement pure virtual methods
556 int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
557 void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
558
559 private:
560 QColor mColor;
561 QgsFillSymbol *mShapeStyleSymbol = nullptr;
562};
564#endif
565#endif
566
567#endif //QGSLAYOUTITEMREGISTRY_H
568
569
570
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.