QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
105  virtual QgsFeatureRenderer *createRenderer( QDomElement &elem, const QgsReadWriteContext &context ) = 0 SIP_FACTORY;
106 
115  { Q_UNUSED( layer ) Q_UNUSED( style ); Q_UNUSED( oldRenderer ); return nullptr; }
116 
118  { Q_UNUSED( elem ) Q_UNUSED( geomType ); return nullptr; }
119 
120  protected:
122  QString mName;
124  QString mVisibleName;
126  QIcon mIcon;
127 };
128 
129 
130 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRendererAbstractMetadata::LayerTypes )
131 
132 
135 typedef QgsFeatureRenderer *( *QgsRendererCreateFromSldFunc )( QDomElement &, QgsWkbTypes::GeometryType geomType ) SIP_SKIP;
136 
142 {
143  public:
144 
149  QgsRendererMetadata( const QString &name,
150  const QString &visibleName,
151  QgsRendererCreateFunc pfCreate,
152  const QIcon &icon = QIcon(),
153  QgsRendererWidgetFunc pfWidget = nullptr,
154  QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) SIP_SKIP
155  : QgsRendererAbstractMetadata( name, visibleName, icon )
156  , mCreateFunc( pfCreate )
157  , mWidgetFunc( pfWidget )
158  , mCreateFromSldFunc( nullptr )
159  , mLayerTypes( layerTypes )
160  {}
161 
163  QgsRendererMetadata( const QString &name,
164  const QString &visibleName,
165  QgsRendererCreateFunc pfCreate,
166  QgsRendererCreateFromSldFunc pfCreateFromSld,
167  const QIcon &icon = QIcon(),
168  QgsRendererWidgetFunc pfWidget = nullptr,
169  QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) SIP_SKIP
170  : QgsRendererAbstractMetadata( name, visibleName, icon )
171  , mCreateFunc( pfCreate )
172  , mWidgetFunc( pfWidget )
173  , mCreateFromSldFunc( pfCreateFromSld )
174  , mLayerTypes( layerTypes )
175  {}
176 
177  QgsFeatureRenderer *createRenderer( QDomElement &elem, const QgsReadWriteContext &context ) override SIP_FACTORY
178  { return mCreateFunc ? mCreateFunc( elem, context ) : nullptr; }
179  QgsRendererWidget *createRendererWidget( QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer ) override SIP_FACTORY
180  { return mWidgetFunc ? mWidgetFunc( layer, style, renderer ) : nullptr; }
182  { return mCreateFromSldFunc ? mCreateFromSldFunc( elem, geomType ) : nullptr; }
183 
185  QgsRendererCreateFunc createFunction() const { return mCreateFunc; } SIP_SKIP
187  QgsRendererWidgetFunc widgetFunction() const { return mWidgetFunc; } SIP_SKIP
189  QgsRendererCreateFromSldFunc createFromSldFunction() const { return mCreateFromSldFunc; } SIP_SKIP
190 
192  void setWidgetFunction( QgsRendererWidgetFunc f ) { mWidgetFunc = f; } SIP_SKIP
193 
194  QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const override { return mLayerTypes; }
195 
196  protected:
203 
204  private:
205 #ifdef SIP_RUN
207 #endif
208 
209  QgsRendererAbstractMetadata::LayerTypes mLayerTypes;
210 };
211 
212 
223 class CORE_EXPORT QgsRendererRegistry
224 {
225  public:
226 
229 
231  QgsRendererRegistry( const QgsRendererRegistry &rh ) = delete;
233  QgsRendererRegistry &operator=( const QgsRendererRegistry &rh ) = delete;
234 
241  bool addRenderer( QgsRendererAbstractMetadata *metadata SIP_TRANSFER );
242 
249  bool removeRenderer( const QString &rendererName );
250 
255  QgsRendererAbstractMetadata *rendererMetadata( const QString &rendererName );
256 
261  QStringList renderersList( QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) const;
262 
268  QStringList renderersList( const QgsVectorLayer *layer ) const;
269 
270  private:
271 #ifdef SIP_RUN
273 #endif
274 
276  QMap<QString, QgsRendererAbstractMetadata *> mRenderers;
277 
279  QStringList mRenderersOrder;
280 };
281 
282 #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:40
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.
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:139
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.