Skip to content
Permalink
Browse files

add static method to test if layer matches QgsMapLayerProxyModel::Fil…

…ters
  • Loading branch information
3nids committed Jun 6, 2020
1 parent 91f71f5 commit d311aef8c7c23942e42bd1537521b3bbe768ceb7
@@ -67,6 +67,13 @@ Returns the filter flags which affect how layers are filtered within the model.
.. seealso:: :py:func:`setFilters`

.. versionadded:: 2.3
%End

static bool layerMatchesFilters( const QgsMapLayer *layer, const Filters &filters );
%Docstring
Returns if the ``layer`` matches the given ``filters``

.. versionadded:: 3.14
%End

void setLayerWhitelist( const QList<QgsMapLayer *> &layers );
@@ -43,6 +43,42 @@ QgsMapLayerProxyModel *QgsMapLayerProxyModel::setFilters( Filters filters )
return this;
}

bool QgsMapLayerProxyModel::layerMatchesFilters( const QgsMapLayer *layer, const Filters &filters )
{
// layer type
if ( ( filters.testFlag( RasterLayer ) && layer->type() == QgsMapLayerType::RasterLayer ) ||
( filters.testFlag( VectorLayer ) && layer->type() == QgsMapLayerType::VectorLayer ) ||
( filters.testFlag( MeshLayer ) && layer->type() == QgsMapLayerType::MeshLayer ) ||
( filters.testFlag( VectorTileLayer ) && layer->type() == QgsMapLayerType::VectorTileLayer ) ||
( filters.testFlag( PluginLayer ) && layer->type() == QgsMapLayerType::PluginLayer ) )
return true;

// geometry type
bool detectGeometry = filters.testFlag( NoGeometry ) ||
filters.testFlag( PointLayer ) ||
filters.testFlag( LineLayer ) ||
filters.testFlag( PolygonLayer ) ||
filters.testFlag( HasGeometry );
if ( detectGeometry && layer->type() == QgsMapLayerType::VectorLayer )
{
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( layer ) )
{
if ( filters.testFlag( HasGeometry ) && vl->isSpatial() )
return true;
if ( filters.testFlag( NoGeometry ) && vl->geometryType() == QgsWkbTypes::NullGeometry )
return true;
if ( filters.testFlag( PointLayer ) && vl->geometryType() == QgsWkbTypes::PointGeometry )
return true;
if ( filters.testFlag( LineLayer ) && vl->geometryType() == QgsWkbTypes::LineGeometry )
return true;
if ( filters.testFlag( PolygonLayer ) && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
return true;
}
}

return false;
}

void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
{
if ( mLayerWhitelist == layers )
@@ -112,38 +148,7 @@ bool QgsMapLayerProxyModel::acceptsLayer( QgsMapLayer *layer ) const
if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
return false;

// layer type
if ( ( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayerType::RasterLayer ) ||
( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayerType::VectorLayer ) ||
( mFilters.testFlag( MeshLayer ) && layer->type() == QgsMapLayerType::MeshLayer ) ||
( mFilters.testFlag( VectorTileLayer ) && layer->type() == QgsMapLayerType::VectorTileLayer ) ||
( mFilters.testFlag( PluginLayer ) && layer->type() == QgsMapLayerType::PluginLayer ) )
return true;

// geometry type
bool detectGeometry = mFilters.testFlag( NoGeometry ) ||
mFilters.testFlag( PointLayer ) ||
mFilters.testFlag( LineLayer ) ||
mFilters.testFlag( PolygonLayer ) ||
mFilters.testFlag( HasGeometry );
if ( detectGeometry && layer->type() == QgsMapLayerType::VectorLayer )
{
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( layer ) )
{
if ( mFilters.testFlag( HasGeometry ) && vl->isSpatial() )
return true;
if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QgsWkbTypes::NullGeometry )
return true;
if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QgsWkbTypes::PointGeometry )
return true;
if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QgsWkbTypes::LineGeometry )
return true;
if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
return true;
}
}

return false;
return layerMatchesFilters( layer, mFilters );
}

void QgsMapLayerProxyModel::setFilterString( const QString &filter )
@@ -86,6 +86,12 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
*/
const Filters &filters() const { return mFilters; }

/**
* Returns if the \a layer matches the given \a filters
* \since QGIS 3.14
*/
static bool layerMatchesFilters( const QgsMapLayer *layer, const Filters &filters );

/**
* Sets a whitelist of \a layers to include within the model. Only layers
* from this list will be shown.

0 comments on commit d311aef

Please sign in to comment.
You can’t perform that action at this time.