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