QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 
339 
340  // item types provided by plugins
341  PluginItem = LayoutTextTable + 10000,
342  };
343 
352  QgsLayoutItemRegistry( QObject *parent = nullptr );
353 
354  ~QgsLayoutItemRegistry() override;
355 
360  bool populate();
361 
363  QgsLayoutItemRegistry( const QgsLayoutItemRegistry &rh ) = delete;
365  QgsLayoutItemRegistry &operator=( const QgsLayoutItemRegistry &rh ) = delete;
366 
372  QgsLayoutItemAbstractMetadata *itemMetadata( int type ) const;
373 
379  QgsLayoutMultiFrameAbstractMetadata *multiFrameMetadata( int type ) const;
380 
385  bool addLayoutItemType( QgsLayoutItemAbstractMetadata *metadata SIP_TRANSFER );
386 
391  bool addLayoutMultiFrameType( QgsLayoutMultiFrameAbstractMetadata *metadata SIP_TRANSFER );
392 
397  QgsLayoutItem *createItem( int type, QgsLayout *layout ) const SIP_FACTORY;
398 
403  QgsLayoutMultiFrame *createMultiFrame( int type, QgsLayout *layout ) const SIP_FACTORY;
404 
410  void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
411 
415  QMap< int, QString> itemTypes() const;
416 
417  signals:
418 
423  void typeAdded( int type, const QString &name );
424 
429  void multiFrameTypeAdded( int type, const QString &name );
430 
431  private:
432 #ifdef SIP_RUN
434 #endif
435 
436  QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata;
437  QMap<int, QgsLayoutMultiFrameAbstractMetadata *> mMultiFrameMetadata;
438 
439 };
440 
441 #if 0
442 #ifndef SIP_RUN
443 //simple item for testing
445 #ifdef ANDROID
446 // For some reason, the Android NDK toolchain requires this to link properly.
447 // Note to self: Please try to remove this again once Qt ships their libs built with gcc-5
448 class CORE_EXPORT TestLayoutItem : public QgsLayoutItem
449 #else
450 class TestLayoutItem : public QgsLayoutItem
451 #endif
452 {
453  Q_OBJECT
454 
455  public:
456 
457  TestLayoutItem( QgsLayout *layout );
458  ~TestLayoutItem() = default;
459 
460  //implement pure virtual methods
461  int type() const override { return QgsLayoutItemRegistry::LayoutItem + 1002; }
462  void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
463 
464  private:
465  QColor mColor;
466  QgsFillSymbol *mShapeStyleSymbol = nullptr;
467 };
469 #endif
470 #endif
471 
472 #endif //QGSLAYOUTITEMREGISTRY_H
473 
474 
475 
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:1190
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.