Add method to whitelist layers to show in QgsMapLayerProxyModel

This commit is contained in:
Nyall Dawson 2018-10-23 10:44:47 +10:00
parent f32fe8b210
commit de0e74be59
4 changed files with 194 additions and 18 deletions

View File

@ -51,35 +51,92 @@ layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel
QgsMapLayerProxyModel *setFilters( QgsMapLayerProxyModel::Filters filters );
%Docstring
setFilters set flags that affect how layers are filtered
Sets ``filter`` flags which affect how layers are filtered within the model.
:param filters: are Filter flags
.. seealso:: :py:func:`filters`
.. versionadded:: 2.3
%End
const Filters &filters() const;
%Docstring
Returns the filter flags which affect how layers are filtered within the model.
.. seealso:: :py:func:`setFilters`
.. versionadded:: 2.3
%End
void setLayerWhitelist( const QList<QgsMapLayer *> &layers );
%Docstring
Sets a whitelist of ``layers`` to include within the model. Only layers
from this list will be shown.
An empty list indicates that no whitelisting should be performed.
.. seealso:: :py:func:`layerWhitelist`
.. seealso:: :py:func:`setExceptedLayerList`
.. versionadded:: 3.4
%End
QList<QgsMapLayer *> layerWhitelist();
%Docstring
Returns the list of layers which are excluded from the model.
An empty list indicates that no whitelisting should be performed.
.. seealso:: :py:func:`setLayerWhitelist`
.. seealso:: :py:func:`exceptedLayerList`
.. versionadded:: 3.4
%End
void setExceptedLayerList( const QList<QgsMapLayer *> &exceptList );
%Docstring
offer the possibility to except some layers to be listed
Sets a blacklist of layers to exclude from the model.
.. seealso:: :py:func:`exceptedLayerList`
.. seealso:: :py:func:`setExceptedLayerIds`
.. seealso:: :py:func:`setLayerWhitelist`
%End
QList<QgsMapLayer *> exceptedLayerList();
%Docstring
Gets the list of maplayers which are excluded from the list
Returns the blacklist of layers which are excluded from the model.
.. seealso:: :py:func:`setExceptedLayerList`
.. seealso:: :py:func:`exceptedLayerIds`
.. seealso:: :py:func:`layerWhitelist`
%End
void setExceptedLayerIds( const QStringList &ids );
%Docstring
Sets the list of maplayer ids which are excluded from the list
Sets a blacklist of layers (by layer ID) to exclude from the model.
.. seealso:: :py:func:`exceptedLayerIds`
.. seealso:: :py:func:`setExceptedLayerList`
%End
QStringList exceptedLayerIds() const;
%Docstring
Gets the list of maplayer ids which are excluded from the list
Returns the blacklist of layer IDs which are excluded from the model.
.. seealso:: :py:func:`setExceptedLayerIds`
.. seealso:: :py:func:`exceptedLayerList`
%End
void setExcludedProviders( const QStringList &providers );
%Docstring
Sets a list of data providers which should be excluded from the model.
Sets a blacklist of data providers which should be excluded from the model.
.. seealso:: :py:func:`excludedProviders`
@ -88,7 +145,7 @@ Sets a list of data providers which should be excluded from the model.
QStringList excludedProviders() const;
%Docstring
Returns the list of data providers which are excluded from the model.
Returns the blacklist of data providers which are excluded from the model.
.. seealso:: :py:func:`setExcludedProviders`

View File

@ -25,7 +25,6 @@
QgsMapLayerProxyModel::QgsMapLayerProxyModel( QObject *parent )
: QSortFilterProxyModel( parent )
, mFilters( All )
, mExceptList( QList<QgsMapLayer*>() )
, mModel( new QgsMapLayerModel( parent ) )
{
setSourceModel( mModel );
@ -42,6 +41,15 @@ QgsMapLayerProxyModel *QgsMapLayerProxyModel::setFilters( Filters filters )
return this;
}
void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
{
if ( mLayerWhitelist == layers )
return;
mLayerWhitelist = layers;
invalidateFilter();
}
void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
{
if ( mExceptList == exceptList )
@ -88,7 +96,7 @@ void QgsMapLayerProxyModel::setFilterString( const QString &filter )
bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
{
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mLayerWhitelist.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
return true;
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
@ -101,6 +109,9 @@ bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex
if ( !layer )
return false;
if ( !mLayerWhitelist.isEmpty() && !mLayerWhitelist.contains( layer ) )
return false;
if ( mExceptList.contains( layer ) )
return false;

View File

@ -67,32 +67,87 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
QgsMapLayerModel *sourceLayerModel() const { return mModel; }
/**
* \brief setFilters set flags that affect how layers are filtered
* \param filters are Filter flags
* Sets \a filter flags which affect how layers are filtered within the model.
*
* \see filters()
*
* \since QGIS 2.3
*/
QgsMapLayerProxyModel *setFilters( QgsMapLayerProxyModel::Filters filters );
/**
* Returns the filter flags which affect how layers are filtered within the model.
*
* \see setFilters()
*
* \since QGIS 2.3
*/
const Filters &filters() const { return mFilters; }
//! offer the possibility to except some layers to be listed
/**
* Sets a whitelist of \a layers to include within the model. Only layers
* from this list will be shown.
*
* An empty list indicates that no whitelisting should be performed.
*
* \see layerWhitelist()
* \see setExceptedLayerList()
*
* \since QGIS 3.4
*/
void setLayerWhitelist( const QList<QgsMapLayer *> &layers );
/**
* Returns the list of layers which are excluded from the model.
*
* An empty list indicates that no whitelisting should be performed.
*
* \see setLayerWhitelist()
* \see exceptedLayerList()
*
* \since QGIS 3.4
*/
QList<QgsMapLayer *> layerWhitelist() {return mLayerWhitelist;}
/**
* Sets a blacklist of layers to exclude from the model.
* \see exceptedLayerList()
* \see setExceptedLayerIds()
* \see setLayerWhitelist()
*/
void setExceptedLayerList( const QList<QgsMapLayer *> &exceptList );
//! Gets the list of maplayers which are excluded from the list
/**
* Returns the blacklist of layers which are excluded from the model.
* \see setExceptedLayerList()
* \see exceptedLayerIds()
* \see layerWhitelist()
*/
QList<QgsMapLayer *> exceptedLayerList() {return mExceptList;}
//! Sets the list of maplayer ids which are excluded from the list
/**
* Sets a blacklist of layers (by layer ID) to exclude from the model.
* \see exceptedLayerIds()
* \see setExceptedLayerList()
*/
void setExceptedLayerIds( const QStringList &ids );
//! Gets the list of maplayer ids which are excluded from the list
/**
* Returns the blacklist of layer IDs which are excluded from the model.
* \see setExceptedLayerIds()
* \see exceptedLayerList()
*/
QStringList exceptedLayerIds() const;
/**
* Sets a list of data providers which should be excluded from the model.
* Sets a blacklist of data providers which should be excluded from the model.
* \see excludedProviders()
* \since QGIS 3.0
*/
void setExcludedProviders( const QStringList &providers );
/**
* Returns the list of data providers which are excluded from the model.
* Returns the blacklist of data providers which are excluded from the model.
* \see setExcludedProviders()
* \since QGIS 3.0
*/
@ -123,6 +178,7 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
private:
Filters mFilters;
QList<QgsMapLayer *> mExceptList;
QList<QgsMapLayer *> mLayerWhitelist;
QgsMapLayerModel *mModel = nullptr;
QStringList mExcludedProviders;
QString mFilterString;

View File

@ -47,6 +47,9 @@ class TestQgsMapLayerProxyModel(unittest.TestCase):
m.setExceptedLayerList([l2])
self.assertEqual(m.exceptedLayerList(), [l2])
m.setLayerWhitelist([l2])
self.assertEqual(m.layerWhitelist(), [l2])
m.setExcludedProviders(['a', 'b'])
self.assertEqual(m.excludedProviders(), ['a', 'b'])
@ -131,6 +134,55 @@ class TestQgsMapLayerProxyModel(unittest.TestCase):
m.setFilterString('')
self.assertEqual(m.rowCount(), 4)
def testFilterByLayer(self):
""" test filtering by layer"""
QgsProject.instance().clear()
m = QgsMapLayerProxyModel()
l1 = QgsVectorLayer("Point?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
'layer 1', "memory")
QgsProject.instance().addMapLayer(l1)
l2 = QgsVectorLayer("Polygon?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
'lAyEr 2', "memory")
QgsProject.instance().addMapLayer(l2)
l3 = QgsVectorLayer("None?field=fldtxt:string&field=fldint:integer",
'another', "memory")
QgsProject.instance().addMapLayer(l3)
l4 = QgsVectorLayer("LineString?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
'final layer', "memory")
QgsProject.instance().addMapLayer(l4)
self.assertEqual(m.rowCount(), 4)
self.assertEqual(m.data(m.index(0, 0)), 'another')
self.assertEqual(m.data(m.index(1, 0)), 'final layer')
self.assertEqual(m.data(m.index(2, 0)), 'layer 1')
self.assertEqual(m.data(m.index(3, 0)), 'lAyEr 2')
m.setExceptedLayerList([l1, l3])
self.assertEqual(m.rowCount(), 2)
self.assertEqual(m.data(m.index(0, 0)), 'final layer')
self.assertEqual(m.data(m.index(1, 0)), 'lAyEr 2')
m.setExceptedLayerIds([l2.id(), l4.id()])
self.assertEqual(m.rowCount(), 2)
self.assertEqual(m.data(m.index(0, 0)), 'another')
self.assertEqual(m.data(m.index(1, 0)), 'layer 1')
m.setLayerWhitelist([l1])
self.assertEqual(m.rowCount(), 1)
self.assertEqual(m.data(m.index(0, 0)), 'layer 1')
m.setExceptedLayerIds([])
self.assertEqual(m.rowCount(), 1)
self.assertEqual(m.data(m.index(0, 0)), 'layer 1')
m.setLayerWhitelist([l2, l3])
self.assertEqual(m.rowCount(), 2)
self.assertEqual(m.data(m.index(0, 0)), 'another')
self.assertEqual(m.data(m.index(1, 0)), 'lAyEr 2')
m.setLayerWhitelist([])
self.assertEqual(m.rowCount(), 4)
if __name__ == '__main__':
unittest.main()