Skip to content

Commit

Permalink
Browser proxy model: add white list filter and renamed black one
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Mar 11, 2020
1 parent a3f4911 commit 31aeb6e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
22 changes: 20 additions & 2 deletions python/core/auto_generated/qgsbrowserproxymodel.sip.in
Expand Up @@ -133,9 +133,9 @@ filterByLayerType() is ``True``.
.. seealso:: :py:func:`setFilterByLayerType`
%End

void setDataItemProviderKeyFilter( const QStringList &hiddenItemsFilter );
void setHiddenDataItemProviderKeyFilter( const QStringList &hiddenItemsFilter );
%Docstring
Sets a filter to hide data items based on on item's data provider key.
Sets a filter to hide data items based on on item's data item provider key.

By default browser model shows all items from all available data item providers and few special
items (e.g. Favourites).
Expand All @@ -149,6 +149,24 @@ This filter is always evaluated.
:param hiddenItemsFilter: a list of data provider prefixes that will be hidden.

.. versionadded:: 3.12
%End

void setShownDataItemProviderKeyFilter( const QStringList &shownItemsFilter );
%Docstring
Sets a filter to show data items based on on item's data item provider key.

By default browser model shows all items from all available data item providers and few special
items (e.g. Favourites).
To customize the behavior, set the filter to load only certain data items.
The items that are not based on data item providers have prefix "special:", for example
"special:Favorites", "special:Home", "PostGIS", "MSSQL"

Only the items created by the providers listed in filter are shown in the layer tree.
This filter is always evaluated.

:param shownItemsFilter: a list of data provider prefixes that will be hidden.

.. versionadded:: 3.14
%End

bool showLayers() const;
Expand Down
13 changes: 10 additions & 3 deletions src/core/qgsbrowserproxymodel.cpp
Expand Up @@ -146,7 +146,7 @@ bool QgsBrowserProxyModel::filterAcceptsString( const QString &value ) const

bool QgsBrowserProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
{
if ( ( mFilter.isEmpty() && !mFilterByLayerType && mHiddenDataItemsKeys.empty() ) || !mModel )
if ( ( mFilter.isEmpty() && !mFilterByLayerType && mHiddenDataItemsKeys.empty() && mShownDataItemsKeys.empty() ) || !mModel )
return true;

QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
Expand Down Expand Up @@ -262,7 +262,7 @@ bool QgsBrowserProxyModel::filterAcceptsProviderKey( const QModelIndex &sourceIn
if ( providerKey.isEmpty() )
return true;

return !mHiddenDataItemsKeys.contains( providerKey );
return !mHiddenDataItemsKeys.contains( providerKey ) && ( mShownDataItemsKeys.isEmpty() || mShownDataItemsKeys.contains( providerKey ) );
}

bool QgsBrowserProxyModel::filterRootAcceptsProviderKey( const QModelIndex &sourceIndex ) const
Expand All @@ -279,12 +279,19 @@ bool QgsBrowserProxyModel::filterRootAcceptsProviderKey( const QModelIndex &sour
return filterRootAcceptsProviderKey( sourceParentIndex );
}

void QgsBrowserProxyModel::setDataItemProviderKeyFilter( const QStringList &filter )
void QgsBrowserProxyModel::setHiddenDataItemProviderKeyFilter( const QStringList &filter )
{
mHiddenDataItemsKeys = filter;
invalidateFilter();
}

void QgsBrowserProxyModel::setShownDataItemProviderKeyFilter( const QStringList &filter )
{
mShownDataItemsKeys = filter;
invalidateFilter();

}


bool QgsBrowserProxyModel::hasChildren( const QModelIndex &parent ) const
{
Expand Down
23 changes: 21 additions & 2 deletions src/core/qgsbrowserproxymodel.h
Expand Up @@ -144,7 +144,7 @@ class CORE_EXPORT QgsBrowserProxyModel : public QSortFilterProxyModel
void setLayerType( QgsMapLayerType type );

/**
* Sets a filter to hide data items based on on item's data provider key.
* Sets a filter to hide data items based on on item's data item provider key.
*
* By default browser model shows all items from all available data item providers and few special
* items (e.g. Favourites).
Expand All @@ -159,7 +159,25 @@ class CORE_EXPORT QgsBrowserProxyModel : public QSortFilterProxyModel
*
* \since QGIS 3.12
*/
void setDataItemProviderKeyFilter( const QStringList &hiddenItemsFilter );
void setHiddenDataItemProviderKeyFilter( const QStringList &hiddenItemsFilter );

/**
* Sets a filter to show data items based on on item's data item provider key.
*
* By default browser model shows all items from all available data item providers and few special
* items (e.g. Favourites).
* To customize the behavior, set the filter to load only certain data items.
* The items that are not based on data item providers have prefix "special:", for example
* "special:Favorites", "special:Home", "PostGIS", "MSSQL"
*
* Only the items created by the providers listed in filter are shown in the layer tree.
* This filter is always evaluated.
*
* \param shownItemsFilter a list of data provider prefixes that will be hidden.
*
* \since QGIS 3.14
*/
void setShownDataItemProviderKeyFilter( const QStringList &shownItemsFilter );

/**
* Returns TRUE if layers must be shown, this flag is TRUE by default.
Expand All @@ -184,6 +202,7 @@ class CORE_EXPORT QgsBrowserProxyModel : public QSortFilterProxyModel

private:
QStringList mHiddenDataItemsKeys;
QStringList mShownDataItemsKeys;
QgsBrowserModel *mModel = nullptr;
QString mFilter; //filter string provided
QVector<QRegExp> mREList; //list of filters, separated by "|"
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsbrowserdockwidget.cpp
Expand Up @@ -128,7 +128,7 @@ void QgsBrowserDockWidget::showEvent( QShowEvent *e )
{
mProxyModel = new QgsBrowserProxyModel( this );
mProxyModel->setBrowserModel( mModel );
mProxyModel->setDataItemProviderKeyFilter( mDisabledDataItemsKeys );
mProxyModel->setHiddenDataItemProviderKeyFilter( mDisabledDataItemsKeys );
mBrowserView->setSettingsSection( objectName().toLower() ); // to distinguish 2 or more instances of the browser
mBrowserView->setBrowserModel( mModel );
mBrowserView->setModel( mProxyModel );
Expand Down Expand Up @@ -297,7 +297,7 @@ void QgsBrowserDockWidget::setDisabledDataItemsKeys( const QStringList &filter )
if ( !mProxyModel )
return;

mProxyModel->setDataItemProviderKeyFilter( mDisabledDataItemsKeys );
mProxyModel->setHiddenDataItemProviderKeyFilter( mDisabledDataItemsKeys );
}

void QgsBrowserDockWidget::removeFavorite()
Expand Down
20 changes: 17 additions & 3 deletions tests/src/core/testqgsbrowserproxymodel.cpp
Expand Up @@ -268,16 +268,30 @@ void TestQgsBrowserProxyModel::testModel()
proxy.setFilterByLayerType( false );

// provider filtering
proxy.setDataItemProviderKeyFilter( QStringList( {QStringLiteral( "provider1" )} ) );
proxy.setHiddenDataItemProviderKeyFilter( QStringList( {QStringLiteral( "provider1" )} ) );
QCOMPARE( proxy.rowCount(), 2 );
root1Index = proxy.index( 0, 0 );
QCOMPARE( proxy.rowCount( root1Index ), 1 );
proxy.setDataItemProviderKeyFilter( QStringList( {QStringLiteral( "provider2" )} ) );
proxy.setHiddenDataItemProviderKeyFilter( QStringList( {QStringLiteral( "provider2" )} ) );
QCOMPARE( proxy.rowCount(), 1 );
root1Index = proxy.index( 0, 0 );
QCOMPARE( proxy.rowCount( root1Index ), 2 );
proxy.setDataItemProviderKeyFilter( QStringList() );
proxy.setHiddenDataItemProviderKeyFilter( QStringList() );
QCOMPARE( proxy.rowCount(), 2 );

// provider filtering
proxy.setHiddenDataItemProviderKeyFilter( QStringList( ) );
proxy.setShownDataItemProviderKeyFilter( QStringList( {QStringLiteral( "provider2" )} ) );
QCOMPARE( proxy.rowCount(), 2 );
root1Index = proxy.index( 0, 0 );
QCOMPARE( proxy.rowCount( root1Index ), 1 );
proxy.setShownDataItemProviderKeyFilter( QStringList( {QStringLiteral( "provider1" )} ) );
QCOMPARE( proxy.rowCount(), 1 );
root1Index = proxy.index( 0, 0 );
QCOMPARE( proxy.rowCount( root1Index ), 2 );
proxy.setShownDataItemProviderKeyFilter( QStringList() );
QCOMPARE( proxy.rowCount(), 2 );

}

void TestQgsBrowserProxyModel::testShowLayers()
Expand Down

0 comments on commit 31aeb6e

Please sign in to comment.