QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
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
The class is used as a container of context for various read/write operations on other objects.
Stores metadata about one renderer class.
LayerType
Layer types the renderer is compatible with.
@ All
Compatible with all vector layers.
virtual QgsFeatureRenderer * createRendererFromSld(QDomElement &elem, QgsWkbTypes::GeometryType geomType)
QIcon icon() const
Returns an icon representing the renderer.
virtual ~QgsRendererAbstractMetadata()=default
void setIcon(const QIcon &icon)
Sets an icon representing the renderer.
virtual QgsRendererWidget * createRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *oldRenderer)
Returns new instance of settings widget for the renderer.
QString mName
name used within QGIS for identification (the same what renderer's type() returns)
QString mVisibleName
name visible for users (translatable)
virtual QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const
Returns flags indicating the types of layer the renderer is compatible with.
QIcon mIcon
icon to be shown in the renderer properties dialog
QString visibleName() const
Returns a friendly display name of the renderer.
QString name() const
Returns the unique name of the renderer.
virtual QgsFeatureRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context)=0
Returns new instance of the renderer given the DOM element.
Convenience metadata class that uses static functions to create renderer and its widget.
void setWidgetFunction(QgsRendererWidgetFunc f)
QgsRendererWidget * createRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer) override
Returns new instance of settings widget for the renderer.
QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const override
Returns flags indicating the types of layer the renderer is compatible with.
QgsRendererCreateFunc createFunction() const
QgsRendererMetadata(const QString &name, const QString &visibleName, QgsRendererCreateFunc pfCreate, const QIcon &icon=QIcon(), QgsRendererWidgetFunc pfWidget=nullptr, QgsRendererAbstractMetadata::LayerTypes layerTypes=QgsRendererAbstractMetadata::All)
Construct metadata.
QgsFeatureRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Returns new instance of the renderer given the DOM element.
QgsRendererCreateFromSldFunc mCreateFromSldFunc
pointer to function that creates an instance of the renderer from SLD
QgsRendererWidgetFunc widgetFunction() const
QgsRendererCreateFunc mCreateFunc
pointer to function that creates an instance of the renderer when loading project / style
QgsRendererMetadata(const QString &name, const QString &visibleName, QgsRendererCreateFunc pfCreate, QgsRendererCreateFromSldFunc pfCreateFromSld, const QIcon &icon=QIcon(), QgsRendererWidgetFunc pfWidget=nullptr, QgsRendererAbstractMetadata::LayerTypes layerTypes=QgsRendererAbstractMetadata::All)
QgsRendererCreateFromSldFunc createFromSldFunction() const
QgsRendererWidgetFunc mWidgetFunc
pointer to function that creates a widget for configuration of renderer's params
QgsFeatureRenderer * createRendererFromSld(QDomElement &elem, QgsWkbTypes::GeometryType geomType) override
Registry of renderers.
QgsRendererRegistry & operator=(const QgsRendererRegistry &rh)=delete
QgsRendererRegistry cannot be copied.
QgsRendererRegistry(const QgsRendererRegistry &rh)=delete
QgsRendererRegistry cannot be copied.
Base class for renderer settings widgets.
Represents a vector layer which manages a vector based data sets.
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:42
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
#define SIP_EXTERNAL
Definition: qgis_sip.h:116
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
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.
QgsRendererWidget *(* QgsRendererWidgetFunc)(QgsVectorLayer *, QgsStyle *, QgsFeatureRenderer *)
QgsFeatureRenderer *(* QgsRendererCreateFunc)(QDomElement &, const QgsReadWriteContext &)
QgsFeatureRenderer *(* QgsRendererCreateFromSldFunc)(QDomElement &, QgsWkbTypes::GeometryType geomType)