QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 "qgis_core.h"
20#include "qgis_sip.h"
21#include "qgsapplication.h"
22#include "qgspathresolver.h"
23#include <QGraphicsItem> //for QGraphicsItem::UserType
24#include <QIcon>
25#include <functional>
26
27#include "qgslayoutitem.h" // temporary
28
29class QgsLayout;
30class QgsLayoutView;
31class QgsLayoutItem;
32class QgsFillSymbol;
34
45{
46 public:
47
54 QgsLayoutItemAbstractMetadata( int type, const QString &visibleName, const QString &visiblePluralName = QString() )
55 : mType( type )
56 , mVisibleName( visibleName )
57 , mVisibleNamePlural( visiblePluralName.isEmpty() ? visibleName : visiblePluralName )
58 {}
59
60 virtual ~QgsLayoutItemAbstractMetadata() = default;
61
65 int type() const { return mType; }
66
71 QString visibleName() const { return mVisibleName; }
72
77 QString visiblePluralName() const { return mVisibleNamePlural; }
78
79 /*
80 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
81 * the case.
82 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
83 *
84 * "
85 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
86 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
87 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
88 * by Python so the /Factory/ on create() would be correct.)
89 *
90 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
91 * "
92 */
93
98
106 virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
107 {
108 Q_UNUSED( properties )
109 Q_UNUSED( pathResolver )
110 Q_UNUSED( saving )
111 }
112
113 private:
114
115 int mType = -1;
116 QString mVisibleName;
117 QString mVisibleNamePlural;
118};
119
122
124typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutItemPathResolverFunc SIP_SKIP;
125
126#ifndef SIP_RUN
127
134{
135 public:
136
143 QgsLayoutItemMetadata( int type, const QString &visibleName, const QString &visiblePluralName,
144 const QgsLayoutItemCreateFunc &pfCreate,
145 const QgsLayoutItemPathResolverFunc &pfPathResolver = nullptr )
146 : QgsLayoutItemAbstractMetadata( type, visibleName, visiblePluralName )
147 , mCreateFunc( pfCreate )
148 , mPathResolverFunc( pfPathResolver )
149 {}
150
154 QgsLayoutItemCreateFunc createFunction() const { return mCreateFunc; }
155
159 QgsLayoutItemPathResolverFunc pathResolverFunction() const { return mPathResolverFunc; }
160
161 QgsLayoutItem *createItem( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
162
163 void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
164 {
165 if ( mPathResolverFunc )
166 mPathResolverFunc( properties, pathResolver, saving );
167 }
168
169 protected:
170 QgsLayoutItemCreateFunc mCreateFunc = nullptr;
171 QgsLayoutItemPathResolverFunc mPathResolverFunc = nullptr;
172
173};
174
175#endif
176
187{
188 public:
189
194 QgsLayoutMultiFrameAbstractMetadata( int type, const QString &visibleName )
195 : mType( type )
196 , mVisibleName( visibleName )
197 {}
198
200
204 int type() const { return mType; }
205
209 virtual QIcon icon() const { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ); }
210
214 QString visibleName() const { return mVisibleName; }
215
216 /*
217 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
218 * the case.
219 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
220 *
221 * "
222 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
223 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
224 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
225 * by Python so the /Factory/ on create() would be correct.)
226 *
227 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
228 * "
229 */
230
235
243 virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
244 {
245 Q_UNUSED( properties )
246 Q_UNUSED( pathResolver )
247 Q_UNUSED( saving )
248 }
249
250 private:
251
252 int mType = -1;
253 QString mVisibleName;
254};
255
258
260typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutMultiFramePathResolverFunc SIP_SKIP;
261
262#ifndef SIP_RUN
263
270{
271 public:
272
277 QgsLayoutMultiFrameMetadata( int type, const QString &visibleName,
278 const QgsLayoutMultiFrameCreateFunc &pfCreate,
279 const QgsLayoutMultiFramePathResolverFunc &pfPathResolver = nullptr )
280 : QgsLayoutMultiFrameAbstractMetadata( type, visibleName )
281 , mCreateFunc( pfCreate )
282 , mPathResolverFunc( pfPathResolver )
283 {}
284
288 QgsLayoutMultiFrameCreateFunc createFunction() const { return mCreateFunc; }
289
293 QgsLayoutMultiFramePathResolverFunc pathResolverFunction() const { return mPathResolverFunc; }
294
295 QgsLayoutMultiFrame *createMultiFrame( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
296
297 void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
298 {
299 if ( mPathResolverFunc )
300 mPathResolverFunc( properties, pathResolver, saving );
301 }
302
303 protected:
304 QgsLayoutMultiFrameCreateFunc mCreateFunc = nullptr;
305 QgsLayoutMultiFramePathResolverFunc mPathResolverFunc = nullptr;
306
307};
308
309#endif
310
311
324class CORE_EXPORT QgsLayoutItemRegistry : public QObject
325{
326 Q_OBJECT
327
328 public:
329
332 {
333 LayoutItem = QGraphicsItem::UserType + 100,
335
336 // known item types
337
338 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
339 // values, and must be updated if any additional types are added
340
351
352 // known multi-frame types
353
354 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
355 // values, and must be updated if any additional types are added
356
360
362
365
367
368 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
369 // values, and must be updated if any additional types are added
370
371 // item types provided by plugins
372 PluginItem = LayoutTextTable + 10000,
373 };
374
383 QgsLayoutItemRegistry( QObject *parent = nullptr );
384
385 ~QgsLayoutItemRegistry() override;
386
391 bool populate();
392
397
403 QgsLayoutItemAbstractMetadata *itemMetadata( int type ) const;
404
410 QgsLayoutMultiFrameAbstractMetadata *multiFrameMetadata( int type ) const;
411
412 /*
413 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
414 * the case.
415 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
416 *
417 * "
418 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
419 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
420 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
421 * by Python so the /Factory/ on create() would be correct.)
422 *
423 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
424 * "
425 */
426
431 bool addLayoutItemType( QgsLayoutItemAbstractMetadata *metadata SIP_TRANSFER );
432
437 bool addLayoutMultiFrameType( QgsLayoutMultiFrameAbstractMetadata *metadata SIP_TRANSFER );
438
443 QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_TRANSFERBACK;
444
449 QgsLayoutMultiFrame *createMultiFrame( int type, QgsLayout *layout ) const SIP_TRANSFERBACK;
450
456 void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
457
461 QMap< int, QString> itemTypes() const;
462
463 signals:
464
469 void typeAdded( int type, const QString &name );
470
475 void multiFrameTypeAdded( int type, const QString &name );
476
477 private:
478#ifdef SIP_RUN
480#endif
481
482 QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
483 QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
484
485};
486
487#if 0
488#ifndef SIP_RUN
490//simple item for testing
491#ifdef ANDROID
492// For some reason, the Android NDK toolchain requires this to link properly.
493// Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
494class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
495#else
496class TestLayoutItem : public QgsLayoutItem
497#endif
498{
499 Q_OBJECT
500
501 public:
502
503 TestLayoutItem( QgsLayout *layout );
504 ~TestLayoutItem() = default;
505
506 //implement pure virtual methods
507 int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
508 void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
509
510 private:
511 QColor mColor;
512 QgsFillSymbol *mShapeStyleSymbol = nullptr;
513};
515#endif
516#endif
517
518#endif //QGSLAYOUTITEMREGISTRY_H
519
520
521
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.
Definition: qgsfillsymbol.h:30
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.
Convenience metadata class that uses static functions to create layout items and their configuration ...
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).
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.
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.
@ LayoutManualTable
Manual (fixed) table.
@ LayoutElevationProfile
Elevation profile item (since QGIS 3.30)
@ LayoutAttributeTable
Attribute table.
@ LayoutPolyline
Polyline shape item.
@ LayoutScaleBar
Scale bar item.
@ LayoutItem
Base class for items.
@ LayoutHtml
Html multiframe item.
@ LayoutTextTable
Preset text table.
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
@ LayoutPolygon
Polygon shape item.
void multiFrameTypeAdded(int type, const QString &name)
Emitted whenever a new multiframe type is added to the registry, with the specified type and visible ...
QgsLayoutItemRegistry(const QgsLayoutItemRegistry &rh)=delete
QgsLayoutItemRegistry cannot be copied.
QgsLayoutItemRegistry & operator=(const QgsLayoutItemRegistry &rh)=delete
QgsLayoutItemRegistry cannot be copied.
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.
Convenience metadata class that uses static functions to create layout multiframes and their configur...
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.
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.
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.
Definition: qgslayoutview.h:50
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
Resolves relative paths into absolute paths and vice versa.
Contains information about the context of a rendering operation.
#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< 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.