Skip to content

Commit d2ce9c6

Browse files
committedMay 2, 2017
Allow access to project's internal layer store, remove some duplicate code
1 parent e1cff82 commit d2ce9c6

File tree

6 files changed

+42
-70
lines changed

6 files changed

+42
-70
lines changed
 

‎python/core/qgsproject.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ class QgsProject : QObject, QgsExpressionContextGenerator
422422
*/
423423
const QgsLabelingEngineSettings &labelingEngineSettings() const;
424424

425+
QgsMapLayerStore *layerStore();
426+
425427
int count() const;
426428

427429
QgsMapLayer *mapLayer( const QString &layerId ) const;

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,6 @@ QList<QgsMapLayer *> QgsProcessingUtils::compatibleLayers( QgsProject *project,
8585
return layers;
8686
}
8787

88-
QgsMapLayer *QgsProcessingUtils::mapLayerFromProject( const QString &string, QgsProject *project )
89-
{
90-
if ( string.isEmpty() )
91-
return nullptr;
92-
93-
QList< QgsMapLayer * > layers = compatibleLayers( project, false );
94-
Q_FOREACH ( QgsMapLayer *l, layers )
95-
{
96-
if ( l->id() == string )
97-
return l;
98-
}
99-
Q_FOREACH ( QgsMapLayer *l, layers )
100-
{
101-
if ( l->name() == string )
102-
return l;
103-
}
104-
Q_FOREACH ( QgsMapLayer *l, layers )
105-
{
106-
if ( normalizeLayerSource( l->source() ) == normalizeLayerSource( string ) )
107-
return l;
108-
}
109-
return nullptr;
110-
}
111-
11288
QgsMapLayer *QgsProcessingUtils::mapLayerFromStore( const QString &string, QgsMapLayerStore *store )
11389
{
11490
if ( !store || string.isEmpty() )
@@ -199,9 +175,13 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
199175
return nullptr;
200176

201177
// prefer project layers
202-
QgsMapLayer *layer = mapLayerFromProject( string, context.project() );
203-
if ( layer )
204-
return layer;
178+
QgsMapLayer *layer = nullptr;
179+
if ( context.project() )
180+
{
181+
QgsMapLayer *layer = mapLayerFromStore( string, context.project()->layerStore() );
182+
if ( layer )
183+
return layer;
184+
}
205185

206186
layer = mapLayerFromStore( string, &context.temporaryLayerStore() );
207187
if ( layer )

‎src/core/processing/qgsprocessingutils.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,6 @@ class CORE_EXPORT QgsProcessingUtils
137137
static bool canUseLayer( const QgsVectorLayer *layer,
138138
const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >() );
139139

140-
/**
141-
* Interprets a \a string as a map layer from a project.
142-
*
143-
* This method attempts to match a string to a project map layer, using
144-
* first the layer ID, then layer names, and finally layer source.
145-
* If the string matches a normalized version of any layer source
146-
* for layers in the specified \a project, then those matching layers will be
147-
* returned.
148-
* \see mapLayerFromString()
149-
*/
150-
static QgsMapLayer *mapLayerFromProject( const QString &string, QgsProject *project );
151-
152140
/**
153141
* Interprets a \a string as a map layer from a store.
154142
*

‎src/core/qgsproject.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,16 @@ const QgsLabelingEngineSettings &QgsProject::labelingEngineSettings() const
10671067
return *mLabelingEngineSettings;
10681068
}
10691069

1070+
QgsMapLayerStore *QgsProject::layerStore()
1071+
{
1072+
return mLayerStore.get();
1073+
}
1074+
1075+
const QgsMapLayerStore *QgsProject::layerStore() const
1076+
{
1077+
return mLayerStore.get();
1078+
}
1079+
10701080
QList<QgsVectorLayer *> QgsProject::avoidIntersectionsLayers() const
10711081
{
10721082
QList<QgsVectorLayer *> layers;

‎src/core/qgsproject.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,18 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
529529
// Functionality from QgsMapLayerRegistry
530530
//
531531

532+
/**
533+
* Returns a pointer to the project's internal layer store.
534+
* /since QGIS 3.0
535+
*/
536+
QgsMapLayerStore *layerStore();
537+
538+
/**
539+
* Returns a pointer to the project's internal layer store.
540+
* /since QGIS 3.0
541+
*/
542+
SIP_SKIP const QgsMapLayerStore *layerStore() const;
543+
532544
//! Returns the number of registered layers.
533545
int count() const;
534546

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -336,42 +336,22 @@ void TestQgsProcessing::normalizeLayerSource()
336336

337337
void TestQgsProcessing::mapLayers()
338338
{
339-
// test mapLayerFromProject
340-
341-
QgsProject p;
342-
343-
// add a bunch of layers to a project
344339
QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
345-
QString raster1 = testDataDir + "tenbytenraster.asc";
346-
QString raster2 = testDataDir + "landsat.tif";
347-
QFileInfo fi1( raster1 );
348-
QgsRasterLayer *r1 = new QgsRasterLayer( fi1.filePath(), "R1" );
349-
QVERIFY( r1->isValid() );
350-
QFileInfo fi2( raster2 );
351-
QgsRasterLayer *r2 = new QgsRasterLayer( fi2.filePath(), "ar2" );
352-
QVERIFY( r2->isValid() );
353-
354-
QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V4", "memory" );
355-
QgsVectorLayer *v2 = new QgsVectorLayer( "Point", "v1", "memory" );
356-
p.addMapLayers( QList<QgsMapLayer *>() << r1 << r2 << v1 << v2 );
340+
QString raster = testDataDir + "landsat.tif";
341+
QString vector = testDataDir + "points.shp";
357342

358-
QVERIFY( ! QgsProcessingUtils::mapLayerFromProject( QString(), nullptr ) );
359-
QVERIFY( ! QgsProcessingUtils::mapLayerFromProject( QStringLiteral( "v1" ), nullptr ) );
360-
QVERIFY( ! QgsProcessingUtils::mapLayerFromProject( QString(), &p ) );
361-
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( raster1, &p ), r1 );
362-
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( raster2, &p ), r2 );
363-
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( "R1", &p ), r1 );
364-
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( "ar2", &p ), r2 );
365-
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( "V4", &p ), v1 );
366-
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( "v1", &p ), v2 );
367-
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( r1->id(), &p ), r1 );
368-
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( v1->id(), &p ), v1 );
369-
370-
// test loadMapLayerFromString
371-
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster2 );
343+
// test loadMapLayerFromString with raster
344+
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster );
372345
QVERIFY( l->isValid() );
373346
QCOMPARE( l->type(), QgsMapLayer::RasterLayer );
374347
delete l;
348+
349+
//test with vector
350+
l = QgsProcessingUtils::loadMapLayerFromString( vector );
351+
QVERIFY( l->isValid() );
352+
QCOMPARE( l->type(), QgsMapLayer::VectorLayer );
353+
delete l;
354+
375355
l = QgsProcessingUtils::loadMapLayerFromString( QString() );
376356
QVERIFY( !l );
377357
l = QgsProcessingUtils::loadMapLayerFromString( QStringLiteral( "so much room for activities!" ) );

0 commit comments

Comments
 (0)
Please sign in to comment.