Skip to content

Commit 51a63b9

Browse files
committed
Allow QgsMapLayerProxyModel to be filtered by string
1 parent 5582a57 commit 51a63b9

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

python/core/auto_generated/qgsmaplayerproxymodel.sip.in

+21
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,34 @@ Returns the list of data providers which are excluded from the model.
9393
.. seealso:: :py:func:`setExcludedProviders`
9494

9595
.. versionadded:: 3.0
96+
%End
97+
98+
QString filterString() const;
99+
%Docstring
100+
Returns the current filter string, if set.
101+
102+
.. seealso:: :py:func:`setFilterString`
103+
104+
.. versionadded:: 3.4
96105
%End
97106

98107
virtual bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;
99108

100109
virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
101110

102111

112+
public slots:
113+
114+
void setFilterString( const QString &filter );
115+
%Docstring
116+
Sets a ``filter`` string, such that only layers with names matching the
117+
specified string will be shown.
118+
119+
.. seealso:: :py:func:`filterString`
120+
121+
.. versionadded:: 3.4
122+
%End
123+
103124
};
104125

105126
QFlags<QgsMapLayerProxyModel::Filter> operator|(QgsMapLayerProxyModel::Filter f1, QFlags<QgsMapLayerProxyModel::Filter> f2);

src/core/qgsmaplayerproxymodel.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
8080
invalidateFilter();
8181
}
8282

83+
void QgsMapLayerProxyModel::setFilterString( const QString &filter )
84+
{
85+
mFilterString = filter;
86+
invalidateFilter();
87+
}
88+
8389
bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
8490
{
85-
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() )
91+
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
8692
return true;
8793

8894
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
@@ -104,6 +110,9 @@ bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex
104110
if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
105111
return false;
106112

113+
if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
114+
return false;
115+
107116
// layer type
108117
if ( ( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
109118
( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||

src/core/qgsmaplayerproxymodel.h

+20
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,34 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
9898
*/
9999
QStringList excludedProviders() const { return mExcludedProviders; }
100100

101+
/**
102+
* Returns the current filter string, if set.
103+
*
104+
* \see setFilterString()
105+
* \since QGIS 3.4
106+
*/
107+
QString filterString() const { return mFilterString; }
108+
101109
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
102110
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
103111

112+
public slots:
113+
114+
/**
115+
* Sets a \a filter string, such that only layers with names matching the
116+
* specified string will be shown.
117+
*
118+
* \see filterString()
119+
* \since QGIS 3.4
120+
*/
121+
void setFilterString( const QString &filter );
122+
104123
private:
105124
Filters mFilters;
106125
QList<QgsMapLayer *> mExceptList;
107126
QgsMapLayerModel *mModel = nullptr;
108127
QStringList mExcludedProviders;
128+
QString mFilterString;
109129
};
110130

111131
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerProxyModel::Filters )

0 commit comments

Comments
 (0)