Skip to content
Permalink
Browse files
Allow access to project's internal layer store, remove some duplicate…
… code
  • Loading branch information
nyalldawson committed May 2, 2017
1 parent e1cff82 commit d2ce9c69b10dabaa5d05348391008d4ae6fe389e
@@ -422,6 +422,8 @@ class QgsProject : QObject, QgsExpressionContextGenerator
*/
const QgsLabelingEngineSettings &labelingEngineSettings() const;

QgsMapLayerStore *layerStore();

int count() const;

QgsMapLayer *mapLayer( const QString &layerId ) const;
@@ -85,30 +85,6 @@ QList<QgsMapLayer *> QgsProcessingUtils::compatibleLayers( QgsProject *project,
return layers;
}

QgsMapLayer *QgsProcessingUtils::mapLayerFromProject( const QString &string, QgsProject *project )
{
if ( string.isEmpty() )
return nullptr;

QList< QgsMapLayer * > layers = compatibleLayers( project, false );
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;
}

QgsMapLayer *QgsProcessingUtils::mapLayerFromStore( const QString &string, QgsMapLayerStore *store )
{
if ( !store || string.isEmpty() )
@@ -199,9 +175,13 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromString( const QString &string, QgsP
return nullptr;

// prefer project layers
QgsMapLayer *layer = mapLayerFromProject( string, context.project() );
if ( layer )
return layer;
QgsMapLayer *layer = nullptr;
if ( context.project() )
{
QgsMapLayer *layer = mapLayerFromStore( string, context.project()->layerStore() );
if ( layer )
return layer;
}

layer = mapLayerFromStore( string, &context.temporaryLayerStore() );
if ( layer )
@@ -137,18 +137,6 @@ class CORE_EXPORT QgsProcessingUtils
static bool canUseLayer( const QgsVectorLayer *layer,
const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >() );

/**
* 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 \a string as a map layer from a store.
*
@@ -1067,6 +1067,16 @@ const QgsLabelingEngineSettings &QgsProject::labelingEngineSettings() const
return *mLabelingEngineSettings;
}

QgsMapLayerStore *QgsProject::layerStore()
{
return mLayerStore.get();
}

const QgsMapLayerStore *QgsProject::layerStore() const
{
return mLayerStore.get();
}

QList<QgsVectorLayer *> QgsProject::avoidIntersectionsLayers() const
{
QList<QgsVectorLayer *> layers;
@@ -529,6 +529,18 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
// Functionality from QgsMapLayerRegistry
//

/**
* Returns a pointer to the project's internal layer store.
* /since QGIS 3.0
*/
QgsMapLayerStore *layerStore();

/**
* Returns a pointer to the project's internal layer store.
* /since QGIS 3.0
*/
SIP_SKIP const QgsMapLayerStore *layerStore() const;

//! Returns the number of registered layers.
int count() const;

@@ -336,42 +336,22 @@ void TestQgsProcessing::normalizeLayerSource()

void TestQgsProcessing::mapLayers()
{
// test mapLayerFromProject

QgsProject p;

// 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" );
p.addMapLayers( QList<QgsMapLayer *>() << r1 << r2 << v1 << v2 );
QString raster = testDataDir + "landsat.tif";
QString vector = testDataDir + "points.shp";

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

// test loadMapLayerFromString
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster2 );
// test loadMapLayerFromString with raster
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster );
QVERIFY( l->isValid() );
QCOMPARE( l->type(), QgsMapLayer::RasterLayer );
delete l;

//test with vector
l = QgsProcessingUtils::loadMapLayerFromString( vector );
QVERIFY( l->isValid() );
QCOMPARE( l->type(), QgsMapLayer::VectorLayer );
delete l;

l = QgsProcessingUtils::loadMapLayerFromString( QString() );
QVERIFY( !l );
l = QgsProcessingUtils::loadMapLayerFromString( QStringLiteral( "so much room for activities!" ) );

0 comments on commit d2ce9c6

Please sign in to comment.