QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  QgsRendererAbstractMetadata( const QString &name, const QString &visibleName, const QIcon &icon = QIcon() )
59  : mName( name )
60  , mVisibleName( visibleName )
61  , mIcon( icon )
62  {}
63  virtual ~QgsRendererAbstractMetadata() = default;
64 
65  QString name() const { return mName; }
66  QString visibleName() const { return mVisibleName; }
67 
68  QIcon icon() const { return mIcon; }
69  void setIcon( const QIcon &icon ) { mIcon = icon; }
70 
75  virtual QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const { return All; }
76 
80  virtual QgsFeatureRenderer *createRenderer( QDomElement &elem, const QgsReadWriteContext &context ) = 0 SIP_FACTORY;
81 
91  { Q_UNUSED( layer ) Q_UNUSED( style ); Q_UNUSED( oldRenderer ); return nullptr; }
92 
94  { Q_UNUSED( elem ) Q_UNUSED( geomType ); return nullptr; }
95 
96  protected:
98  QString mName;
100  QString mVisibleName;
102  QIcon mIcon;
103 };
104 
105 
106 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRendererAbstractMetadata::LayerTypes )
107 
108 
111 typedef QgsFeatureRenderer *( *QgsRendererCreateFromSldFunc )( QDomElement &, QgsWkbTypes::GeometryType geomType ) SIP_SKIP;
112 
118 {
119  public:
120 
125  QgsRendererMetadata( const QString &name,
126  const QString &visibleName,
127  QgsRendererCreateFunc pfCreate,
128  const QIcon &icon = QIcon(),
129  QgsRendererWidgetFunc pfWidget = nullptr,
130  QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) SIP_SKIP
131  : QgsRendererAbstractMetadata( name, visibleName, icon )
132  , mCreateFunc( pfCreate )
133  , mWidgetFunc( pfWidget )
134  , mCreateFromSldFunc( nullptr )
135  , mLayerTypes( layerTypes )
136  {}
137 
139  QgsRendererMetadata( const QString &name,
140  const QString &visibleName,
141  QgsRendererCreateFunc pfCreate,
142  QgsRendererCreateFromSldFunc pfCreateFromSld,
143  const QIcon &icon = QIcon(),
144  QgsRendererWidgetFunc pfWidget = nullptr,
145  QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) SIP_SKIP
146  : QgsRendererAbstractMetadata( name, visibleName, icon )
147  , mCreateFunc( pfCreate )
148  , mWidgetFunc( pfWidget )
149  , mCreateFromSldFunc( pfCreateFromSld )
150  , mLayerTypes( layerTypes )
151  {}
152 
153  QgsFeatureRenderer *createRenderer( QDomElement &elem, const QgsReadWriteContext &context ) override SIP_FACTORY
154  { return mCreateFunc ? mCreateFunc( elem, context ) : nullptr; }
155  QgsRendererWidget *createRendererWidget( QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer ) override SIP_FACTORY
156  { return mWidgetFunc ? mWidgetFunc( layer, style, renderer ) : nullptr; }
158  { return mCreateFromSldFunc ? mCreateFromSldFunc( elem, geomType ) : nullptr; }
159 
161  QgsRendererCreateFunc createFunction() const { return mCreateFunc; } SIP_SKIP
163  QgsRendererWidgetFunc widgetFunction() const { return mWidgetFunc; } SIP_SKIP
165  QgsRendererCreateFromSldFunc createFromSldFunction() const { return mCreateFromSldFunc; } SIP_SKIP
166 
168  void setWidgetFunction( QgsRendererWidgetFunc f ) { mWidgetFunc = f; } SIP_SKIP
169 
170  QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const override { return mLayerTypes; }
171 
172  protected:
179 
180  private:
181 #ifdef SIP_RUN
183 #endif
184 
185  QgsRendererAbstractMetadata::LayerTypes mLayerTypes;
186 };
187 
188 
199 class CORE_EXPORT QgsRendererRegistry
200 {
201  public:
202 
205 
207  QgsRendererRegistry( const QgsRendererRegistry &rh ) = delete;
209  QgsRendererRegistry &operator=( const QgsRendererRegistry &rh ) = delete;
210 
217  bool addRenderer( QgsRendererAbstractMetadata *metadata SIP_TRANSFER );
218 
225  bool removeRenderer( const QString &rendererName );
226 
231  QgsRendererAbstractMetadata *rendererMetadata( const QString &rendererName );
232 
237  QStringList renderersList( QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) const;
238 
244  QStringList renderersList( const QgsVectorLayer *layer ) const;
245 
246  private:
247 #ifdef SIP_RUN
249 #endif
250 
252  QMap<QString, QgsRendererAbstractMetadata *> mRenderers;
253 
255  QStringList mRenderersOrder;
256 };
257 
258 #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)
QgsFeatureRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Returns new instance of the renderer given the DOM element.
Registry of renderers.
QgsRendererWidgetFunc widgetFunction() const
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:119
QgsRendererWidget *(* QgsRendererWidgetFunc)(QgsVectorLayer *, QgsStyle *, QgsFeatureRenderer *)
void setIcon(const QIcon &icon)
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QIcon mIcon
icon to be shown in the renderer properties dialog
#define SIP_FACTORY
Definition: qgis_sip.h:69
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:109
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.