Skip to content

Commit de0e74b

Browse files
committed
Add method to whitelist layers to show in QgsMapLayerProxyModel
1 parent f32fe8b commit de0e74b

File tree

4 files changed

+194
-18
lines changed

4 files changed

+194
-18
lines changed

python/core/auto_generated/qgsmaplayerproxymodel.sip.in

+65-8
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,92 @@ layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel
5151

5252
QgsMapLayerProxyModel *setFilters( QgsMapLayerProxyModel::Filters filters );
5353
%Docstring
54-
setFilters set flags that affect how layers are filtered
54+
Sets ``filter`` flags which affect how layers are filtered within the model.
5555

56-
:param filters: are Filter flags
56+
.. seealso:: :py:func:`filters`
5757

5858
.. versionadded:: 2.3
5959
%End
60+
6061
const Filters &filters() const;
62+
%Docstring
63+
Returns the filter flags which affect how layers are filtered within the model.
64+
65+
.. seealso:: :py:func:`setFilters`
66+
67+
.. versionadded:: 2.3
68+
%End
69+
70+
void setLayerWhitelist( const QList<QgsMapLayer *> &layers );
71+
%Docstring
72+
Sets a whitelist of ``layers`` to include within the model. Only layers
73+
from this list will be shown.
74+
75+
An empty list indicates that no whitelisting should be performed.
76+
77+
.. seealso:: :py:func:`layerWhitelist`
78+
79+
.. seealso:: :py:func:`setExceptedLayerList`
80+
81+
.. versionadded:: 3.4
82+
%End
83+
84+
QList<QgsMapLayer *> layerWhitelist();
85+
%Docstring
86+
Returns the list of layers which are excluded from the model.
87+
88+
An empty list indicates that no whitelisting should be performed.
89+
90+
.. seealso:: :py:func:`setLayerWhitelist`
91+
92+
.. seealso:: :py:func:`exceptedLayerList`
93+
94+
.. versionadded:: 3.4
95+
%End
6196

6297
void setExceptedLayerList( const QList<QgsMapLayer *> &exceptList );
6398
%Docstring
64-
offer the possibility to except some layers to be listed
99+
Sets a blacklist of layers to exclude from the model.
100+
101+
.. seealso:: :py:func:`exceptedLayerList`
102+
103+
.. seealso:: :py:func:`setExceptedLayerIds`
104+
105+
.. seealso:: :py:func:`setLayerWhitelist`
65106
%End
107+
66108
QList<QgsMapLayer *> exceptedLayerList();
67109
%Docstring
68-
Gets the list of maplayers which are excluded from the list
110+
Returns the blacklist of layers which are excluded from the model.
111+
112+
.. seealso:: :py:func:`setExceptedLayerList`
113+
114+
.. seealso:: :py:func:`exceptedLayerIds`
115+
116+
.. seealso:: :py:func:`layerWhitelist`
69117
%End
70118

71119
void setExceptedLayerIds( const QStringList &ids );
72120
%Docstring
73-
Sets the list of maplayer ids which are excluded from the list
121+
Sets a blacklist of layers (by layer ID) to exclude from the model.
122+
123+
.. seealso:: :py:func:`exceptedLayerIds`
124+
125+
.. seealso:: :py:func:`setExceptedLayerList`
74126
%End
127+
75128
QStringList exceptedLayerIds() const;
76129
%Docstring
77-
Gets the list of maplayer ids which are excluded from the list
130+
Returns the blacklist of layer IDs which are excluded from the model.
131+
132+
.. seealso:: :py:func:`setExceptedLayerIds`
133+
134+
.. seealso:: :py:func:`exceptedLayerList`
78135
%End
79136

80137
void setExcludedProviders( const QStringList &providers );
81138
%Docstring
82-
Sets a list of data providers which should be excluded from the model.
139+
Sets a blacklist of data providers which should be excluded from the model.
83140

84141
.. seealso:: :py:func:`excludedProviders`
85142

@@ -88,7 +145,7 @@ Sets a list of data providers which should be excluded from the model.
88145

89146
QStringList excludedProviders() const;
90147
%Docstring
91-
Returns the list of data providers which are excluded from the model.
148+
Returns the blacklist of data providers which are excluded from the model.
92149

93150
.. seealso:: :py:func:`setExcludedProviders`
94151

src/core/qgsmaplayerproxymodel.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
QgsMapLayerProxyModel::QgsMapLayerProxyModel( QObject *parent )
2626
: QSortFilterProxyModel( parent )
2727
, mFilters( All )
28-
, mExceptList( QList<QgsMapLayer*>() )
2928
, mModel( new QgsMapLayerModel( parent ) )
3029
{
3130
setSourceModel( mModel );
@@ -42,6 +41,15 @@ QgsMapLayerProxyModel *QgsMapLayerProxyModel::setFilters( Filters filters )
4241
return this;
4342
}
4443

44+
void QgsMapLayerProxyModel::setLayerWhitelist( const QList<QgsMapLayer *> &layers )
45+
{
46+
if ( mLayerWhitelist == layers )
47+
return;
48+
49+
mLayerWhitelist = layers;
50+
invalidateFilter();
51+
}
52+
4553
void QgsMapLayerProxyModel::setExceptedLayerList( const QList<QgsMapLayer *> &exceptList )
4654
{
4755
if ( mExceptList == exceptList )
@@ -88,7 +96,7 @@ void QgsMapLayerProxyModel::setFilterString( const QString &filter )
8896

8997
bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
9098
{
91-
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
99+
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mLayerWhitelist.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
92100
return true;
93101

94102
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
@@ -101,6 +109,9 @@ bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex
101109
if ( !layer )
102110
return false;
103111

112+
if ( !mLayerWhitelist.isEmpty() && !mLayerWhitelist.contains( layer ) )
113+
return false;
114+
104115
if ( mExceptList.contains( layer ) )
105116
return false;
106117

src/core/qgsmaplayerproxymodel.h

+64-8
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,87 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
6767
QgsMapLayerModel *sourceLayerModel() const { return mModel; }
6868

6969
/**
70-
* \brief setFilters set flags that affect how layers are filtered
71-
* \param filters are Filter flags
70+
* Sets \a filter flags which affect how layers are filtered within the model.
71+
*
72+
* \see filters()
73+
*
7274
* \since QGIS 2.3
7375
*/
7476
QgsMapLayerProxyModel *setFilters( QgsMapLayerProxyModel::Filters filters );
77+
78+
/**
79+
* Returns the filter flags which affect how layers are filtered within the model.
80+
*
81+
* \see setFilters()
82+
*
83+
* \since QGIS 2.3
84+
*/
7585
const Filters &filters() const { return mFilters; }
7686

77-
//! offer the possibility to except some layers to be listed
87+
/**
88+
* Sets a whitelist of \a layers to include within the model. Only layers
89+
* from this list will be shown.
90+
*
91+
* An empty list indicates that no whitelisting should be performed.
92+
*
93+
* \see layerWhitelist()
94+
* \see setExceptedLayerList()
95+
*
96+
* \since QGIS 3.4
97+
*/
98+
void setLayerWhitelist( const QList<QgsMapLayer *> &layers );
99+
100+
/**
101+
* Returns the list of layers which are excluded from the model.
102+
*
103+
* An empty list indicates that no whitelisting should be performed.
104+
*
105+
* \see setLayerWhitelist()
106+
* \see exceptedLayerList()
107+
*
108+
* \since QGIS 3.4
109+
*/
110+
QList<QgsMapLayer *> layerWhitelist() {return mLayerWhitelist;}
111+
112+
/**
113+
* Sets a blacklist of layers to exclude from the model.
114+
* \see exceptedLayerList()
115+
* \see setExceptedLayerIds()
116+
* \see setLayerWhitelist()
117+
*/
78118
void setExceptedLayerList( const QList<QgsMapLayer *> &exceptList );
79-
//! Gets the list of maplayers which are excluded from the list
119+
120+
/**
121+
* Returns the blacklist of layers which are excluded from the model.
122+
* \see setExceptedLayerList()
123+
* \see exceptedLayerIds()
124+
* \see layerWhitelist()
125+
*/
80126
QList<QgsMapLayer *> exceptedLayerList() {return mExceptList;}
81127

82-
//! Sets the list of maplayer ids which are excluded from the list
128+
/**
129+
* Sets a blacklist of layers (by layer ID) to exclude from the model.
130+
* \see exceptedLayerIds()
131+
* \see setExceptedLayerList()
132+
*/
83133
void setExceptedLayerIds( const QStringList &ids );
84-
//! Gets the list of maplayer ids which are excluded from the list
134+
135+
/**
136+
* Returns the blacklist of layer IDs which are excluded from the model.
137+
* \see setExceptedLayerIds()
138+
* \see exceptedLayerList()
139+
*/
85140
QStringList exceptedLayerIds() const;
86141

87142
/**
88-
* Sets a list of data providers which should be excluded from the model.
143+
* Sets a blacklist of data providers which should be excluded from the model.
89144
* \see excludedProviders()
90145
* \since QGIS 3.0
91146
*/
92147
void setExcludedProviders( const QStringList &providers );
93148

94149
/**
95-
* Returns the list of data providers which are excluded from the model.
150+
* Returns the blacklist of data providers which are excluded from the model.
96151
* \see setExcludedProviders()
97152
* \since QGIS 3.0
98153
*/
@@ -123,6 +178,7 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
123178
private:
124179
Filters mFilters;
125180
QList<QgsMapLayer *> mExceptList;
181+
QList<QgsMapLayer *> mLayerWhitelist;
126182
QgsMapLayerModel *mModel = nullptr;
127183
QStringList mExcludedProviders;
128184
QString mFilterString;

tests/src/python/test_qgsmaplayerproxymodel.py

+52
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def testGettersSetters(self):
4747
m.setExceptedLayerList([l2])
4848
self.assertEqual(m.exceptedLayerList(), [l2])
4949

50+
m.setLayerWhitelist([l2])
51+
self.assertEqual(m.layerWhitelist(), [l2])
52+
5053
m.setExcludedProviders(['a', 'b'])
5154
self.assertEqual(m.excludedProviders(), ['a', 'b'])
5255

@@ -131,6 +134,55 @@ def testFilterString(self):
131134
m.setFilterString('')
132135
self.assertEqual(m.rowCount(), 4)
133136

137+
def testFilterByLayer(self):
138+
""" test filtering by layer"""
139+
QgsProject.instance().clear()
140+
m = QgsMapLayerProxyModel()
141+
l1 = QgsVectorLayer("Point?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
142+
'layer 1', "memory")
143+
QgsProject.instance().addMapLayer(l1)
144+
l2 = QgsVectorLayer("Polygon?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
145+
'lAyEr 2', "memory")
146+
QgsProject.instance().addMapLayer(l2)
147+
l3 = QgsVectorLayer("None?field=fldtxt:string&field=fldint:integer",
148+
'another', "memory")
149+
QgsProject.instance().addMapLayer(l3)
150+
l4 = QgsVectorLayer("LineString?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
151+
'final layer', "memory")
152+
QgsProject.instance().addMapLayer(l4)
153+
154+
self.assertEqual(m.rowCount(), 4)
155+
self.assertEqual(m.data(m.index(0, 0)), 'another')
156+
self.assertEqual(m.data(m.index(1, 0)), 'final layer')
157+
self.assertEqual(m.data(m.index(2, 0)), 'layer 1')
158+
self.assertEqual(m.data(m.index(3, 0)), 'lAyEr 2')
159+
160+
m.setExceptedLayerList([l1, l3])
161+
self.assertEqual(m.rowCount(), 2)
162+
self.assertEqual(m.data(m.index(0, 0)), 'final layer')
163+
self.assertEqual(m.data(m.index(1, 0)), 'lAyEr 2')
164+
165+
m.setExceptedLayerIds([l2.id(), l4.id()])
166+
self.assertEqual(m.rowCount(), 2)
167+
self.assertEqual(m.data(m.index(0, 0)), 'another')
168+
self.assertEqual(m.data(m.index(1, 0)), 'layer 1')
169+
170+
m.setLayerWhitelist([l1])
171+
self.assertEqual(m.rowCount(), 1)
172+
self.assertEqual(m.data(m.index(0, 0)), 'layer 1')
173+
174+
m.setExceptedLayerIds([])
175+
self.assertEqual(m.rowCount(), 1)
176+
self.assertEqual(m.data(m.index(0, 0)), 'layer 1')
177+
178+
m.setLayerWhitelist([l2, l3])
179+
self.assertEqual(m.rowCount(), 2)
180+
self.assertEqual(m.data(m.index(0, 0)), 'another')
181+
self.assertEqual(m.data(m.index(1, 0)), 'lAyEr 2')
182+
183+
m.setLayerWhitelist([])
184+
self.assertEqual(m.rowCount(), 4)
185+
134186

135187
if __name__ == '__main__':
136188
unittest.main()

0 commit comments

Comments
 (0)