QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
29 class QgsLayout;
30 class QgsLayoutView;
31 class QgsLayoutItem;
32 class QgsFillSymbol;
34 
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 
83  virtual QgsLayoutItem *createItem( QgsLayout *layout ) = 0 SIP_FACTORY;
84 
92  virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
93  {
94  Q_UNUSED( properties )
95  Q_UNUSED( pathResolver )
96  Q_UNUSED( saving )
97  }
98 
99  private:
100 
101  int mType = -1;
102  QString mVisibleName;
103  QString mVisibleNamePlural;
104 };
105 
108 
110 typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutItemPathResolverFunc SIP_SKIP;
111 
112 #ifndef SIP_RUN
113 
121 {
122  public:
123 
130  QgsLayoutItemMetadata( int type, const QString &visibleName, const QString &visiblePluralName,
131  const QgsLayoutItemCreateFunc &pfCreate,
132  const QgsLayoutItemPathResolverFunc &pfPathResolver = nullptr )
133  : QgsLayoutItemAbstractMetadata( type, visibleName, visiblePluralName )
134  , mCreateFunc( pfCreate )
135  , mPathResolverFunc( pfPathResolver )
136  {}
137 
141  QgsLayoutItemCreateFunc createFunction() const { return mCreateFunc; }
142 
146  QgsLayoutItemPathResolverFunc pathResolverFunction() const { return mPathResolverFunc; }
147 
148  QgsLayoutItem *createItem( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
149 
150  void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
151  {
152  if ( mPathResolverFunc )
153  mPathResolverFunc( properties, pathResolver, saving );
154  }
155 
156  protected:
157  QgsLayoutItemCreateFunc mCreateFunc = nullptr;
158  QgsLayoutItemPathResolverFunc mPathResolverFunc = nullptr;
159 
160 };
161 
162 #endif
163 
175 {
176  public:
177 
182  QgsLayoutMultiFrameAbstractMetadata( int type, const QString &visibleName )
183  : mType( type )
184  , mVisibleName( visibleName )
185  {}
186 
188 
192  int type() const { return mType; }
193 
197  virtual QIcon icon() const { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ); }
198 
202  QString visibleName() const { return mVisibleName; }
203 
208 
216  virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
217  {
218  Q_UNUSED( properties )
219  Q_UNUSED( pathResolver )
220  Q_UNUSED( saving )
221  }
222 
223  private:
224 
225  int mType = -1;
226  QString mVisibleName;
227 };
228 
231 
233 typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutMultiFramePathResolverFunc SIP_SKIP;
234 
235 #ifndef SIP_RUN
236 
244 {
245  public:
246 
251  QgsLayoutMultiFrameMetadata( int type, const QString &visibleName,
252  const QgsLayoutMultiFrameCreateFunc &pfCreate,
253  const QgsLayoutMultiFramePathResolverFunc &pfPathResolver = nullptr )
254  : QgsLayoutMultiFrameAbstractMetadata( type, visibleName )
255  , mCreateFunc( pfCreate )
256  , mPathResolverFunc( pfPathResolver )
257  {}
258 
262  QgsLayoutMultiFrameCreateFunc createFunction() const { return mCreateFunc; }
263 
267  QgsLayoutMultiFramePathResolverFunc pathResolverFunction() const { return mPathResolverFunc; }
268 
269  QgsLayoutMultiFrame *createMultiFrame( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
270 
271  void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
272  {
273  if ( mPathResolverFunc )
274  mPathResolverFunc( properties, pathResolver, saving );
275  }
276 
277  protected:
278  QgsLayoutMultiFrameCreateFunc mCreateFunc = nullptr;
279  QgsLayoutMultiFramePathResolverFunc mPathResolverFunc = nullptr;
280 
281 };
282 
283 #endif
284 
285 
299 class CORE_EXPORT QgsLayoutItemRegistry : public QObject
300 {
301  Q_OBJECT
302 
303  public:
304 
306  enum ItemType
307  {
308  LayoutItem = QGraphicsItem::UserType + 100,
310 
311  // known item types
312 
313  // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
314  // values, and must be updated if any additional types are added
315 
326 
327  // known multi-frame 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 
335 
337 
340 
341  // item types provided by plugins
342  PluginItem = LayoutTextTable + 10000,
343  };
344 
353  QgsLayoutItemRegistry( QObject *parent = nullptr );
354 
355  ~QgsLayoutItemRegistry() override;
356 
361  bool populate();
362 
367 
373  QgsLayoutItemAbstractMetadata *itemMetadata( int type ) const;
374 
380  QgsLayoutMultiFrameAbstractMetadata *multiFrameMetadata( int type ) const;
381 
386  bool addLayoutItemType( QgsLayoutItemAbstractMetadata *metadata SIP_TRANSFER );
387 
392  bool addLayoutMultiFrameType( QgsLayoutMultiFrameAbstractMetadata *metadata SIP_TRANSFER );
393 
398  QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_FACTORY;
399 
404  QgsLayoutMultiFrame *createMultiFrame( int type, QgsLayout *layout ) const SIP_FACTORY;
405 
411  void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
412 
416  QMap< int, QString> itemTypes() const;
417 
418  signals:
419 
424  void typeAdded( int type, const QString &name );
425 
430  void multiFrameTypeAdded( int type, const QString &name );
431 
432  private:
433 #ifdef SIP_RUN
435 #endif
436 
437  QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
438  QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
439 
440 };
441 
442 #if 0
443 #ifndef SIP_RUN
444 //simple item for testing
446 #ifdef ANDROID
447 // For some reason, the Android NDK toolchain requires this to link properly.
448 // Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
449 class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
450 #else
451 class TestLayoutItem : public QgsLayoutItem
452 #endif
453 {
454  Q_OBJECT
455 
456  public:
457 
458  TestLayoutItem( QgsLayout *layout );
459  ~TestLayoutItem() = default;
460 
461  //implement pure virtual methods
462  int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
463  void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
464 
465  private:
466  QColor mColor;
467  QgsFillSymbol *mShapeStyleSymbol = nullptr;
468 };
470 #endif
471 #endif
472 
473 #endif //QGSLAYOUTITEMREGISTRY_H
474 
475 
476 
QgsLayoutItemRegistry::LayoutPolygon
@ LayoutPolygon
Polygon shape item.
Definition: qgslayoutitemregistry.h:322
QgsLayoutMultiFrameAbstractMetadata::visibleName
QString visibleName() const
Returns a translated, user visible name for the layout multiframe class.
Definition: qgslayoutitemregistry.h:202
QgsLayoutMultiFrameAbstractMetadata::resolvePaths
virtual void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving)
Resolve paths in the item's properties (if there are any paths).
Definition: qgslayoutitemregistry.h:216
QgsLayoutItemMetadata::QgsLayoutItemMetadata
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,...
Definition: qgslayoutitemregistry.h:130
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsLayoutItemRegistry::ItemType
ItemType
Item types.
Definition: qgslayoutitemregistry.h:307
QgsLayoutItemAbstractMetadata::~QgsLayoutItemAbstractMetadata
virtual ~QgsLayoutItemAbstractMetadata()=default
QgsLayoutMultiFrameMetadata
Convenience metadata class that uses static functions to create layout multiframes and their configur...
Definition: qgslayoutitemregistry.h:244
QgsLayoutItemAbstractMetadata::visiblePluralName
QString visiblePluralName() const
Returns a translated, user visible name for plurals of the layout item class (e.g.
Definition: qgslayoutitemregistry.h:78
QgsLayoutItemMetadata::resolvePaths
void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) override
Resolve paths in the item's properties (if there are any paths).
Definition: qgslayoutitemregistry.h:150
qgspathresolver.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsLayoutItemAbstractMetadata::resolvePaths
virtual void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving)
Resolve paths in the item's properties (if there are any paths).
Definition: qgslayoutitemregistry.h:92
QgsLayoutItemCreateFunc
std::function< QgsLayoutItem *(QgsLayout *)> QgsLayoutItemCreateFunc
Layout item creation function.
Definition: qgslayoutitemregistry.h:107
QgsLayoutMultiFrameMetadata::pathResolverFunction
QgsLayoutMultiFramePathResolverFunc pathResolverFunction() const
Returns the classes' path resolver function.
Definition: qgslayoutitemregistry.h:267
QgsLayoutMultiFrame
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Definition: qgslayoutmultiframe.h:49
QgsLayoutItemAbstractMetadata::type
int type() const
Returns the unique item type code for the layout item class.
Definition: qgslayoutitemregistry.h:66
QgsLayoutItemMetadata
Convenience metadata class that uses static functions to create layout items and their configuration ...
Definition: qgslayoutitemregistry.h:121
QgsLayoutItemRegistry::typeAdded
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.
QgsLayoutItemRegistry::operator=
QgsLayoutItemRegistry & operator=(const QgsLayoutItemRegistry &rh)=delete
QgsLayoutItemRegistry cannot be copied.
QgsLayoutItem::draw
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayoutItemRegistry::LayoutGroup
@ LayoutGroup
Grouped item.
Definition: qgslayoutitemregistry.h:309
QgsLayoutItem::type
int type() const override
Returns a unique graphics item type identifier.
Definition: qgslayoutitem.cpp:124
qgsapplication.h
QgsLayoutItemAbstractMetadata
Stores metadata about one layout item class.
Definition: qgslayoutitemregistry.h:46
QgsLayoutMultiFrameMetadata::resolvePaths
void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) override
Resolve paths in the item's properties (if there are any paths).
Definition: qgslayoutitemregistry.h:271
QgsLayoutMultiFramePathResolverFunc
std::function< void(QVariantMap &, const QgsPathResolver &, bool)> QgsLayoutMultiFramePathResolverFunc
Layout multiframe path resolver function.
Definition: qgslayoutitemregistry.h:233
QgsLayoutMultiFrameAbstractMetadata
Stores metadata about one layout multiframe class.
Definition: qgslayoutitemregistry.h:175
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayoutMultiFrameMetadata::createMultiFrame
QgsLayoutMultiFrame * createMultiFrame(QgsLayout *layout) override
Creates a layout multiframe of this class for a specified layout.
Definition: qgslayoutitemregistry.h:269
qgslayoutitem.h
QgsLayoutItemPathResolverFunc
std::function< void(QVariantMap &, const QgsPathResolver &, bool)> QgsLayoutItemPathResolverFunc
Layout item path resolver function.
Definition: qgslayoutitemregistry.h:110
QgsLayoutItemRegistry::LayoutPolyline
@ LayoutPolyline
Polyline shape item.
Definition: qgslayoutitemregistry.h:323
qgis_sip.h
QgsLayoutMultiFrameAbstractMetadata::createMultiFrame
virtual QgsLayoutMultiFrame * createMultiFrame(QgsLayout *layout)=0
Creates a layout multiframe of this class for a specified layout.
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsLayoutMultiFrameMetadata::QgsLayoutMultiFrameMetadata
QgsLayoutMultiFrameMetadata(int type, const QString &visibleName, const QgsLayoutMultiFrameCreateFunc &pfCreate, const QgsLayoutMultiFramePathResolverFunc &pfPathResolver=nullptr)
Constructor for QgsLayoutMultiFrameMetadata with the specified class type and visibleName,...
Definition: qgslayoutitemregistry.h:251
QgsLayoutItemMetadata::createItem
QgsLayoutItem * createItem(QgsLayout *layout) override
Creates a layout item of this class for a specified layout.
Definition: qgslayoutitemregistry.h:148
QgsLayoutItemRegistry::Layout3DMap
@ Layout3DMap
3D map item
Definition: qgslayoutitemregistry.h:336
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
QgsLayoutItemAbstractMetadata::QgsLayoutItemAbstractMetadata
QgsLayoutItemAbstractMetadata(int type, const QString &visibleName, const QString &visiblePluralName=QString())
Constructor for QgsLayoutItemAbstractMetadata with the specified class type and visibleName.
Definition: qgslayoutitemregistry.h:55
QgsLayoutItemRegistry::LayoutScaleBar
@ LayoutScaleBar
Scale bar item.
Definition: qgslayoutitemregistry.h:324
QgsLayoutItemRegistry::LayoutLabel
@ LayoutLabel
Label item.
Definition: qgslayoutitemregistry.h:319
QgsLayoutMultiFrameAbstractMetadata::QgsLayoutMultiFrameAbstractMetadata
QgsLayoutMultiFrameAbstractMetadata(int type, const QString &visibleName)
Constructor for QgsLayoutMultiFrameAbstractMetadata with the specified class type and visibleName.
Definition: qgslayoutitemregistry.h:182
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutMultiFrameAbstractMetadata::icon
virtual QIcon icon() const
Returns an icon representing the layout multiframe type.
Definition: qgslayoutitemregistry.h:197
QgsLayoutItemMetadata::pathResolverFunction
QgsLayoutItemPathResolverFunc pathResolverFunction() const
Returns the classes' path resolver function.
Definition: qgslayoutitemregistry.h:146
QgsLayoutItemRegistry::LayoutPicture
@ LayoutPicture
Picture item.
Definition: qgslayoutitemregistry.h:318
QgsLayoutItemRegistry::LayoutMarker
@ LayoutMarker
Marker item.
Definition: qgslayoutitemregistry.h:339
QgsFillSymbol
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgssymbol.h:1234
QgsLayoutItemRegistry::LayoutMap
@ LayoutMap
Map item.
Definition: qgslayoutitemregistry.h:317
QgsLayoutView
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
QgsLayoutItemRegistry::LayoutTextTable
@ LayoutTextTable
Preset text table.
Definition: qgslayoutitemregistry.h:334
QgsLayoutMultiFrameCreateFunc
std::function< QgsLayoutMultiFrame *(QgsLayout *)> QgsLayoutMultiFrameCreateFunc
Layout multiframe creation function.
Definition: qgslayoutitemregistry.h:230
QgsLayoutItemRegistry::LayoutHtml
@ LayoutHtml
Html multiframe item.
Definition: qgslayoutitemregistry.h:332
QgsLayoutItemAbstractMetadata::createItem
virtual QgsLayoutItem * createItem(QgsLayout *layout)=0
Creates a layout item of this class for a specified layout.
QgsLayoutItemRegistry
Registry of available layout item types.
Definition: qgslayoutitemregistry.h:300
QgsLayoutMultiFrameMetadata::createFunction
QgsLayoutMultiFrameCreateFunc createFunction() const
Returns the classes' multiframe creation function.
Definition: qgslayoutitemregistry.h:262
QgsLayoutItemRegistry::LayoutFrame
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
Definition: qgslayoutitemregistry.h:325
QgsLayoutItemRegistry::LayoutShape
@ LayoutShape
Shape item.
Definition: qgslayoutitemregistry.h:321
QgsLayoutItemAbstractMetadata::visibleName
QString visibleName() const
Returns a translated, user visible name for the layout item class.
Definition: qgslayoutitemregistry.h:72
QgsLayoutItemRegistry::LayoutItem
@ LayoutItem
Base class for items.
Definition: qgslayoutitemregistry.h:308
QgsLayoutItemRegistry::LayoutAttributeTable
@ LayoutAttributeTable
Attribute table.
Definition: qgslayoutitemregistry.h:333
QgsLayoutItemRegistry::QgsLayoutItemRegistry
QgsLayoutItemRegistry(const QgsLayoutItemRegistry &rh)=delete
QgsLayoutItemRegistry cannot be copied.
QgsLayoutItemRegistry::LayoutManualTable
@ LayoutManualTable
Manual (fixed) table.
Definition: qgslayoutitemregistry.h:338
QgsLayoutMultiFrameAbstractMetadata::~QgsLayoutMultiFrameAbstractMetadata
virtual ~QgsLayoutMultiFrameAbstractMetadata()=default
QgsLayoutItemRegistry::LayoutLegend
@ LayoutLegend
Legend item.
Definition: qgslayoutitemregistry.h:320
QgsPathResolver
Resolves relative paths into absolute paths and vice versa.
Definition: qgspathresolver.h:32
QgsLayoutItemRegistry::LayoutPage
@ LayoutPage
Page items.
Definition: qgslayoutitemregistry.h:316
QgsLayoutItemRegistry::multiFrameTypeAdded
void multiFrameTypeAdded(int type, const QString &name)
Emitted whenever a new multiframe type is added to the registry, with the specified type and visible ...
QgsLayoutItemMetadata::createFunction
QgsLayoutItemCreateFunc createFunction() const
Returns the classes' item creation function.
Definition: qgslayoutitemregistry.h:141
QgsLayoutMultiFrameAbstractMetadata::type
int type() const
Returns the unique item type code for the layout multiframe class.
Definition: qgslayoutitemregistry.h:192