QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
323  // item types provided by plugins
324  PluginItem = LayoutTextTable + 10000,
325  };
326 
335  QgsLayoutItemRegistry( QObject *parent = nullptr );
336 
337  ~QgsLayoutItemRegistry() override;
338 
343  bool populate();
344 
346  QgsLayoutItemRegistry( const QgsLayoutItemRegistry &rh ) = delete;
348  QgsLayoutItemRegistry &operator=( const QgsLayoutItemRegistry &rh ) = delete;
349 
355  QgsLayoutItemAbstractMetadata *itemMetadata( int type ) const;
356 
362  QgsLayoutMultiFrameAbstractMetadata *multiFrameMetadata( int type ) const;
363 
368  bool addLayoutItemType( QgsLayoutItemAbstractMetadata *metadata SIP_TRANSFER );
369 
374  bool addLayoutMultiFrameType( QgsLayoutMultiFrameAbstractMetadata *metadata SIP_TRANSFER );
375 
380  QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_FACTORY;
381 
386  QgsLayoutMultiFrame *createMultiFrame( int type, QgsLayout *layout ) const SIP_FACTORY;
387 
393  void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
394 
398  QMap< int, QString> itemTypes() const;
399 
400  signals:
401 
406  void typeAdded( int type, const QString &name );
407 
412  void multiFrameTypeAdded( int type, const QString &name );
413 
414  private:
415 #ifdef SIP_RUN
417 #endif
418 
419  QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
420  QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
421 
422 };
423 
424 #if 0
425 #ifndef SIP_RUN
426 //simple item for testing
428 #ifdef ANDROID
429 // For some reason, the Android NDK toolchain requires this to link properly.
430 // Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
431 class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
432 #else
433 class TestLayoutItem : public QgsLayoutItem
434 #endif
435 {
436  Q_OBJECT
437 
438  public:
439 
440  TestLayoutItem( QgsLayout *layout );
441  ~TestLayoutItem() = default;
442 
443  //implement pure virtual methods
444  int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
445  void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
446 
447  private:
448  QColor mColor;
449  QgsFillSymbol *mShapeStyleSymbol = nullptr;
450 };
452 #endif
453 #endif
454 
455 #endif //QGSLAYOUTITEMREGISTRY_H
456 
457 
458 
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.
void resolvePaths(QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) override
Resolve paths in the item&#39;s properties (if there are any paths).
int type() const
Returns the unique item type code for the layout item class.
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.
QString visibleName() const
Returns a translated, user visible name for the layout multiframe class.
Stores metadata about one layout multiframe class.
Convenience metadata class that uses static functions to create layout multiframes and their configur...
QgsLayoutMultiFramePathResolverFunc pathResolverFunction() const
Returns the classes&#39; path resolver function.
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).
QgsLayoutMultiFrameCreateFunc createFunction() const
Returns the classes&#39; multiframe creation 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.
#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
virtual QIcon icon() const
Returns an icon representing the layout multiframe type.
Frame item, part of a QgsLayoutMultiFrame object.
QgsLayoutItemCreateFunc createFunction() const
Returns the classes&#39; item 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.
QString visibleName() const
Returns a translated, user visible name for the layout item class.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
int type() const
Returns the unique item type code for the layout multiframe class.
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.