QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
57 QgsLayoutItemAbstractMetadata( int type, const QString &visibleName, const QString &visiblePluralName = QString() )
58 : mType( type )
59 , mVisibleName( visibleName )
60 , mVisibleNamePlural( visiblePluralName.isEmpty() ? visibleName : visiblePluralName )
61 {}
62
63 virtual ~QgsLayoutItemAbstractMetadata() = default;
64
68 int type() const { return mType; }
69
74 QString visibleName() const { return mVisibleName; }
75
80 QString visiblePluralName() const { return mVisibleNamePlural; }
81
82 /*
83 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
84 * the case.
85 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
86 *
87 * "
88 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
89 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
90 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
91 * by Python so the /Factory/ on create() would be correct.)
92 *
93 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
94 * "
95 */
96
101
109 virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
110 {
111 Q_UNUSED( properties )
112 Q_UNUSED( pathResolver )
113 Q_UNUSED( saving )
114 }
115
116 private:
117 int mType = -1;
118 QString mVisibleName;
119 QString mVisibleNamePlural;
120};
121
124
126typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutItemPathResolverFunc SIP_SKIP;
127
128#ifndef SIP_RUN
129
136{
137 public:
144 QgsLayoutItemMetadata( int type, const QString &visibleName, const QString &visiblePluralName, const QgsLayoutItemCreateFunc &pfCreate, const QgsLayoutItemPathResolverFunc &pfPathResolver = nullptr )
146 , mCreateFunc( pfCreate )
147 , mPathResolverFunc( pfPathResolver )
148 {}
149
154
159
160 QgsLayoutItem *createItem( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
161
162 void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
163 {
164 if ( mPathResolverFunc )
165 mPathResolverFunc( properties, pathResolver, saving );
166 }
167
168 protected:
171};
172
173#endif
174
185{
186 public:
192 : mType( type )
193 , mVisibleName( visibleName )
194 {}
195
197
201 int type() const { return mType; }
202
206 virtual QIcon icon() const { return QgsApplication::getThemeIcon( u"/mActionAddBasicRectangle.svg"_s ); }
207
211 QString visibleName() const { return mVisibleName; }
212
213 /*
214 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
215 * the case.
216 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
217 *
218 * "
219 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
220 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
221 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
222 * by Python so the /Factory/ on create() would be correct.)
223 *
224 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
225 * "
226 */
227
232
240 virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
241 {
242 Q_UNUSED( properties )
243 Q_UNUSED( pathResolver )
244 Q_UNUSED( saving )
245 }
246
247 private:
248 int mType = -1;
249 QString mVisibleName;
250};
251
254
256typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutMultiFramePathResolverFunc SIP_SKIP;
257
258#ifndef SIP_RUN
259
266{
267 public:
272 QgsLayoutMultiFrameMetadata( int type, const QString &visibleName, const QgsLayoutMultiFrameCreateFunc &pfCreate, const QgsLayoutMultiFramePathResolverFunc &pfPathResolver = nullptr )
274 , mCreateFunc( pfCreate )
275 , mPathResolverFunc( pfPathResolver )
276 {}
277
282
287
288 QgsLayoutMultiFrame *createMultiFrame( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
289
290 void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
291 {
292 if ( mPathResolverFunc )
293 mPathResolverFunc( properties, pathResolver, saving );
294 }
295
296 protected:
299};
300
301#endif
302
303
316class CORE_EXPORT QgsLayoutItemRegistry : public QObject
317{
318 Q_OBJECT
319
320 public:
323 {
324 LayoutItem = QGraphicsItem::UserType + 100,
326
327 // known item types
328
329 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
330 // values, and must be updated if any additional types are added
331
342
343 // known multi-frame types
344
345 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
346 // values, and must be updated if any additional types are added
347
351
353
356
358
360
361 // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
362 // values, and must be updated if any additional types are added
363
364 // item types provided by plugins
366 };
367
376 QgsLayoutItemRegistry( QObject *parent = nullptr );
377
378 ~QgsLayoutItemRegistry() override;
379
384 bool populate();
385
388
395
402
403 /*
404 * IMPORTANT: While it seems like /Factory/ would be the correct annotations here, that's not
405 * the case.
406 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
407 *
408 * "
409 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
410 * In this case it isn't because it has already been seen when being returned by QgsProcessingAlgorithm::createInstance()
411 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
412 * by Python so the /Factory/ on create() would be correct.)
413 *
414 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
415 * "
416 */
417
423
429 bool removeLayoutItemType( int typeId );
430
439
445 bool removeLayoutMultiFrameType( int typeId );
446
455
461
466 QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_TRANSFERBACK;
467
473
479 void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
480
484 QMap< int, QString> itemTypes() const;
485
486 signals:
487
492 void typeAdded( int type, const QString &name );
493
500 void typeRemoved( int type );
501
506 void multiFrameTypeAdded( int type, const QString &name );
507
514 void multiFrameTypeRemoved( int type );
515
516 private:
517#ifdef SIP_RUN
519#endif
520
521 QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
522 QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
523};
524
525#if 0
526#ifndef SIP_RUN
528//simple item for testing
529#ifdef ANDROID
530// For some reason, the Android NDK toolchain requires this to link properly.
531// Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
532class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
533#else
534class TestLayoutItem : public QgsLayoutItem
535#endif
536{
537 Q_OBJECT
538
539 public:
540
541 TestLayoutItem( QgsLayout *layout );
542 ~TestLayoutItem() = default;
543
544 //implement pure virtual methods
545 int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
546 void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
547
548 private:
549 QColor mColor;
550 QgsFillSymbol *mShapeStyleSymbol = nullptr;
551};
553#endif
554#endif
555
556#endif //QGSLAYOUTITEMREGISTRY_H
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:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_TRANSFERBACK
Definition qgis_sip.h:47
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.