Skip to content
Permalink
Browse files

Allow QgsMapLayerProxyModel to be filtered by string

  • Loading branch information
nyalldawson committed Aug 22, 2018
1 parent 5582a57 commit 51a63b98fbcd7e9db609a40fa2b0c4381ec5d837
@@ -93,13 +93,34 @@ 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;

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);
@@ -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 ) ||
@@ -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 )

0 comments on commit 51a63b9

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