Skip to content

Commit c575c4a

Browse files
committedMay 2, 2017
Fix build
1 parent f81971d commit c575c4a

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed
 

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,46 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromProject( const QString &string, Qgs
108108
}
109109
return nullptr;
110110
}
111+
112+
QgsMapLayer *QgsProcessingUtils::mapLayerFromStore( const QString &string, QgsMapLayerStore *store )
113+
{
114+
if ( string.isEmpty() )
115+
return nullptr;
116+
117+
QList< QgsMapLayer * > layers = store->mapLayers().values();
118+
119+
layers.erase( std::remove_if( layers.begin(), layers.end(), []( QgsMapLayer * layer )
120+
{
121+
switch ( layer->type() )
122+
{
123+
case QgsMapLayer::VectorLayer:
124+
return !canUseLayer( qobject_cast< QgsVectorLayer * >( layer ) );
125+
case QgsMapLayer::RasterLayer:
126+
return !canUseLayer( qobject_cast< QgsRasterLayer * >( layer ) );
127+
case QgsMapLayer::PluginLayer:
128+
return true;
129+
}
130+
return true;
131+
} ), layers.end() );
132+
133+
Q_FOREACH ( QgsMapLayer *l, layers )
134+
{
135+
if ( l->id() == string )
136+
return l;
137+
}
138+
Q_FOREACH ( QgsMapLayer *l, layers )
139+
{
140+
if ( l->name() == string )
141+
return l;
142+
}
143+
Q_FOREACH ( QgsMapLayer *l, layers )
144+
{
145+
if ( normalizeLayerSource( l->source() ) == normalizeLayerSource( string ) )
146+
return l;
147+
}
148+
return nullptr;
149+
}
150+
111151
///@cond PRIVATE
112152
class ProjectionSettingRestorer
113153
{
@@ -163,7 +203,7 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
163203
if ( layer )
164204
return layer;
165205

166-
layer = mapLayerFromProject( string, &context.temporaryLayerStore() );
206+
layer = mapLayerFromStore( string, &context.temporaryLayerStore() );
167207
if ( layer )
168208
return layer;
169209

‎src/core/processing/qgsprocessingutils.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
class QgsProject;
2929
class QgsProcessingContext;
30+
class QgsMapLayerStore;
3031

3132
#include <QString>
3233

@@ -134,7 +135,7 @@ class CORE_EXPORT QgsProcessingUtils
134135

135136
static bool canUseLayer( const QgsRasterLayer *layer );
136137
static bool canUseLayer( const QgsVectorLayer *layer,
137-
const QList< QgsWkbTypes::GeometryType > &geometryTypes );
138+
const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >() );
138139

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

152+
/**
153+
* Interprets a \a string as a map layer from a store.
154+
*
155+
* This method attempts to match a string to a store map layer, using
156+
* first the layer ID, then layer names, and finally layer source.
157+
* If the string matches a normalized version of any layer source
158+
* for layers in the specified \a store, then those matching layers will be
159+
* returned.
160+
* \see mapLayerFromString()
161+
*/
162+
static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store );
163+
151164
/**
152165
* Interprets a string as a map layer. The method will attempt to
153166
* load a layer matching the passed \a string. E.g. if the string is a file path,

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class TestQgsProcessing: public QObject
105105
void compatibleLayers();
106106
void normalizeLayerSource();
107107
void mapLayers();
108+
void mapLayerFromStore();
108109
void mapLayerFromString();
109110
void algorithm();
110111
void features();
@@ -381,6 +382,40 @@ void TestQgsProcessing::mapLayers()
381382
delete l;
382383
}
383384

385+
void TestQgsProcessing::mapLayerFromStore()
386+
{
387+
// test mapLayerFromStore
388+
389+
QgsMapLayerStore store;
390+
391+
// add a bunch of layers to a project
392+
QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
393+
QString raster1 = testDataDir + "tenbytenraster.asc";
394+
QString raster2 = testDataDir + "landsat.tif";
395+
QFileInfo fi1( raster1 );
396+
QgsRasterLayer *r1 = new QgsRasterLayer( fi1.filePath(), "R1" );
397+
QVERIFY( r1->isValid() );
398+
QFileInfo fi2( raster2 );
399+
QgsRasterLayer *r2 = new QgsRasterLayer( fi2.filePath(), "ar2" );
400+
QVERIFY( r2->isValid() );
401+
402+
QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V4", "memory" );
403+
QgsVectorLayer *v2 = new QgsVectorLayer( "Point", "v1", "memory" );
404+
store.addMapLayers( QList<QgsMapLayer *>() << r1 << r2 << v1 << v2 );
405+
406+
QVERIFY( ! QgsProcessingUtils::mapLayerFromStore( QString(), nullptr ) );
407+
QVERIFY( ! QgsProcessingUtils::mapLayerFromStore( QStringLiteral( "v1" ), nullptr ) );
408+
QVERIFY( ! QgsProcessingUtils::mapLayerFromStore( QString(), &store ) );
409+
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( raster1, &store ), r1 );
410+
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( raster2, &store ), r2 );
411+
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( "R1", &store ), r1 );
412+
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( "ar2", &store ), r2 );
413+
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( "V4", &store ), v1 );
414+
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( "v1", &store ), v2 );
415+
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( r1->id(), &store ), r1 );
416+
QCOMPARE( QgsProcessingUtils::mapLayerFromStore( v1->id(), &store ), v1 );
417+
}
418+
384419
void TestQgsProcessing::mapLayerFromString()
385420
{
386421
// test mapLayerFromString

0 commit comments

Comments
 (0)