|
17 | 17 | #define QGSLAYOUTITEMREGISTRY_H
|
18 | 18 |
|
19 | 19 | #include "qgis_core.h"
|
| 20 | +#include "qgis_sip.h" |
| 21 | +#include "qgspathresolver.h" |
20 | 22 | #include <QGraphicsItem> //for QGraphicsItem::UserType
|
| 23 | +#include <functional> |
21 | 24 |
|
| 25 | +class QgsLayout; |
22 | 26 | class QgsLayoutItem;
|
23 | 27 |
|
| 28 | +/** |
| 29 | + * \ingroup core |
| 30 | + * \brief Stores metadata about one layout item class. |
| 31 | + * \note In C++ you can use QgsSymbolLayerMetadata convenience class. |
| 32 | + * \since QGIS 3.0 |
| 33 | + */ |
| 34 | +class CORE_EXPORT QgsLayoutItemAbstractMetadata |
| 35 | +{ |
| 36 | + public: |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor for QgsLayoutItemAbstractMetadata with the specified class \a type |
| 40 | + * and \a visibleName. |
| 41 | + */ |
| 42 | + QgsLayoutItemAbstractMetadata( int type, const QString &visibleName ) |
| 43 | + : mType( type ) |
| 44 | + , mVisibleName( visibleName ) |
| 45 | + {} |
| 46 | + |
| 47 | + virtual ~QgsLayoutItemAbstractMetadata() = default; |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns the unique item type code for the layout item class. |
| 51 | + */ |
| 52 | + int type() const { return mType; } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns a translated, user visible name for the layout item class. |
| 56 | + */ |
| 57 | + QString visibleName() const { return mVisibleName; } |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a layout item of this class for a specified \a layout, given the map of \a properties. |
| 61 | + */ |
| 62 | + virtual QgsLayoutItem *createItem( QgsLayout *layout, const QVariantMap &properties ) = 0 SIP_FACTORY; |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates a configuration widget for layout items of this type. Can return nullptr if no configuration GUI is required. |
| 66 | + */ |
| 67 | + virtual QWidget *createItemWidget() SIP_FACTORY { return nullptr; } |
| 68 | + |
| 69 | + /** |
| 70 | + * Resolve paths in the item's \a properties (if there are any paths). |
| 71 | + * When \a saving is true, paths are converted from absolute to relative, |
| 72 | + * when \a saving is false, paths are converted from relative to absolute. |
| 73 | + * This ensures that paths in project files can be relative, but in item |
| 74 | + * instances the paths are always absolute. |
| 75 | + */ |
| 76 | + virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) |
| 77 | + { |
| 78 | + Q_UNUSED( properties ); |
| 79 | + Q_UNUSED( pathResolver ); |
| 80 | + Q_UNUSED( saving ); |
| 81 | + } |
| 82 | + |
| 83 | + private: |
| 84 | + |
| 85 | + int mType = -1; |
| 86 | + QString mVisibleName; |
| 87 | +}; |
| 88 | + |
| 89 | +//! Layout item creation function |
| 90 | +typedef std::function<QgsLayoutItem *( QgsLayout *, const QVariantMap & )> QgsLayoutItemCreateFunc SIP_SKIP; |
| 91 | + |
| 92 | +//! Layout item configuration widget creation function |
| 93 | +typedef std::function<QWidget *()> QgsLayoutItemWidgetFunc SIP_SKIP; |
| 94 | + |
| 95 | +//! Layout item path resolver function |
| 96 | +typedef std::function<void( QVariantMap &, const QgsPathResolver &, bool )> QgsLayoutItemPathResolverFunc SIP_SKIP; |
| 97 | + |
| 98 | +#ifndef SIP_RUN |
| 99 | + |
| 100 | +/** |
| 101 | + * \ingroup core |
| 102 | + * Convenience metadata class that uses static functions to create layout items and their configuration widgets. |
| 103 | + * \since QGIS 3.0 |
| 104 | + * \note not available in Python bindings |
| 105 | + */ |
| 106 | +class CORE_EXPORT QgsLayoutItemMetadata : public QgsLayoutItemAbstractMetadata |
| 107 | +{ |
| 108 | + public: |
| 109 | + |
| 110 | + /** |
| 111 | + * Constructor for QgsLayoutItemMetadata with the specified class \a type |
| 112 | + * and \a visibleName, and function pointers for the various item and |
| 113 | + * configuration widget creation functions. |
| 114 | + */ |
| 115 | + QgsLayoutItemMetadata( int type, const QString &visibleName, |
| 116 | + QgsLayoutItemCreateFunc pfCreate, |
| 117 | + QgsLayoutItemPathResolverFunc pfPathResolver = nullptr, |
| 118 | + QgsLayoutItemWidgetFunc pfWidget = nullptr ) |
| 119 | + : QgsLayoutItemAbstractMetadata( type, visibleName ) |
| 120 | + , mCreateFunc( pfCreate ) |
| 121 | + , mWidgetFunc( pfWidget ) |
| 122 | + , mPathResolverFunc( pfPathResolver ) |
| 123 | + {} |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns the classes' item creation function. |
| 127 | + */ |
| 128 | + QgsLayoutItemCreateFunc createFunction() const { return mCreateFunc; } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns the classes' configuration widget creation function. |
| 132 | + * \see setWidgetFunction() |
| 133 | + */ |
| 134 | + QgsLayoutItemWidgetFunc widgetFunction() const { return mWidgetFunc; } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns the classes' path resolver function. |
| 138 | + */ |
| 139 | + QgsLayoutItemPathResolverFunc pathResolverFunction() const { return mPathResolverFunc; } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sets the classes' configuration widget creation \a function. |
| 143 | + * \see widgetFunction() |
| 144 | + */ |
| 145 | + void setWidgetFunction( QgsLayoutItemWidgetFunc function ) { mWidgetFunc = function; } |
| 146 | + |
| 147 | + virtual QgsLayoutItem *createItem( QgsLayout *layout, const QVariantMap &properties ) override { return mCreateFunc ? mCreateFunc( layout, properties ) : nullptr; } |
| 148 | + virtual QWidget *createItemWidget() override { return mWidgetFunc ? mWidgetFunc() : nullptr; } |
| 149 | + virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) override |
| 150 | + { |
| 151 | + if ( mPathResolverFunc ) |
| 152 | + mPathResolverFunc( properties, pathResolver, saving ); |
| 153 | + } |
| 154 | + |
| 155 | + protected: |
| 156 | + QgsLayoutItemCreateFunc mCreateFunc = nullptr; |
| 157 | + QgsLayoutItemWidgetFunc mWidgetFunc = nullptr; |
| 158 | + QgsLayoutItemPathResolverFunc mPathResolverFunc = nullptr; |
| 159 | + |
| 160 | +}; |
| 161 | + |
| 162 | +#endif |
| 163 | + |
| 164 | + |
| 165 | + |
24 | 166 | /**
|
25 | 167 | * \ingroup core
|
26 | 168 | * \class QgsLayoutItemRegistry
|
@@ -57,8 +199,52 @@ class CORE_EXPORT QgsLayoutItemRegistry : public QObject
|
57 | 199 | */
|
58 | 200 | QgsLayoutItemRegistry( QObject *parent = nullptr );
|
59 | 201 |
|
| 202 | + ~QgsLayoutItemRegistry(); |
| 203 | + |
| 204 | + //! QgsLayoutItemRegistry cannot be copied. |
| 205 | + QgsLayoutItemRegistry( const QgsLayoutItemRegistry &rh ) = delete; |
| 206 | + //! QgsLayoutItemRegistryQgsLayoutItemRegistry cannot be copied. |
| 207 | + QgsLayoutItemRegistry &operator=( const QgsLayoutItemRegistry &rh ) = delete; |
| 208 | + |
| 209 | + /** |
| 210 | + * Returns the metadata for the specified item \a type. Returns nullptr if |
| 211 | + * a corresponding type was not found in the registry. |
| 212 | + */ |
| 213 | + QgsLayoutItemAbstractMetadata *itemMetadata( int type ) const; |
| 214 | + |
| 215 | + /** |
| 216 | + * Registers a new layout item type. Takes ownership of the metadata instance. |
| 217 | + */ |
| 218 | + bool addLayoutItemType( QgsLayoutItemAbstractMetadata *metadata SIP_TRANSFER ); |
| 219 | + |
| 220 | + /** |
| 221 | + * Creates a new instance of a layout item given the item \a type, target \a layout and \a properties. |
| 222 | + */ |
| 223 | + QgsLayoutItem *createItem( int type, QgsLayout *layout, const QVariantMap &properties = QVariantMap() ) const SIP_FACTORY; |
| 224 | + |
| 225 | + /** |
| 226 | + * Creates a new instance of a layout item configuration widget for the specified item \a type. |
| 227 | + */ |
| 228 | + QWidget *createItemWidget( int type ) const SIP_FACTORY; |
| 229 | + |
| 230 | + /** |
| 231 | + * Resolve paths in properties of a particular symbol layer. |
| 232 | + * This normally means converting relative paths to absolute paths when loading |
| 233 | + * and converting absolute paths to relative paths when saving. |
| 234 | + */ |
| 235 | + void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const; |
| 236 | + |
| 237 | + /** |
| 238 | + * Returns a map of available item types to translated name. |
| 239 | + */ |
| 240 | + QMap< int, QString> itemTypes() const; |
60 | 241 |
|
61 | 242 | private:
|
| 243 | +#ifdef SIP_RUN |
| 244 | + QgsLayoutItemRegistry( const QgsLayoutItemRegistry &rh ); |
| 245 | +#endif |
| 246 | + |
| 247 | + QMap<int, QgsLayoutItemAbstractMetadata *> mMetadata; |
62 | 248 |
|
63 | 249 | };
|
64 | 250 |
|
|
0 commit comments