diff --git a/python/core/qgsvectordataprovider.sip.in b/python/core/qgsvectordataprovider.sip.in old mode 100644 new mode 100755 index 3aef648718e..d102ada2c4d --- a/python/core/qgsvectordataprovider.sip.in +++ b/python/core/qgsvectordataprovider.sip.in @@ -51,6 +51,7 @@ of feature and attribute information from a spatial datasource. ReadLayerMetadata, WriteLayerMetadata, CancelSupport, + CreateRenderer, }; typedef QFlags 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 ); diff --git a/python/core/qgsvectorlayer.sip.in b/python/core/qgsvectorlayer.sip.in index 6487ddcfdb0..a4e7680918b 100644 --- a/python/core/qgsvectorlayer.sip.in +++ b/python/core/qgsvectorlayer.sip.in @@ -936,6 +936,9 @@ data source .. versionadded:: 2.10 %End + virtual QString loadDefaultStyle( bool &resultFlag /Out/ ); + + QgsVectorLayerFeatureCounter *countSymbolFeatures(); %Docstring Count features for symbols. diff --git a/src/core/qgsvectordataprovider.cpp b/src/core/qgsvectordataprovider.cpp index 4cd3f3f30c8..1a6b264227d 100644 --- a/src/core/qgsvectordataprovider.cpp +++ b/src/core/qgsvectordataprovider.cpp @@ -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 ); diff --git a/src/core/qgsvectordataprovider.h b/src/core/qgsvectordataprovider.h old mode 100644 new mode 100755 index 3bb14d1ec57..8f5e09a4c09 --- a/src/core/qgsvectordataprovider.h +++ b/src/core/qgsvectordataprovider.h @@ -40,6 +40,7 @@ typedef QHash 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 ); /** diff --git a/src/core/qgsvectorlayer.cpp b/src/core/qgsvectorlayer.cpp index 04be94ce224..e9fc3a0b9c7 100644 --- a/src/core/qgsvectorlayer.cpp +++ b/src/core/qgsvectorlayer.cpp @@ -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 ) { diff --git a/src/core/qgsvectorlayer.h b/src/core/qgsvectorlayer.h index dae3dab7a1f..5af9aae61c3 100644 --- a/src/core/qgsvectorlayer.h +++ b/src/core/qgsvectorlayer.h @@ -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