Skip to content

Commit

Permalink
condense compatibleRasterLayers/compatibleMeshLayers/compatiblePlugin…
Browse files Browse the repository at this point in the history
…Layers into one unified function
  • Loading branch information
alexbruy authored and nyalldawson committed Jul 29, 2021
1 parent a94ad63 commit 473ac47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 deletions.
65 changes: 18 additions & 47 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -40,26 +40,7 @@

QList<QgsRasterLayer *> QgsProcessingUtils::compatibleRasterLayers( QgsProject *project, bool sort )
{
if ( !project )
return QList<QgsRasterLayer *>();

QList<QgsRasterLayer *> layers;

const auto rasterLayers = project->layers<QgsRasterLayer *>();
for ( QgsRasterLayer *l : rasterLayers )
{
if ( canUseLayer( l ) )
layers << l;
}

if ( sort )
{
std::sort( layers.begin(), layers.end(), []( const QgsRasterLayer * a, const QgsRasterLayer * b ) -> bool
{
return QString::localeAwareCompare( a->name(), b->name() ) < 0;
} );
}
return layers;
return compatibleMapLayers< QgsRasterLayer >( project, sort );
}

QList<QgsVectorLayer *> QgsProcessingUtils::compatibleVectorLayers( QgsProject *project, const QList<int> &geometryTypes, bool sort )
Expand Down Expand Up @@ -87,43 +68,30 @@ QList<QgsVectorLayer *> QgsProcessingUtils::compatibleVectorLayers( QgsProject *

QList<QgsMeshLayer *> QgsProcessingUtils::compatibleMeshLayers( QgsProject *project, bool sort )
{
if ( !project )
return QList<QgsMeshLayer *>();

QList<QgsMeshLayer *> layers;
const auto meshLayers = project->layers<QgsMeshLayer *>();
for ( QgsMeshLayer *l : meshLayers )
{
if ( canUseLayer( l ) )
layers << l;
}

if ( sort )
{
std::sort( layers.begin(), layers.end(), []( const QgsMeshLayer * a, const QgsMeshLayer * b ) -> bool
{
return QString::localeAwareCompare( a->name(), b->name() ) < 0;
} );
}
return layers;
return compatibleMapLayers< QgsMeshLayer >( project, sort );
}

QList<QgsPluginLayer *> QgsProcessingUtils::compatiblePluginLayers( QgsProject *project, bool sort )
{
return compatibleMapLayers< QgsPluginLayer >( project, sort );
}

template<typename T> QList<T *> QgsProcessingUtils::compatibleMapLayers( QgsProject *project, bool sort )
{
if ( !project )
return QList<QgsPluginLayer *>();
return QList<T *>();

QList<QgsPluginLayer *> layers;
const auto pluginLayers = project->layers<QgsPluginLayer *>();
for ( QgsPluginLayer *l : pluginLayers )
QList<T *> layers;
const auto projectLayers = project->layers<T *>();
for ( T *l : projectLayers )
{
if ( canUseLayer( l ) )
layers << l;
}

if ( sort )
{
std::sort( layers.begin(), layers.end(), []( const QgsPluginLayer * a, const QgsPluginLayer * b ) -> bool
std::sort( layers.begin(), layers.end(), []( const T * a, const T * b ) -> bool
{
return QString::localeAwareCompare( a->name(), b->name() ) < 0;
} );
Expand All @@ -138,19 +106,22 @@ QList<QgsMapLayer *> QgsProcessingUtils::compatibleLayers( QgsProject *project,

QList<QgsMapLayer *> layers;

const auto rasterLayers = compatibleRasterLayers( project, false );
//~ const auto rasterLayers = compatibleRasterLayers( project, false );
const auto rasterLayers = compatibleMapLayers< QgsRasterLayer >( project, false );
for ( QgsRasterLayer *rl : rasterLayers )
layers << rl;

const auto vectorLayers = compatibleVectorLayers( project, QList< int >(), false );
for ( QgsVectorLayer *vl : vectorLayers )
layers << vl;

const auto meshLayers = compatibleMeshLayers( project, false );
//~ const auto meshLayers = compatibleMeshLayers( project, false );
const auto meshLayers = compatibleMapLayers< QgsMeshLayer >( project, false );
for ( QgsMeshLayer *vl : meshLayers )
layers << vl;

const auto pluginLayers = compatiblePluginLayers( project, false );
//~ const auto pluginLayers = compatiblePluginLayers( project, false );
const auto pluginLayers = compatibleMapLayers< QgsPluginLayer >( project, false );
for ( QgsPluginLayer *pl : pluginLayers )
layers << pl;

Expand Down
14 changes: 14 additions & 0 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -440,6 +440,20 @@ class CORE_EXPORT QgsProcessingUtils
static bool canUseLayer( const QgsVectorLayer *layer,
const QList< int > &sourceTypes = QList< int >() );

/**
* Returns a list of map layers with the given layer type from a \a project which are compatible
* with the processing framework.
*
* If the \a sort argument is TRUE then the layers will be sorted by their QgsMapLayer::name()
* value.
* \see compatibleRasterLayers()
* \see compatibleVectorLayers()
* \see compatibleMeshLayers()
* \see compatiblePluginLayers()
* \since QGIS 3.22
*/
template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );

/**
* Interprets a \a string as a map layer from a store.
*
Expand Down

0 comments on commit 473ac47

Please sign in to comment.