QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
107 typedef std::function<QgsLayoutItem *( QgsLayout * )> QgsLayoutItemCreateFunc SIP_SKIP;
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 
187  virtual ~QgsLayoutMultiFrameAbstractMetadata() = default;
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 
207  virtual QgsLayoutMultiFrame *createMultiFrame( QgsLayout *layout ) = 0 SIP_FACTORY;
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 
230 typedef std::function<QgsLayoutMultiFrame *( QgsLayout * )> QgsLayoutMultiFrameCreateFunc SIP_SKIP;
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 
338  // item types provided by plugins
339  PluginItem = LayoutTextTable + 10000,
340  };
341 
350  QgsLayoutItemRegistry( QObject *parent = nullptr );
351 
352  ~QgsLayoutItemRegistry() override;
353 
358  bool populate();
359 
361  QgsLayoutItemRegistry( const QgsLayoutItemRegistry &rh ) = delete;
363  QgsLayoutItemRegistry &operator=( const QgsLayoutItemRegistry &rh ) = delete;
364 
370  QgsLayoutItemAbstractMetadata *itemMetadata( int type ) const;
371 
377  QgsLayoutMultiFrameAbstractMetadata *multiFrameMetadata( int type ) const;
378 
383  bool addLayoutItemType( QgsLayoutItemAbstractMetadata *metadata SIP_TRANSFER );
384 
389  bool addLayoutMultiFrameType( QgsLayoutMultiFrameAbstractMetadata *metadata SIP_TRANSFER );
390 
395  QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_FACTORY;
396 
401  QgsLayoutMultiFrame *createMultiFrame( int type, QgsLayout *layout ) const SIP_FACTORY;
402 
408  void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
409 
413  QMap< int, QString> itemTypes() const;
414 
415  signals:
416 
421  void typeAdded( int type, const QString &name );
422 
427  void multiFrameTypeAdded( int type, const QString &name );
428 
429  private:
430 #ifdef SIP_RUN
432 #endif
433 
434  QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
435  QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
436 
437 };
438 
439 #if 0
440 #ifndef SIP_RUN
441 //simple item for testing
443 #ifdef ANDROID
444 // For some reason, the Android NDK toolchain requires this to link properly.
445 // Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
446 class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
447 #else
448 class TestLayoutItem : public QgsLayoutItem
449 #endif
450 {
451  Q_OBJECT
452 
453  public:
454 
455  TestLayoutItem( QgsLayout *layout );
456  ~TestLayoutItem() = default;
457 
458  //implement pure virtual methods
459  int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
460  void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
461 
462  private:
463  QColor mColor;
464  QgsFillSymbol *mShapeStyleSymbol = nullptr;
465 };
467 #endif
468 #endif
469 
470 #endif //QGSLAYOUTITEMREGISTRY_H
471 
472 
473 
Base class for graphical items within a QgsLayout.
int type() const override
Returns a unique graphics item type identifier.
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).
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.
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, and function pointers for the various item creation functions.
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...
QString visiblePluralName() const
Returns a translated, user visible name for plurals of the layout item class (e.g.
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.
#define SIP_SKIP
Definition: qgis_sip.h:126
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:76
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.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgssymbol.h:1155
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.
QgsLayoutItemAbstractMetadata(int type, const QString &visibleName, const QString &visiblePluralName=QString())
Constructor for QgsLayoutItemAbstractMetadata with the specified class type and visibleName.