Navigation Menu

Skip to content

Commit

Permalink
Only expose a single mapLayerFromString method in QgsProcessingUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 2, 2017
1 parent f84d703 commit 11fb72e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 51 deletions.
30 changes: 6 additions & 24 deletions python/core/processing/qgsprocessingutils.sip
Expand Up @@ -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

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

Expand Down
4 changes: 2 additions & 2 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -130,7 +130,7 @@ class ProjectionSettingRestorer
};
///@endcond PRIVATE

QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string )
QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string )
{
if ( QFileInfo::exists( string ) )
{
Expand Down Expand Up @@ -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 );
Expand Down
48 changes: 28 additions & 20 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -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 );

/**
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -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;
Expand Down

0 comments on commit 11fb72e

Please sign in to comment.