Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 2, 2017
1 parent f81971d commit c575c4a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
42 changes: 41 additions & 1 deletion src/core/processing/qgsprocessingutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,46 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromProject( const QString &string, Qgs
}
return nullptr;
}

QgsMapLayer *QgsProcessingUtils::mapLayerFromStore( const QString &string, QgsMapLayerStore *store )
{
if ( string.isEmpty() )
return nullptr;

QList< QgsMapLayer * > layers = store->mapLayers().values();

layers.erase( std::remove_if( layers.begin(), layers.end(), []( QgsMapLayer * layer )
{
switch ( layer->type() )
{
case QgsMapLayer::VectorLayer:
return !canUseLayer( qobject_cast< QgsVectorLayer * >( layer ) );
case QgsMapLayer::RasterLayer:
return !canUseLayer( qobject_cast< QgsRasterLayer * >( layer ) );
case QgsMapLayer::PluginLayer:
return true;
}
return true;
} ), layers.end() );

Q_FOREACH ( QgsMapLayer *l, layers )
{
if ( l->id() == string )
return l;
}
Q_FOREACH ( QgsMapLayer *l, layers )
{
if ( l->name() == string )
return l;
}
Q_FOREACH ( QgsMapLayer *l, layers )
{
if ( normalizeLayerSource( l->source() ) == normalizeLayerSource( string ) )
return l;
}
return nullptr;
}

///@cond PRIVATE
class ProjectionSettingRestorer
{
Expand Down Expand Up @@ -163,7 +203,7 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
if ( layer )
return layer;

layer = mapLayerFromProject( string, &context.temporaryLayerStore() );
layer = mapLayerFromStore( string, &context.temporaryLayerStore() );
if ( layer )
return layer;

Expand Down
15 changes: 14 additions & 1 deletion src/core/processing/qgsprocessingutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

class QgsProject;
class QgsProcessingContext;
class QgsMapLayerStore;

#include <QString>

Expand Down Expand Up @@ -134,7 +135,7 @@ class CORE_EXPORT QgsProcessingUtils

static bool canUseLayer( const QgsRasterLayer *layer );
static bool canUseLayer( const QgsVectorLayer *layer,
const QList< QgsWkbTypes::GeometryType > &geometryTypes );
const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >() );

/**
* Interprets a \a string as a map layer from a project.
Expand All @@ -148,6 +149,18 @@ class CORE_EXPORT QgsProcessingUtils
*/
static QgsMapLayer *mapLayerFromProject( const QString &string, QgsProject *project );

/**
* Interprets a \a string as a map layer from a store.
*
* This method attempts to match a string to a store 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 store, then those matching layers will be
* returned.
* \see mapLayerFromString()
*/
static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store );

/**
* 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,
Expand Down
35 changes: 35 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class TestQgsProcessing: public QObject
void compatibleLayers();
void normalizeLayerSource();
void mapLayers();
void mapLayerFromStore();
void mapLayerFromString();
void algorithm();
void features();
Expand Down Expand Up @@ -381,6 +382,40 @@ void TestQgsProcessing::mapLayers()
delete l;
}

void TestQgsProcessing::mapLayerFromStore()
{
// test mapLayerFromStore

QgsMapLayerStore store;

// add a bunch of layers to a project
QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
QString raster1 = testDataDir + "tenbytenraster.asc";
QString raster2 = testDataDir + "landsat.tif";
QFileInfo fi1( raster1 );
QgsRasterLayer *r1 = new QgsRasterLayer( fi1.filePath(), "R1" );
QVERIFY( r1->isValid() );
QFileInfo fi2( raster2 );
QgsRasterLayer *r2 = new QgsRasterLayer( fi2.filePath(), "ar2" );
QVERIFY( r2->isValid() );

QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V4", "memory" );
QgsVectorLayer *v2 = new QgsVectorLayer( "Point", "v1", "memory" );
store.addMapLayers( QList<QgsMapLayer *>() << r1 << r2 << v1 << v2 );

QVERIFY( ! QgsProcessingUtils::mapLayerFromStore( QString(), nullptr ) );
QVERIFY( ! QgsProcessingUtils::mapLayerFromStore( QStringLiteral( "v1" ), nullptr ) );
QVERIFY( ! QgsProcessingUtils::mapLayerFromStore( QString(), &store ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( raster1, &store ), r1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( raster2, &store ), r2 );
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( "R1", &store ), r1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( "ar2", &store ), r2 );
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( "V4", &store ), v1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( "v1", &store ), v2 );
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( r1->id(), &store ), r1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( v1->id(), &store ), v1 );
}

void TestQgsProcessing::mapLayerFromString()
{
// test mapLayerFromString
Expand Down

0 comments on commit c575c4a

Please sign in to comment.