Skip to content

Commit

Permalink
Allow QgsVectorDataProviders to create QgsFeatureRenderers
Browse files Browse the repository at this point in the history
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 7, 2018
1 parent b42c893 commit 678f658
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
18 changes: 18 additions & 0 deletions python/core/qgsvectordataprovider.sip.in 100644 → 100755
Expand Up @@ -51,6 +51,7 @@ of feature and attribute information from a spatial datasource.
ReadLayerMetadata,
WriteLayerMetadata,
CancelSupport,
CreateRenderer,
};

typedef QFlags<QgsVectorDataProvider::Capability> Capabilities;
Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 3 additions & 0 deletions python/core/qgsvectorlayer.sip.in
Expand Up @@ -936,6 +936,9 @@ data source
.. versionadded:: 2.10
%End

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


QgsVectorLayerFeatureCounter *countSymbolFeatures();
%Docstring
Count features for symbols.
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -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 );
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsvectordataprovider.h 100644 → 100755
Expand Up @@ -40,6 +40,7 @@ typedef QHash<int, QString> QgsAttrPalIndexNameHash;
class QgsFeatureIterator;
class QgsTransaction;
class QgsFeedback;
class QgsFeatureRenderer;

#include "qgsfeaturerequest.h"

Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );

/**
Expand Down
35 changes: 32 additions & 3 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -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 );
}
Expand All @@ -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 )
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -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
Expand Down

0 comments on commit 678f658

Please sign in to comment.