Skip to content
Permalink
Browse files
Only expose a single mapLayerFromString method in QgsProcessingUtils
  • Loading branch information
nyalldawson committed May 2, 2017
1 parent f84d703 commit 11fb72eef7f099a608ae0bf0007e524c5d7b9867
@@ -66,32 +66,14 @@ class QgsProcessingUtils
:rtype: list of QgsMapLayer
%End


static QgsMapLayer *mapLayerFromProject( const QString &string, QgsProject *project );
%Docstring
Interprets a ``string`` as a map layer from a project.

This method attempts to match a string to a project map layer, using
first the layer ID, then layer names, and finally layer source.
If the string matches a normalized version of any layer source
for layers in the specified ``project``, then those matching layers will be
returned.
.. seealso:: mapLayerFromString()
:rtype: QgsMapLayer
%End

static QgsMapLayer *mapLayerFromString( const QString &string ) /Factory/;
%Docstring
Interprets a string as a map layer. The method will attempt to
load a layer matching the passed ``string``. E.g. if the string is a file path,
then the layer at this file path will be loaded.
The caller takes responsibility for deleting the returned map layer.
.. seealso:: mapLayerFromProject()
:rtype: QgsMapLayer
%End

static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
%Docstring
Interprets a string as a map layer within the supplied ``context``. The method will attempt to
load a layer matching the passed ``string``. E.g. if the string matches a layer ID or name
within the current project this layer will be returned.
If the string is a file path and ``allowLoadingNewLayers`` is true, then the layer at this
file path will be loaded and added to the context's temporary layer store.
Ownership of the layer remains with the ``context`` or the context's current project.
:rtype: QgsMapLayer
%End

@@ -123,11 +123,12 @@ def setValue(self, value):

def value(self):
layers = ''
context = dataobjects.createContext()
for i in range(self.layersTree.topLevelItemCount()):
item = self.layersTree.topLevelItem(i)
if item:
layerName = item.text(0)
layer = QgsProcessingUtils.mapLayerFromProject(layerName, QgsProject.instance())
layer = QgsProcessingUtils.mapLayerFromString(layerName, context)
if not layer:
continue

@@ -130,7 +130,7 @@ class ProjectionSettingRestorer
};
///@endcond PRIVATE

QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string )
QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string )
{
if ( QFileInfo::exists( string ) )
{
@@ -170,7 +170,7 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
if ( !allowLoadingNewLayers )
return nullptr;

layer = mapLayerFromString( string );
layer = loadMapLayerFromString( string );
if ( layer )
{
context.temporaryLayerStore().addMapLayer( layer );
@@ -79,28 +79,14 @@ class CORE_EXPORT QgsProcessingUtils
*/
static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );


/**
* Interprets a \a string as a map layer from a project.
*
* This method attempts to match a string to a project map layer, using
* first the layer ID, then layer names, and finally layer source.
* If the string matches a normalized version of any layer source
* for layers in the specified \a project, then those matching layers will be
* returned.
* \see mapLayerFromString()
* Interprets a string as a map layer within the supplied \a context. The method will attempt to
* load a layer matching the passed \a string. E.g. if the string matches a layer ID or name
* within the current project this layer will be returned.
* If the string is a file path and \a allowLoadingNewLayers is true, then the layer at this
* file path will be loaded and added to the context's temporary layer store.
* Ownership of the layer remains with the \a context or the context's current project.
*/
static QgsMapLayer *mapLayerFromProject( const QString &string, QgsProject *project );

/**
* Interprets a string as a map layer. The method will attempt to
* load a layer matching the passed \a string. E.g. if the string is a file path,
* then the layer at this file path will be loaded.
* The caller takes responsibility for deleting the returned map layer.
* \see mapLayerFromProject()
*/
static QgsMapLayer *mapLayerFromString( const QString &string ) SIP_FACTORY;

static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );

/**
@@ -139,6 +125,28 @@ class CORE_EXPORT QgsProcessingUtils
static bool canUseLayer( const QgsVectorLayer *layer,
const QList< QgsWkbTypes::GeometryType > &geometryTypes );

/**
* Interprets a \a string as a map layer from a project.
*
* This method attempts to match a string to a project map layer, using
* first the layer ID, then layer names, and finally layer source.
* If the string matches a normalized version of any layer source
* for layers in the specified \a project, then those matching layers will be
* returned.
* \see mapLayerFromString()
*/
static QgsMapLayer *mapLayerFromProject( const QString &string, QgsProject *project );

/**
* Interprets a string as a map layer. The method will attempt to
* load a layer matching the passed \a string. E.g. if the string is a file path,
* then the layer at this file path will be loaded.
* The caller takes responsibility for deleting the returned map layer.
*/
static QgsMapLayer *loadMapLayerFromString( const QString &string );

friend class TestQgsProcessing;

};

#endif // QGSPROCESSINGUTILS_H
@@ -365,15 +365,15 @@ void TestQgsProcessing::mapLayerFromString()
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( v1->id(), &p ), v1 );

// test mapLayerFromString
QgsMapLayer *l = QgsProcessingUtils::mapLayerFromString( raster2 );
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster2 );
QVERIFY( l->isValid() );
QCOMPARE( l->type(), QgsMapLayer::RasterLayer );
delete l;
l = QgsProcessingUtils::mapLayerFromString( QString() );
l = QgsProcessingUtils::loadMapLayerFromString( QString() );
QVERIFY( !l );
l = QgsProcessingUtils::mapLayerFromString( QStringLiteral( "so much room for activities!" ) );
l = QgsProcessingUtils::loadMapLayerFromString( QStringLiteral( "so much room for activities!" ) );
QVERIFY( !l );
l = QgsProcessingUtils::mapLayerFromString( testDataDir + "multipoint.shp" );
l = QgsProcessingUtils::loadMapLayerFromString( testDataDir + "multipoint.shp" );
QVERIFY( l->isValid() );
QCOMPARE( l->type(), QgsMapLayer::VectorLayer );
delete l;

0 comments on commit 11fb72e

Please sign in to comment.