Allow QgsMapLayerProxyModel to be filtered by string

This commit is contained in:
Nyall Dawson 2018-08-22 14:03:12 +10:00
parent 5582a57918
commit 51a63b98fb
3 changed files with 51 additions and 1 deletions

View File

@ -93,6 +93,15 @@ Returns the list of data providers which are excluded from the model.
.. seealso:: :py:func:`setExcludedProviders`
.. versionadded:: 3.0
%End
QString filterString() const;
%Docstring
Returns the current filter string, if set.
.. seealso:: :py:func:`setFilterString`
.. versionadded:: 3.4
%End
virtual bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;
@ -100,6 +109,18 @@ Returns the list of data providers which are excluded from the model.
virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
public slots:
void setFilterString( const QString &filter );
%Docstring
Sets a ``filter`` string, such that only layers with names matching the
specified string will be shown.
.. seealso:: :py:func:`filterString`
.. versionadded:: 3.4
%End
};
QFlags<QgsMapLayerProxyModel::Filter> operator|(QgsMapLayerProxyModel::Filter f1, QFlags<QgsMapLayerProxyModel::Filter> f2);

View File

@ -80,9 +80,15 @@ void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
invalidateFilter();
}
void QgsMapLayerProxyModel::setFilterString( const QString &filter )
{
mFilterString = filter;
invalidateFilter();
}
bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
{
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() )
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
return true;
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
@ -104,6 +110,9 @@ bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex
if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
return false;
if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
return false;
// layer type
if ( ( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||

View File

@ -98,14 +98,34 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
*/
QStringList excludedProviders() const { return mExcludedProviders; }
/**
* Returns the current filter string, if set.
*
* \see setFilterString()
* \since QGIS 3.4
*/
QString filterString() const { return mFilterString; }
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
public slots:
/**
* Sets a \a filter string, such that only layers with names matching the
* specified string will be shown.
*
* \see filterString()
* \since QGIS 3.4
*/
void setFilterString( const QString &filter );
private:
Filters mFilters;
QList<QgsMapLayer *> mExceptList;
QgsMapLayerModel *mModel = nullptr;
QStringList mExcludedProviders;
QString mFilterString;
};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerProxyModel::Filters )