Skip to content
Permalink
Browse files

Allow QgsVectorDataProviders to create QgsFeatureRenderers

Implements qgis/QGIS-Enhancement-Proposals#111

Adds support to QgsVectorDataProvider to create vector layer renderers
using provider-specific backend information.
  • Loading branch information
nyalldawson committed Mar 1, 2018
1 parent b42c893 commit 678f65853a14f8a9769a9e25cdb4639ed2c5e67b
@@ -51,6 +51,7 @@ of feature and attribute information from a spatial datasource.
ReadLayerMetadata,
WriteLayerMetadata,
CancelSupport,
CreateRenderer,
};

typedef QFlags<QgsVectorDataProvider::Capability> Capabilities;
@@ -477,6 +478,23 @@ Must be implemented by providers that support saving and loading styles to db re
%Docstring
It returns false by default.
Must be implemented by providers that support delete styles from db returning true
%End

virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const;
%Docstring
Creates a new vector layer feature renderer, using provider backend specific information.

The ``configuration`` map can be used to pass provider-specific configuration maps to the provider to
allow customisation of the returned renderer. Support and format of ``configuration`` varies by provider.

When called with an empty ``configuration`` map the provider's default renderer will be returned.

This method returns a new renderer and the caller takes ownership of the returned object.

Only providers which report the CreateRenderer capability will return a feature renderer. Other
providers will return None.

.. versionadded:: 3.2
%End

static QVariant convertValue( QVariant::Type type, const QString &value );
@@ -936,6 +936,9 @@ data source
.. versionadded:: 2.10
%End

virtual QString loadDefaultStyle( bool &resultFlag /Out/ );


QgsVectorLayerFeatureCounter *countSymbolFeatures();
%Docstring
Count features for symbols.
@@ -681,6 +681,11 @@ bool QgsVectorDataProvider::isDeleteStyleFromDatabaseSupported() const
return false;
}

QgsFeatureRenderer *QgsVectorDataProvider::createRenderer( const QVariantMap & ) const SIP_FACTORY
{
return nullptr;
}

void QgsVectorDataProvider::pushError( const QString &msg ) const
{
QgsDebugMsg( msg );
19 src/core/qgsvectordataprovider.h 100644 → 100755
@@ -40,6 +40,7 @@ typedef QHash<int, QString> QgsAttrPalIndexNameHash;
class QgsFeatureIterator;
class QgsTransaction;
class QgsFeedback;
class QgsFeatureRenderer;

#include "qgsfeaturerequest.h"

@@ -91,6 +92,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
ReadLayerMetadata = 1 << 21, //!< Provider can read layer metadata from data store. Since QGIS 3.0. See QgsDataProvider::layerMetadata()
WriteLayerMetadata = 1 << 22, //!< Provider can write layer metadata to the data store. Since QGIS 3.0. See QgsDataProvider::writeLayerMetadata()
CancelSupport = 1 << 23, //!< Supports interruption of pending queries from a separated thread. Since QGIS 3.2
CreateRenderer = 1 << 24, //!< Provider can create feature renderers using backend-specific formatting information. Since QGIS 3.2. See QgsVectorDataProvider::createRenderer().
};

Q_DECLARE_FLAGS( Capabilities, Capability )
@@ -479,6 +481,23 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
*/
virtual bool isDeleteStyleFromDatabaseSupported() const;

/**
* Creates a new vector layer feature renderer, using provider backend specific information.
*
* The \a configuration map can be used to pass provider-specific configuration maps to the provider to
* allow customisation of the returned renderer. Support and format of \a configuration varies by provider.
*
* When called with an empty \a configuration map the provider's default renderer will be returned.
*
* This method returns a new renderer and the caller takes ownership of the returned object.
*
* Only providers which report the CreateRenderer capability will return a feature renderer. Other
* providers will return nullptr.
*
* \since QGIS 3.2
*/
virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const;

static QVariant convertValue( QVariant::Type type, const QString &value );

/**
@@ -1465,10 +1465,22 @@ void QgsVectorLayer::setDataSource( const QString &dataSource, const QString &ba
// reset style if loading default style, style is missing, or geometry type has changed
if ( !renderer() || !legend() || geomType != geometryType() || loadDefaultStyleFlag )
{
// check if there is a default style / propertysheet defined
// for this layer and if so apply it
bool defaultLoadedFlag = false;
if ( loadDefaultStyleFlag )

if ( loadDefaultStyleFlag && isSpatial() && mDataProvider->capabilities() & QgsVectorDataProvider::CreateRenderer )
{
// first try to create a renderer directly from the data provider
std::unique_ptr< QgsFeatureRenderer > defaultRenderer( mDataProvider->createRenderer() );
if ( defaultRenderer )
{
defaultLoadedFlag = true;
setRenderer( defaultRenderer.release() );
}
}

// else check if there is a default style / propertysheet defined
// for this layer and if so apply it
if ( !defaultLoadedFlag && loadDefaultStyleFlag )
{
loadDefaultStyle( defaultLoadedFlag );
}
@@ -1486,6 +1498,23 @@ void QgsVectorLayer::setDataSource( const QString &dataSource, const QString &ba
emit repaintRequested();
}

QString QgsVectorLayer::loadDefaultStyle( bool &resultFlag )
{
if ( isSpatial() && mDataProvider->capabilities() & QgsVectorDataProvider::CreateRenderer )
{
// first try to create a renderer directly from the data provider
std::unique_ptr< QgsFeatureRenderer > defaultRenderer( mDataProvider->createRenderer() );
if ( defaultRenderer )
{
resultFlag = true;
setRenderer( defaultRenderer.release() );
return QString();
}
}

return QgsMapLayer::loadDefaultStyle( resultFlag );
}


bool QgsVectorLayer::setDataProvider( QString const &provider )
{
@@ -930,6 +930,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, bool loadDefaultStyleFlag = false );

QString loadDefaultStyle( bool &resultFlag SIP_OUT ) override;

/**
* Count features for symbols.
* The method will return the feature counter task. You will need to

0 comments on commit 678f658

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