Skip to content
Permalink
Browse files

Add API to allow extra feature renderers to be created for a vector

layer, so that these renderers are always drawn either above or
below the normal layer renderer

This allows programatic creation of extra styling for a vector layer,
e.g. to overlay custom markers or outlines on features (such as
highlighting "good" or "bad" features), without messing with
any user-created symbology for the layer.
  • Loading branch information
nyalldawson committed Dec 27, 2020
1 parent 39560ea commit d2fb33e0b74df6a5d9ae954ee7c46be87fccb4e0
@@ -442,7 +442,7 @@ QgsVectorLayer cannot be copied.
Returns a new instance equivalent to this one. A new provider is
created for the same data source and renderers for features and diagrams
are cloned too. Moreover, each attributes (transparency, extent, selected
features and so on) are identicals.
features and so on) are identical.

:return: a new layer instance

@@ -821,14 +821,56 @@ Sets diagram rendering object (takes ownership)

QgsFeatureRenderer *renderer();
%Docstring
Returns renderer.
Returns the feature renderer used for rendering the features in the layer in 2D
map views.

.. seealso:: :py:func:`setRenderer`
%End


void setRenderer( QgsFeatureRenderer *r /Transfer/ );
%Docstring
Sets renderer which will be invoked to represent this layer.
Sets the feature renderer which will be invoked to represent this layer in 2D map views.
Ownership is transferred.

.. seealso:: :py:func:`renderer`
%End

void addFeatureRendererGenerator( QgsFeatureRendererGenerator *generator /Transfer/ );
%Docstring
Adds a new feature renderer ``generator`` to the layer.

Ownership of ``generator`` is transferred to the layer.

.. seealso:: :py:func:`removeFeatureRendererGenerator`

.. seealso:: :py:func:`featureRendererGenerators`

.. versionadded:: 3.18
%End

void removeFeatureRendererGenerator( const QString &id );
%Docstring
Removes the feature renderer with matching ``id`` from the layer.

The corresponding generator will be deleted.

.. seealso:: :py:func:`addFeatureRendererGenerator`

.. seealso:: :py:func:`featureRendererGenerators`

.. versionadded:: 3.18
%End

QList< const QgsFeatureRendererGenerator * > featureRendererGenerators() const;
%Docstring
Returns a list of the feature renderer generators owned by the layer.

.. seealso:: :py:func:`addFeatureRendererGenerator`

.. seealso:: :py:func:`removeFeatureRendererGenerator`

.. versionadded:: 3.18
%End

QgsWkbTypes::GeometryType geometryType() const;
@@ -100,6 +100,7 @@
#include "qgsgeometryoptions.h"
#include "qgsexpressioncontextutils.h"
#include "qgsruntimeprofiler.h"
#include "qgsfeaturerenderergenerator.h"

#include "diagram/qgsdiagram.h"

@@ -236,6 +237,8 @@ QgsVectorLayer::~QgsVectorLayer()

if ( mFeatureCounter )
mFeatureCounter->cancel();

qDeleteAll( mRendererGenerators );
}

QgsVectorLayer *QgsVectorLayer::clone() const
@@ -3718,6 +3721,31 @@ void QgsVectorLayer::setRenderer( QgsFeatureRenderer *r )
}
}

void QgsVectorLayer::addFeatureRendererGenerator( QgsFeatureRendererGenerator *generator )
{
mRendererGenerators << generator;
}

void QgsVectorLayer::removeFeatureRendererGenerator( const QString &id )
{
for ( int i = mRendererGenerators.count() - 1; i >= 0; --i )
{
if ( mRendererGenerators.at( i )->id() == id )
{
delete mRendererGenerators.at( i );
mRendererGenerators.removeAt( i );
}
}
}

QList<const QgsFeatureRendererGenerator *> QgsVectorLayer::featureRendererGenerators() const
{
QList< const QgsFeatureRendererGenerator * > res;
for ( const QgsFeatureRendererGenerator *generator : mRendererGenerators )
res << generator;
return res;
}

void QgsVectorLayer::beginEditCommand( const QString &text )
{
if ( !mDataProvider )
@@ -80,6 +80,7 @@ class QgsAuxiliaryLayer;
class QgsGeometryOptions;
class QgsStyleEntityVisitorInterface;
class QgsVectorLayerTemporalProperties;
class QgsFeatureRendererGenerator;

typedef QList<int> QgsAttributeList;
typedef QSet<int> QgsAttributeIds;
@@ -569,7 +570,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* Returns a new instance equivalent to this one. A new provider is
* created for the same data source and renderers for features and diagrams
* are cloned too. Moreover, each attributes (transparency, extent, selected
* features and so on) are identicals.
* features and so on) are identical.
* \returns a new layer instance
* \since QGIS 3.0
*/
@@ -896,21 +897,62 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
void setDiagramLayerSettings( const QgsDiagramLayerSettings &s );
const QgsDiagramLayerSettings *diagramLayerSettings() const { return mDiagramLayerSettings; }

//! Returns renderer.
/**
* Returns the feature renderer used for rendering the features in the layer in 2D
* map views.
*
* \see setRenderer()
*/
QgsFeatureRenderer *renderer() { return mRenderer; }

/**
* Returns const renderer.
* Returns the feature renderer used for rendering the features in the layer in 2D
* map views.
*
* \see setRenderer()
* \note not available in Python bindings
*/
const QgsFeatureRenderer *renderer() const SIP_SKIP { return mRenderer; }

/**
* Sets renderer which will be invoked to represent this layer.
* Sets the feature renderer which will be invoked to represent this layer in 2D map views.
* Ownership is transferred.
*
* \see renderer()
*/
void setRenderer( QgsFeatureRenderer *r SIP_TRANSFER );

/**
* Adds a new feature renderer \a generator to the layer.
*
* Ownership of \a generator is transferred to the layer.
*
* \see removeFeatureRendererGenerator()
* \see featureRendererGenerators()
* \since QGIS 3.18
*/
void addFeatureRendererGenerator( QgsFeatureRendererGenerator *generator SIP_TRANSFER );

/**
* Removes the feature renderer with matching \a id from the layer.
*
* The corresponding generator will be deleted.
*
* \see addFeatureRendererGenerator()
* \see featureRendererGenerators()
* \since QGIS 3.18
*/
void removeFeatureRendererGenerator( const QString &id );

/**
* Returns a list of the feature renderer generators owned by the layer.
*
* \see addFeatureRendererGenerator()
* \see removeFeatureRendererGenerator()
* \since QGIS 3.18
*/
QList< const QgsFeatureRendererGenerator * > featureRendererGenerators() const;

//! Returns point, line or polygon
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const;

@@ -2939,6 +2981,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
QList<QgsWeakRelation> mWeakRelations;

bool mSetLegendFromStyle = false;

QList< QgsFeatureRendererGenerator * > mRendererGenerators;
};


0 comments on commit d2fb33e

Please sign in to comment.
You can’t perform that action at this time.