QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
53  QgsLayoutItemAbstractMetadata( int type, const QString &visibleName )
54  : mType( type )
55  , mVisibleName( visibleName )
56  {}
57 
58  virtual ~QgsLayoutItemAbstractMetadata() = default;
59 
63  int type() const { return mType; }
64 
68  QString visibleName() const { return mVisibleName; }
69 
73  virtual QgsLayoutItem *createItem( QgsLayout *layout ) = 0 SIP_FACTORY;
74 
82  virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
83  {
84  Q_UNUSED( properties );
85  Q_UNUSED( pathResolver );
86  Q_UNUSED( saving );
87  }
88 
89  private:
90 
91  int mType = -1;
92  QString mVisibleName;
93 };
94 
96 typedef std::function<QgsLayoutItem *( QgsLayout * )> QgsLayoutItemCreateFunc SIP_SKIP;
97 
99 typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutItemPathResolverFunc SIP_SKIP;
100 
101 #ifndef SIP_RUN
102 
110 {
111  public:
112 
117  QgsLayoutItemMetadata( int type, const QString &visibleName,
118  const QgsLayoutItemCreateFunc &pfCreate,
119  const QgsLayoutItemPathResolverFunc &pfPathResolver = nullptr )
120  : QgsLayoutItemAbstractMetadata( type, visibleName )
121  , mCreateFunc( pfCreate )
122  , mPathResolverFunc( pfPathResolver )
123  {}
124 
128  QgsLayoutItemCreateFunc createFunction() const { return mCreateFunc; }
129 
133  QgsLayoutItemPathResolverFunc pathResolverFunction() const { return mPathResolverFunc; }
134 
135  QgsLayoutItem *createItem( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
136 
137  void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
138  {
139  if ( mPathResolverFunc )
140  mPathResolverFunc( properties, pathResolver, saving );
141  }
142 
143  protected:
144  QgsLayoutItemCreateFunc mCreateFunc = nullptr;
145  QgsLayoutItemPathResolverFunc mPathResolverFunc = nullptr;
146 
147 };
148 
149 #endif
150 
162 {
163  public:
164 
169  QgsLayoutMultiFrameAbstractMetadata( int type, const QString &visibleName )
170  : mType( type )
171  , mVisibleName( visibleName )
172  {}
173 
174  virtual ~QgsLayoutMultiFrameAbstractMetadata() = default;
175 
179  int type() const { return mType; }
180 
184  virtual QIcon icon() const { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ); }
185 
189  QString visibleName() const { return mVisibleName; }
190 
194  virtual QgsLayoutMultiFrame *createMultiFrame( QgsLayout *layout ) = 0 SIP_FACTORY;
195 
203  virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
204  {
205  Q_UNUSED( properties );
206  Q_UNUSED( pathResolver );
207  Q_UNUSED( saving );
208  }
209 
210  private:
211 
212  int mType = -1;
213  QString mVisibleName;
214 };
215 
217 typedef std::function<QgsLayoutMultiFrame *( QgsLayout * )> QgsLayoutMultiFrameCreateFunc SIP_SKIP;
218 
220 typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutMultiFramePathResolverFunc SIP_SKIP;
221 
222 #ifndef SIP_RUN
223 
231 {
232  public:
233 
238  QgsLayoutMultiFrameMetadata( int type, const QString &visibleName,
239  const QgsLayoutMultiFrameCreateFunc &pfCreate,
240  const QgsLayoutMultiFramePathResolverFunc &pfPathResolver = nullptr )
241  : QgsLayoutMultiFrameAbstractMetadata( type, visibleName )
242  , mCreateFunc( pfCreate )
243  , mPathResolverFunc( pfPathResolver )
244  {}
245 
249  QgsLayoutMultiFrameCreateFunc createFunction() const { return mCreateFunc; }
250 
254  QgsLayoutMultiFramePathResolverFunc pathResolverFunction() const { return mPathResolverFunc; }
255 
256  QgsLayoutMultiFrame *createMultiFrame( QgsLayout *layout ) override { return mCreateFunc ? mCreateFunc( layout ) : nullptr; }
257 
258  void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override
259  {
260  if ( mPathResolverFunc )
261  mPathResolverFunc( properties, pathResolver, saving );
262  }
263 
264  protected:
265  QgsLayoutMultiFrameCreateFunc mCreateFunc = nullptr;
266  QgsLayoutMultiFramePathResolverFunc mPathResolverFunc = nullptr;
267 
268 };
269 
270 #endif
271 
272 
286 class CORE_EXPORT QgsLayoutItemRegistry : public QObject
287 {
288  Q_OBJECT
289 
290  public:
291 
293  enum ItemType
294  {
295  LayoutItem = QGraphicsItem::UserType + 100,
297 
298  // known item types
299 
300  // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
301  // values, and must be updated if any additional types are added
302 
313 
314  // known multi-frame types
315 
316  // WARNING!!!! SIP CASTING OF QgsLayoutItem and QgsLayoutMultiFrame DEPENDS on these
317  // values, and must be updated if any additional types are added
318 
322 
324 
325  // item types provided by plugins
326  PluginItem = LayoutTextTable + 10000,
327  };
328 
337  QgsLayoutItemRegistry( QObject *parent = nullptr );
338 
339  ~QgsLayoutItemRegistry() override;
340 
345  bool populate();
346 
348  QgsLayoutItemRegistry( const QgsLayoutItemRegistry &rh ) = delete;
350  QgsLayoutItemRegistry &operator=( const QgsLayoutItemRegistry &rh ) = delete;
351 
357  QgsLayoutItemAbstractMetadata *itemMetadata( int type ) const;
358 
364  QgsLayoutMultiFrameAbstractMetadata *multiFrameMetadata( int type ) const;
365 
370  bool addLayoutItemType( QgsLayoutItemAbstractMetadata *metadata SIP_TRANSFER );
371 
376  bool addLayoutMultiFrameType( QgsLayoutMultiFrameAbstractMetadata *metadata SIP_TRANSFER );
377 
382  QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_FACTORY;
383 
388  QgsLayoutMultiFrame *createMultiFrame( int type, QgsLayout *layout ) const SIP_FACTORY;
389 
395  void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
396 
400  QMap< int, QString> itemTypes() const;
401 
402  signals:
403 
408  void typeAdded( int type, const QString &name );
409 
414  void multiFrameTypeAdded( int type, const QString &name );
415 
416  private:
417 #ifdef SIP_RUN
419 #endif
420 
421  QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
422  QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
423 
424 };
425 
426 #if 0
427 #ifndef SIP_RUN
428 //simple item for testing
430 #ifdef ANDROID
431 // For some reason, the Android NDK toolchain requires this to link properly.
432 // Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
433 class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
434 #else
435 class TestLayoutItem : public QgsLayoutItem
436 #endif
437 {
438  Q_OBJECT
439 
440  public:
441 
442  TestLayoutItem( QgsLayout *layout );
443  ~TestLayoutItem() = default;
444 
445  //implement pure virtual methods
446  int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
447  void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
448 
449  private:
450  QColor mColor;
451  QgsFillSymbol *mShapeStyleSymbol = nullptr;
452 };
454 #endif
455 #endif
456 
457 #endif //QGSLAYOUTITEMREGISTRY_H
458 
459 
460 
virtual QIcon icon() const
Returns an icon representing the layout multiframe type.
QgsLayoutItemCreateFunc createFunction() const
Returns the classes&#39; item creation function.
QgsLayoutItemMetadata(int type, const QString &visibleName, const QgsLayoutItemCreateFunc &pfCreate, const QgsLayoutItemPathResolverFunc &pfPathResolver=nullptr)
Constructor for QgsLayoutItemMetadata with the specified class type and visibleName, and function pointers for the various item creation functions.
Base class for graphical items within a QgsLayout.
int type() const override
Returns a unique graphics item type identifier.
QString visibleName() const
Returns a translated, user visible name for the layout multiframe class.
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:49
void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) override
Resolve paths in the item&#39;s properties (if there are any paths).
Convenience metadata class that uses static functions to create layout items and their configuration ...
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Stores metadata about one layout item class.
Stores metadata about one layout multiframe class.
Convenience metadata class that uses static functions to create layout multiframes and their configur...
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
virtual void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving)
Resolve paths in the item&#39;s properties (if there are any paths).
QgsLayoutMultiFramePathResolverFunc pathResolverFunction() const
Returns the classes&#39; path resolver function.
QgsLayoutItemAbstractMetadata(int type, const QString &visibleName)
Constructor for QgsLayoutItemAbstractMetadata with the specified class type and visibleName.
#define SIP_SKIP
Definition: qgis_sip.h:119
QgsLayoutMultiFrameAbstractMetadata(int type, const QString &visibleName)
Constructor for QgsLayoutMultiFrameAbstractMetadata with the specified class type and visibleName...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
std::function< QgsLayoutMultiFrame *(QgsLayout *)> QgsLayoutMultiFrameCreateFunc
Layout multiframe creation function.
QgsLayoutItem * createItem(QgsLayout *layout) override
Creates a layout item of this class for a specified layout.
int type() const
Returns the unique item type code for the layout item class.
#define SIP_FACTORY
Definition: qgis_sip.h:69
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QString visibleName() const
Returns a translated, user visible name for the layout item class.
Frame item, part of a QgsLayoutMultiFrame object.
QgsLayoutMultiFrameCreateFunc createFunction() const
Returns the classes&#39; multiframe creation function.
QgsLayoutMultiFrameMetadata(int type, const QString &visibleName, const QgsLayoutMultiFrameCreateFunc &pfCreate, const QgsLayoutMultiFramePathResolverFunc &pfPathResolver=nullptr)
Constructor for QgsLayoutMultiFrameMetadata with the specified class type and visibleName, and function pointers for the various item creation functions.
QgsLayoutItemPathResolverFunc pathResolverFunction() const
Returns the classes&#39; path resolver function.
Contains information about the context of a rendering operation.
Registry of available layout item types.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgssymbol.h:1003
std::function< void(QVariantMap &, const QgsPathResolver &, bool)> QgsLayoutMultiFramePathResolverFunc
Layout multiframe path resolver function.
virtual void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving)
Resolve paths in the item&#39;s properties (if there are any paths).
Resolves relative paths into absolute paths and vice versa.
std::function< QgsLayoutItem *(QgsLayout *)> QgsLayoutItemCreateFunc
Layout item creation function.
QgsLayoutMultiFrame * createMultiFrame(QgsLayout *layout) override
Creates a layout multiframe of this class for a specified layout.
void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) override
Resolve paths in the item&#39;s properties (if there are any paths).
std::function< void(QVariantMap &, const QgsPathResolver &, bool)> QgsLayoutItemPathResolverFunc
Layout item path resolver function.
int type() const
Returns the unique item type code for the layout multiframe class.