Skip to content
Permalink
Browse files
Allow QgsDataProvider access through QgsMapLayer
Avoids having to cast to QgsRasterLayer/QgsVectorLayer before
accessing the common QgsDataProvider members
  • Loading branch information
nyalldawson committed Apr 18, 2017
1 parent 87a6708 commit 3535ee2d5566f241dcca55943c3f26fbef12bd33
@@ -95,6 +95,13 @@ Returns the layer's unique ID, which is used to access this layer from QgsProjec
:rtype: str
%End

virtual QgsDataProvider *dataProvider();
%Docstring
Returns the layer's data provider.
:rtype: QgsDataProvider
%End


QString originalName() const;
%Docstring
Returns the original name of the layer.
@@ -383,12 +383,7 @@ Returns a comment for the data in the layer
:rtype: str
%End

QgsVectorDataProvider *dataProvider();
%Docstring
Returns the data provider
:rtype: QgsVectorDataProvider
%End

virtual QgsVectorDataProvider *dataProvider();

void setProviderEncoding( const QString &encoding );
%Docstring
@@ -103,12 +103,12 @@ class QgsRasterLayer : QgsMapLayer
QString bandName( int bandNoInt ) const;

/** Returns the data provider */
QgsRasterDataProvider* dataProvider();
virtual QgsRasterDataProvider* dataProvider();

/** Returns the data provider in a const-correct manner
@note available in python bindings as constDataProvider()
*/
const QgsRasterDataProvider* dataProvider() const /PyName=constDataProvider/;
virtual const QgsRasterDataProvider* dataProvider() const /PyName=constDataProvider/;

/** Synchronises with changes in the datasource */
virtual void reload();
@@ -215,13 +215,20 @@ class CORE_EXPORT QgsLayerTreeGroup : public QgsLayerTreeNode

//! Whether the group is mutually exclusive (i.e. only one child can be checked at a time)
bool mMutuallyExclusive;
//! Keeps track which child has been most recently selected
//! (so if the whole group is unchecked and checked again, we know which child to check)

/**
* Keeps track which child has been most recently selected
* (so if the whole group is unchecked and checked again, we know which child to check)
*/
int mMutuallyExclusiveChildIndex;

private:

#ifdef SIP_RUN

/**
* Copies are not allowed
*/
QgsLayerTreeGroup( const QgsLayerTreeGroup &other );
#endif

@@ -146,25 +146,7 @@ void QgsLayerTreeLayer::writeXml( QDomElement &parentElement )
if ( mRef.layer )
{
elem.setAttribute( "source", mRef.layer->publicSource() );
QString providerKey;
switch ( mRef.layer->type() )
{
case QgsMapLayer::VectorLayer:
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mRef.layer );
providerKey = vl->dataProvider()->name();
break;
}
case QgsMapLayer::RasterLayer:
{
QgsRasterLayer *rl = qobject_cast< QgsRasterLayer * >( mRef.layer );
providerKey = rl->dataProvider()->name();
break;
}
case QgsMapLayer::PluginLayer:
break;
}
elem.setAttribute( "providerKey", providerKey );
elem.setAttribute( "providerKey", mRef.layer->dataProvider() ? mRef.layer->dataProvider()->name() : QString() );
}

elem.setAttribute( QStringLiteral( "checked" ), mChecked ? QStringLiteral( "Qt::Checked" ) : QStringLiteral( "Qt::Unchecked" ) );
@@ -131,6 +131,10 @@ class CORE_EXPORT QgsLayerTreeLayer : public QgsLayerTreeNode
private:

#ifdef SIP_RUN

/**
* Copies are not allowed
*/
QgsLayerTreeLayer( const QgsLayerTreeLayer &other );
#endif
};
@@ -450,7 +450,6 @@ class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColo
*/
QList< QColor > colors() const;

// QgsColorRamp interface
virtual double value( int index ) const override;
virtual QColor color( double value ) const override;
virtual QString type() const override { return QStringLiteral( "preset" ); }
@@ -459,10 +458,8 @@ class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColo
virtual QgsStringMap properties() const override;
int count() const override;

// QgsColorScheme interface
QString schemeName() const override { return QStringLiteral( "preset" ); }
QgsNamedColorList fetchColors( const QString &context = QString(),
const QColor &baseColor = QColor() ) override;
QgsNamedColorList fetchColors( const QString &context = QString(), const QColor &baseColor = QColor() ) override;
bool isEditable() const override { return true; }

private:
@@ -35,6 +35,7 @@
#include "qgsrendercontext.h"
#include "qgsmaplayerdependency.h"

class QgsDataProvider;
class QgsMapLayerLegend;
class QgsMapLayerRenderer;
class QgsMapLayerStyleManager;
@@ -128,6 +129,17 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
QString name() const;

/**
* Returns the layer's data provider.
*/

This comment has been minimized.

Copy link
@elpaso

elpaso Nov 27, 2017

Contributor

Forgot \since ?

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Nov 27, 2017

Author Collaborator

I guess I left it off since it didn't really change the API - python code has been able to call layer.dataProvider() since forever.

This comment has been minimized.

Copy link
@elpaso

elpaso Nov 27, 2017

Contributor

That makes sense (I thought \since was not just for Python, sorry for the noise)

virtual QgsDataProvider *dataProvider() { return nullptr; }

/**
* Returns the layer's data provider in a const-correct manner
* \note not available in Python bindings
*/
virtual const QgsDataProvider *dataProvider() const SIP_SKIP { return nullptr; }

/** Returns the original name of the layer.
* \returns the original layer name
*/
@@ -98,11 +98,7 @@ bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex
if ( mExceptList.contains( layer ) )
return false;

QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
if ( vl && mExcludedProviders.contains( vl->dataProvider()->name() ) )
return false;
QgsRasterLayer *rl = qobject_cast<QgsRasterLayer *>( layer );
if ( rl && mExcludedProviders.contains( rl->dataProvider()->name() ) )
if ( mExcludedProviders.contains( layer->dataProvider()->name() ) )
return false;

if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
@@ -122,7 +118,7 @@ bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex
mFilters.testFlag( HasGeometry );
if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer )
{
if ( vl )
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( layer ) )
{
if ( mFilters.testFlag( HasGeometry ) && vl->hasGeometryType() )
return true;
@@ -65,26 +65,9 @@ struct _LayerRef
layer->name() != name )
return false;

switch ( layer->type() )
{
case QgsMapLayer::VectorLayer:
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer );
if ( vl->dataProvider()->name() != provider )
return false;
break;
}
case QgsMapLayer::RasterLayer:
{
QgsRasterLayer *rl = qobject_cast< QgsRasterLayer * >( layer );
if ( rl->dataProvider()->name() != provider )
return false;
break;
}
case QgsMapLayer::PluginLayer:
break;
if ( layer->dataProvider()->name() != provider )
return false;

}
return true;
}
};
@@ -108,23 +108,21 @@ static void _addLayerTreeNodeToUriList( QgsLayerTreeNode *node, QgsMimeDataUtils
else if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
if ( !nodeLayer->layer() )
QgsMapLayer *layer = nodeLayer->layer();
if ( !layer )
return;

QgsMimeDataUtils::Uri uri;
if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ) )
uri.name = layer->name();
uri.uri = layer->dataProvider()->dataSourceUri();
uri.providerKey = layer->dataProvider()->name();
if ( layer->type() == QgsMapLayer::VectorLayer )
{
uri.layerType = QStringLiteral( "vector" );
uri.name = vlayer->name();
uri.providerKey = vlayer->dataProvider()->name();
uri.uri = vlayer->dataProvider()->dataSourceUri();
}
else if ( QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( nodeLayer->layer() ) )
else if ( layer->type() == QgsMapLayer::RasterLayer )
{
uri.layerType = QStringLiteral( "raster" );
uri.name = rlayer->name();
uri.providerKey = rlayer->dataProvider()->name();
uri.uri = rlayer->dataProvider()->dataSourceUri();
}
else
{
@@ -525,22 +525,22 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
* Get metadata, dependent on the provider type, that will be display in the metadata tab of the layer properties.
* \returns The provider metadata
*/
virtual QVariantMap metadata() const { return QVariantMap(); };
virtual QVariantMap metadata() const { return QVariantMap(); }

/**
* Get the translated metadata key.
* \param mdKey The metadata key
* \returns The translated metadata value
*/
virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; };
virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }

/**
* Get the translated metadata value.
* \param mdKey The metadata key
* \param value The metadata value
* \returns The translated metadata value
*/
virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); };
virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }

signals:

@@ -33,6 +33,7 @@
#include "qgsfeaturerequest.h"
#include "qgsfields.h"
#include "qgssnapper.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorsimplifymethod.h"
#include "qgseditformconfig.h"
#include "qgsattributetableconfig.h"
@@ -63,7 +64,6 @@ class QgsRelation;
class QgsRelationManager;
class QgsSingleSymbolRenderer;
class QgsSymbol;
class QgsVectorDataProvider;
class QgsVectorLayerJoinInfo;
class QgsVectorLayerEditBuffer;
class QgsVectorLayerJoinBuffer;
@@ -437,13 +437,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
QString displayExpression() const;

//! Returns the data provider
QgsVectorDataProvider *dataProvider();

/** Returns the data provider in a const-correct manner
* \note not available in Python bindings
*/
const QgsVectorDataProvider *dataProvider() const SIP_SKIP;
QgsVectorDataProvider *dataProvider() override;
const QgsVectorDataProvider *dataProvider() const override SIP_SKIP;

//! Sets the textencoding of the data provider
void setProviderEncoding( const QString &encoding );
@@ -32,6 +32,7 @@
#include "qgis.h"
#include "qgsmaplayer.h"
#include "qgsraster.h"
#include "qgsrasterdataprovider.h"
#include "qgsrasterpipe.h"
#include "qgsrasterviewport.h"
#include "qgsrasterminmaxorigin.h"
@@ -247,13 +248,12 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
//! \brief Get the name of a band given its number
QString bandName( int bandNoInt ) const;

//! Returns the data provider
QgsRasterDataProvider *dataProvider();
QgsRasterDataProvider *dataProvider() override;

/** Returns the data provider in a const-correct manner
\note available in Python bindings as constDataProvider()
*/
const QgsRasterDataProvider *dataProvider() const;
const QgsRasterDataProvider *dataProvider() const override;

//! Synchronises with changes in the datasource
virtual void reload() override;

0 comments on commit 3535ee2

Please sign in to comment.