Skip to content

Commit

Permalink
Allow QgsMapLayerProxyModel to be filtered by string
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 22, 2018
1 parent 5582a57 commit 51a63b9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
21 changes: 21 additions & 0 deletions python/core/auto_generated/qgsmaplayerproxymodel.sip.in
Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion src/core/qgsmaplayerproxymodel.cpp
Expand Up @@ -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 );
Expand All @@ -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 ) ||
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgsmaplayerproxymodel.h
Expand Up @@ -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 )
Expand Down

0 comments on commit 51a63b9

Please sign in to comment.