QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsrendererregistry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrendererregistry.h
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSRENDERERREGISTRY_H
16 #define QGSRENDERERREGISTRY_H
17 
18 #include "qgis_core.h"
19 #include "qgis_sip.h"
20 #include <QIcon>
21 #include <QMap>
22 #include <QStringList>
23 #include <QDomElement>
24 
25 // #include "qgis.h"
26 #include "qgswkbtypes.h"
27 
28 class QgsFeatureRenderer;
30 class QgsVectorLayer;
31 class QgsStyle;
33 
41 class CORE_EXPORT QgsRendererAbstractMetadata
42 {
43  public:
44 
49  enum LayerType
50  {
51  PointLayer = 1,
52  LineLayer = 2,
53  PolygonLayer = 4,
54  All = PointLayer | LineLayer | PolygonLayer,
55  };
56  Q_DECLARE_FLAGS( LayerTypes, LayerType )
57 
58 
65  QgsRendererAbstractMetadata( const QString &name, const QString &visibleName, const QIcon &icon = QIcon() )
66  : mName( name )
67  , mVisibleName( visibleName )
68  , mIcon( icon )
69  {}
70  virtual ~QgsRendererAbstractMetadata() = default;
71 
76  QString name() const { return mName; }
77 
82  QString visibleName() const { return mVisibleName; }
83 
88  QIcon icon() const { return mIcon; }
89 
94  void setIcon( const QIcon &icon ) { mIcon = icon; }
95 
100  virtual QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const { return All; }
101 
106  virtual QgsFeatureRenderer *createRenderer( QDomElement &elem, const QgsReadWriteContext &context ) = 0 SIP_FACTORY;
107 
116  { Q_UNUSED( layer ) Q_UNUSED( style ); Q_UNUSED( oldRenderer ); return nullptr; }
117 
119  { Q_UNUSED( elem ) Q_UNUSED( geomType ); return nullptr; }
120 
121  protected:
123  QString mName;
125  QString mVisibleName;
127  QIcon mIcon;
128 };
129 
130 
131 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRendererAbstractMetadata::LayerTypes )
132 
133 
136 typedef QgsFeatureRenderer *( *QgsRendererCreateFromSldFunc )( QDomElement &, QgsWkbTypes::GeometryType geomType ) SIP_SKIP;
137 
143 {
144  public:
145 
150  QgsRendererMetadata( const QString &name,
151  const QString &visibleName,
152  QgsRendererCreateFunc pfCreate,
153  const QIcon &icon = QIcon(),
154  QgsRendererWidgetFunc pfWidget = nullptr,
155  QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) SIP_SKIP
156  : QgsRendererAbstractMetadata( name, visibleName, icon )
157  , mCreateFunc( pfCreate )
158  , mWidgetFunc( pfWidget )
159  , mCreateFromSldFunc( nullptr )
160  , mLayerTypes( layerTypes )
161  {}
162 
164  QgsRendererMetadata( const QString &name,
165  const QString &visibleName,
166  QgsRendererCreateFunc pfCreate,
167  QgsRendererCreateFromSldFunc pfCreateFromSld,
168  const QIcon &icon = QIcon(),
169  QgsRendererWidgetFunc pfWidget = nullptr,
170  QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) SIP_SKIP
171  : QgsRendererAbstractMetadata( name, visibleName, icon )
172  , mCreateFunc( pfCreate )
173  , mWidgetFunc( pfWidget )
174  , mCreateFromSldFunc( pfCreateFromSld )
175  , mLayerTypes( layerTypes )
176  {}
177 
178  QgsFeatureRenderer *createRenderer( QDomElement &elem, const QgsReadWriteContext &context ) override SIP_FACTORY
179  { return mCreateFunc ? mCreateFunc( elem, context ) : nullptr; }
181  { return mWidgetFunc ? mWidgetFunc( layer, style, renderer ) : nullptr; }
183  { return mCreateFromSldFunc ? mCreateFromSldFunc( elem, geomType ) : nullptr; }
184 
186  QgsRendererCreateFunc createFunction() const { return mCreateFunc; } SIP_SKIP
188  QgsRendererWidgetFunc widgetFunction() const { return mWidgetFunc; } SIP_SKIP
190  QgsRendererCreateFromSldFunc createFromSldFunction() const { return mCreateFromSldFunc; } SIP_SKIP
191 
193  void setWidgetFunction( QgsRendererWidgetFunc f ) { mWidgetFunc = f; } SIP_SKIP
194 
195  QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const override { return mLayerTypes; }
196 
197  protected:
204 
205  private:
206 #ifdef SIP_RUN
208 #endif
209 
210  QgsRendererAbstractMetadata::LayerTypes mLayerTypes;
211 };
212 
213 
224 class CORE_EXPORT QgsRendererRegistry
225 {
226  public:
227 
230 
232  QgsRendererRegistry( const QgsRendererRegistry &rh ) = delete;
234  QgsRendererRegistry &operator=( const QgsRendererRegistry &rh ) = delete;
235 
242  bool addRenderer( QgsRendererAbstractMetadata *metadata SIP_TRANSFER );
243 
250  bool removeRenderer( const QString &rendererName );
251 
256  QgsRendererAbstractMetadata *rendererMetadata( const QString &rendererName );
257 
262  QStringList renderersList( QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) const;
263 
269  QStringList renderersList( const QgsVectorLayer *layer ) const;
270 
271  private:
272 #ifdef SIP_RUN
274 #endif
275 
277  QMap<QString, QgsRendererAbstractMetadata *> mRenderers;
278 
280  QStringList mRenderersOrder;
281 };
282 
283 #endif // QGSRENDERERREGISTRY_H
QgsRendererAbstractMetadata::setIcon
void setIcon(const QIcon &icon)
Sets an icon representing the renderer.
Definition: qgsrendererregistry.h:94
QgsRendererAbstractMetadata::mName
QString mName
name used within QGIS for identification (the same what renderer's type() returns)
Definition: qgsrendererregistry.h:123
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsRendererMetadata::createRendererWidget
QgsRendererWidget * createRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer) override
Returns new instance of settings widget for the renderer.
Definition: qgsrendererregistry.h:180
QgsRendererAbstractMetadata::visibleName
QString visibleName() const
Returns a friendly display name of the renderer.
Definition: qgsrendererregistry.h:82
QgsRendererAbstractMetadata::name
QString name() const
Returns the unique name of the renderer.
Definition: qgsrendererregistry.h:76
QgsRendererMetadata::QgsRendererMetadata
QgsRendererMetadata(const QString &name, const QString &visibleName, QgsRendererCreateFunc pfCreate, const QIcon &icon=QIcon(), QgsRendererWidgetFunc pfWidget=nullptr, QgsRendererAbstractMetadata::LayerTypes layerTypes=QgsRendererAbstractMetadata::All)
Construct metadata.
Definition: qgsrendererregistry.h:150
QgsRendererAbstractMetadata::mVisibleName
QString mVisibleName
name visible for users (translatable)
Definition: qgsrendererregistry.h:125
QgsRendererMetadata::mCreateFunc
QgsRendererCreateFunc mCreateFunc
pointer to function that creates an instance of the renderer when loading project / style
Definition: qgsrendererregistry.h:199
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsRendererWidgetFunc
QgsRendererWidget *(* QgsRendererWidgetFunc)(QgsVectorLayer *, QgsStyle *, QgsFeatureRenderer *)
Definition: qgsrendererregistry.h:135
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsRendererAbstractMetadata::LayerType
LayerType
Layer types the renderer is compatible with.
Definition: qgsrendererregistry.h:49
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
QgsRendererAbstractMetadata::All
@ All
Compatible with all vector layers.
Definition: qgsrendererregistry.h:54
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
QgsRendererMetadata
Convenience metadata class that uses static functions to create renderer and its widget.
Definition: qgsrendererregistry.h:142
QgsStyle
Definition: qgsstyle.h:159
QgsRendererMetadata::createFromSldFunction
QgsRendererCreateFromSldFunc createFromSldFunction() const
Definition: qgsrendererregistry.h:190
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsRendererAbstractMetadata::compatibleLayerTypes
virtual QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const
Returns flags indicating the types of layer the renderer is compatible with.
Definition: qgsrendererregistry.h:100
QgsRendererMetadata::QgsRendererMetadata
QgsRendererMetadata(const QString &name, const QString &visibleName, QgsRendererCreateFunc pfCreate, QgsRendererCreateFromSldFunc pfCreateFromSld, const QIcon &icon=QIcon(), QgsRendererWidgetFunc pfWidget=nullptr, QgsRendererAbstractMetadata::LayerTypes layerTypes=QgsRendererAbstractMetadata::All)
Definition: qgsrendererregistry.h:164
QgsRendererMetadata::setWidgetFunction
void setWidgetFunction(QgsRendererWidgetFunc f)
Definition: qgsrendererregistry.h:193
QgsRendererMetadata::widgetFunction
QgsRendererWidgetFunc widgetFunction() const
Definition: qgsrendererregistry.h:188
QgsRendererMetadata::mCreateFromSldFunc
QgsRendererCreateFromSldFunc mCreateFromSldFunc
pointer to function that creates an instance of the renderer from SLD
Definition: qgsrendererregistry.h:203
QgsRendererAbstractMetadata::icon
QIcon icon() const
Returns an icon representing the renderer.
Definition: qgsrendererregistry.h:88
QgsWkbTypes
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:41
QgsRendererAbstractMetadata::mIcon
QIcon mIcon
icon to be shown in the renderer properties dialog
Definition: qgsrendererregistry.h:127
SIP_EXTERNAL
#define SIP_EXTERNAL
Definition: qgis_sip.h:116
QgsRendererMetadata::createFunction
QgsRendererCreateFunc createFunction() const
Definition: qgsrendererregistry.h:186
QgsRendererMetadata::compatibleLayerTypes
QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const override
Returns flags indicating the types of layer the renderer is compatible with.
Definition: qgsrendererregistry.h:195
QgsRendererMetadata::createRendererFromSld
QgsFeatureRenderer * createRendererFromSld(QDomElement &elem, QgsWkbTypes::GeometryType geomType) override
Definition: qgsrendererregistry.h:182
QgsRendererMetadata::mWidgetFunc
QgsRendererWidgetFunc mWidgetFunc
pointer to function that creates a widget for configuration of renderer's params
Definition: qgsrendererregistry.h:201
QgsRendererAbstractMetadata::createRendererWidget
virtual QgsRendererWidget * createRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *oldRenderer)
Returns new instance of settings widget for the renderer.
Definition: qgsrendererregistry.h:115
QgsRendererRegistry
Registry of renderers.
Definition: qgsrendererregistry.h:224
QgsRendererMetadata::createRenderer
QgsFeatureRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Returns new instance of the renderer given the DOM element.
Definition: qgsrendererregistry.h:178
qgswkbtypes.h
QgsRendererCreateFunc
QgsFeatureRenderer *(* QgsRendererCreateFunc)(QDomElement &, const QgsReadWriteContext &)
Definition: qgsrendererregistry.h:134
QgsRendererAbstractMetadata
Stores metadata about one renderer class.
Definition: qgsrendererregistry.h:41
QgsRendererAbstractMetadata::createRendererFromSld
virtual QgsFeatureRenderer * createRendererFromSld(QDomElement &elem, QgsWkbTypes::GeometryType geomType)
Definition: qgsrendererregistry.h:118
QgsRendererCreateFromSldFunc
QgsFeatureRenderer *(* QgsRendererCreateFromSldFunc)(QDomElement &, QgsWkbTypes::GeometryType geomType)
Definition: qgsrendererregistry.h:136