Navigation Menu

Skip to content

Commit

Permalink
Add sourceName() method to QgsFeatureSource
Browse files Browse the repository at this point in the history
Sometimes it's necessary to retrieve the name of a source...
  • Loading branch information
nyalldawson committed Jun 26, 2017
1 parent d88d52b commit 7ae1ee9
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 11 deletions.
6 changes: 6 additions & 0 deletions python/core/qgsfeaturesource.sip
Expand Up @@ -33,6 +33,12 @@ class QgsFeatureSource
:rtype: QgsFeatureIterator
%End

virtual QString sourceName() const = 0;
%Docstring
Returns a friendly display name for the source. The returned value can be an empty string.
:rtype: str
%End

virtual QgsCoordinateReferenceSystem sourceCrs() const = 0;
%Docstring
Returns the coordinate reference system for features in the source.
Expand Down
1 change: 1 addition & 0 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -128,6 +128,7 @@ Bitmask of all provider's editing capabilities

virtual QgsRectangle sourceExtent() const;

virtual QString sourceName() const;

virtual QString dataComment() const;
%Docstring
Expand Down
2 changes: 2 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -700,6 +700,8 @@ Return the provider type for this layer

virtual QgsCoordinateReferenceSystem sourceCrs() const;

virtual QString sourceName() const;


virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
%Docstring
Expand Down
8 changes: 8 additions & 0 deletions python/core/qgsvectorlayerfeatureiterator.sip
Expand Up @@ -168,11 +168,19 @@ class QgsVectorLayerSelectedFeatureSource : QgsFeatureSource
%End

virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const;

virtual QgsCoordinateReferenceSystem sourceCrs() const;

virtual QgsFields fields() const;

virtual QgsWkbTypes::Type wkbType() const;

virtual long featureCount() const;

virtual QString sourceName() const;



};

/************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ImportIntoPostGIS.py
Expand Up @@ -137,7 +137,7 @@ def processAlgorithm(self, parameters, context, feedback):
if table:
table.strip()
if not table or table == '':
table = layer.name()
table = source.name()
table = table.replace('.', '_')
table = table.replace(' ', '').lower()[0:62]
providerName = 'postgres'
Expand Down
6 changes: 6 additions & 0 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -508,3 +508,9 @@ long QgsProcessingFeatureSource::featureCount() const
{
return mSource->featureCount();
}

QString QgsProcessingFeatureSource::sourceName() const
{
return mSource->sourceName();

}
11 changes: 6 additions & 5 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -244,11 +244,12 @@ class QgsProcessingFeatureSource : public QgsFeatureSource

~QgsProcessingFeatureSource();

virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
virtual QgsCoordinateReferenceSystem sourceCrs() const override;
virtual QgsFields fields() const override;
virtual QgsWkbTypes::Type wkbType() const override;
virtual long featureCount() const override;
QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
QgsCoordinateReferenceSystem sourceCrs() const override;
QgsFields fields() const override;
QgsWkbTypes::Type wkbType() const override;
long featureCount() const override;
QString sourceName() const override;

private:

Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsfeaturesource.h
Expand Up @@ -46,6 +46,11 @@ class CORE_EXPORT QgsFeatureSource
*/
virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const = 0;

/**
* Returns a friendly display name for the source. The returned value can be an empty string.
*/
virtual QString sourceName() const = 0;

/**
* Returns the coordinate reference system for features in the source.
*/
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -161,6 +161,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat

QgsCoordinateReferenceSystem sourceCrs() const override;
QgsRectangle sourceExtent() const override;
QString sourceName() const override { return QString(); }

/**
* Return a short comment for the data that this provider is
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -312,6 +312,11 @@ QgsCoordinateReferenceSystem QgsVectorLayer::sourceCrs() const
return crs();
}

QString QgsVectorLayer::sourceName() const
{
return name();
}

void QgsVectorLayer::reload()
{
if ( mDataProvider )
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -698,6 +698,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
QString providerType() const;

QgsCoordinateReferenceSystem sourceCrs() const override;
QString sourceName() const override;

/** Reads vector layer specific state from project file Dom node.
* \note Called by QgsMapLayer::readXml().
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -1064,6 +1064,7 @@ QgsVectorLayerSelectedFeatureSource::QgsVectorLayerSelectedFeatureSource( QgsVec
: mSource( layer )
, mSelectedFeatureIds( layer->selectedFeatureIds() )
, mWkbType( layer->wkbType() )
, mName( layer->name() )
{}

QgsFeatureIterator QgsVectorLayerSelectedFeatureSource::getFeatures( const QgsFeatureRequest &request ) const
Expand Down Expand Up @@ -1103,3 +1104,8 @@ long QgsVectorLayerSelectedFeatureSource::featureCount() const
{
return mSelectedFeatureIds.count();
}

QString QgsVectorLayerSelectedFeatureSource::sourceName() const
{
return mName;
}
13 changes: 8 additions & 5 deletions src/core/qgsvectorlayerfeatureiterator.h
Expand Up @@ -282,18 +282,21 @@ class CORE_EXPORT QgsVectorLayerSelectedFeatureSource : public QgsFeatureSource
*/
QgsVectorLayerSelectedFeatureSource( QgsVectorLayer *layer );

virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
virtual QgsCoordinateReferenceSystem sourceCrs() const override;
virtual QgsFields fields() const override;
virtual QgsWkbTypes::Type wkbType() const override;
virtual long featureCount() const override;
QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
QgsCoordinateReferenceSystem sourceCrs() const override;
QgsFields fields() const override;
QgsWkbTypes::Type wkbType() const override;
long featureCount() const override;
QString sourceName() const override;


private:

// ideally this wouldn't be mutable, but QgsVectorLayerFeatureSource has non-const getFeatures()
mutable QgsVectorLayerFeatureSource mSource;
QgsFeatureIds mSelectedFeatureIds;
QgsWkbTypes::Type mWkbType = QgsWkbTypes::Unknown;
QString mName;

};

Expand Down

0 comments on commit 7ae1ee9

Please sign in to comment.