QGIS API Documentation  3.4.3-Madeira (2f64a3c)
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 QGSRENDERERV2REGISTRY_H
16 #define QGSRENDERERV2REGISTRY_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 
27 class QgsFeatureRenderer;
29 class QgsVectorLayer;
30 class QgsStyle;
32 
40 class CORE_EXPORT QgsRendererAbstractMetadata
41 {
42  public:
43 
48  enum LayerType
49  {
50  PointLayer = 1,
51  LineLayer = 2,
52  PolygonLayer = 4,
53  All = PointLayer | LineLayer | PolygonLayer,
54  };
55  Q_DECLARE_FLAGS( LayerTypes, LayerType )
56 
57  QgsRendererAbstractMetadata( const QString &name, const QString &visibleName, const QIcon &icon = QIcon() )
58  : mName( name )
59  , mVisibleName( visibleName )
60  , mIcon( icon )
61  {}
62  virtual ~QgsRendererAbstractMetadata() = default;
63 
64  QString name() const { return mName; }
65  QString visibleName() const { return mVisibleName; }
66 
67  QIcon icon() const { return mIcon; }
68  void setIcon( const QIcon &icon ) { mIcon = icon; }
69 
74  virtual QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const { return All; }
75 
79  virtual QgsFeatureRenderer *createRenderer( QDomElement &elem, const QgsReadWriteContext &context ) = 0 SIP_FACTORY;
80 
90  { Q_UNUSED( layer ); Q_UNUSED( style ); Q_UNUSED( oldRenderer ); return nullptr; }
91 
93  { Q_UNUSED( elem ); Q_UNUSED( geomType ); return nullptr; }
94 
95  protected:
97  QString mName;
99  QString mVisibleName;
101  QIcon mIcon;
102 };
103 
104 
105 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRendererAbstractMetadata::LayerTypes )
106 
107 
110 typedef QgsFeatureRenderer *( *QgsRendererCreateFromSldFunc )( QDomElement &, QgsWkbTypes::GeometryType geomType ) SIP_SKIP;
111 
117 {
118  public:
119 
124  QgsRendererMetadata( const QString &name,
125  const QString &visibleName,
126  QgsRendererCreateFunc pfCreate,
127  const QIcon &icon = QIcon(),
128  QgsRendererWidgetFunc pfWidget = nullptr,
129  QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) SIP_SKIP
130  : QgsRendererAbstractMetadata( name, visibleName, icon )
131  , mCreateFunc( pfCreate )
132  , mWidgetFunc( pfWidget )
133  , mCreateFromSldFunc( nullptr )
134  , mLayerTypes( layerTypes )
135  {}
136 
138  QgsRendererMetadata( const QString &name,
139  const QString &visibleName,
140  QgsRendererCreateFunc pfCreate,
141  QgsRendererCreateFromSldFunc pfCreateFromSld,
142  const QIcon &icon = QIcon(),
143  QgsRendererWidgetFunc pfWidget = nullptr,
144  QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) SIP_SKIP
145  : QgsRendererAbstractMetadata( name, visibleName, icon )
146  , mCreateFunc( pfCreate )
147  , mWidgetFunc( pfWidget )
148  , mCreateFromSldFunc( pfCreateFromSld )
149  , mLayerTypes( layerTypes )
150  {}
151 
152  QgsFeatureRenderer *createRenderer( QDomElement &elem, const QgsReadWriteContext &context ) override SIP_FACTORY
153  { return mCreateFunc ? mCreateFunc( elem, context ) : nullptr; }
154  QgsRendererWidget *createRendererWidget( QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer ) override SIP_FACTORY
155  { return mWidgetFunc ? mWidgetFunc( layer, style, renderer ) : nullptr; }
157  { return mCreateFromSldFunc ? mCreateFromSldFunc( elem, geomType ) : nullptr; }
158 
160  QgsRendererCreateFunc createFunction() const { return mCreateFunc; } SIP_SKIP
162  QgsRendererWidgetFunc widgetFunction() const { return mWidgetFunc; } SIP_SKIP
164  QgsRendererCreateFromSldFunc createFromSldFunction() const { return mCreateFromSldFunc; } SIP_SKIP
165 
167  void setWidgetFunction( QgsRendererWidgetFunc f ) { mWidgetFunc = f; } SIP_SKIP
168 
169  QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const override { return mLayerTypes; }
170 
171  protected:
178 
179  private:
180 #ifdef SIP_RUN
182 #endif
183 
184  QgsRendererAbstractMetadata::LayerTypes mLayerTypes;
185 };
186 
187 
198 class CORE_EXPORT QgsRendererRegistry
199 {
200  public:
201 
204 
206  QgsRendererRegistry( const QgsRendererRegistry &rh ) = delete;
208  QgsRendererRegistry &operator=( const QgsRendererRegistry &rh ) = delete;
209 
216  bool addRenderer( QgsRendererAbstractMetadata *metadata SIP_TRANSFER );
217 
224  bool removeRenderer( const QString &rendererName );
225 
230  QgsRendererAbstractMetadata *rendererMetadata( const QString &rendererName );
231 
236  QStringList renderersList( QgsRendererAbstractMetadata::LayerTypes layerTypes = QgsRendererAbstractMetadata::All ) const;
237 
243  QStringList renderersList( const QgsVectorLayer *layer ) const;
244 
245  private:
246 #ifdef SIP_RUN
248 #endif
249 
251  QMap<QString, QgsRendererAbstractMetadata *> mRenderers;
252 
254  QStringList mRenderersOrder;
255 };
256 
257 #endif // QGSRENDERERV2REGISTRY_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:138
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.