Skip to content
Permalink
Browse files

Browser proxy model: add white list filter and renamed black one

  • Loading branch information
elpaso committed Mar 11, 2020
1 parent a3f4911 commit 31aeb6e351fb710da5338db2c2166d10c5cca415
@@ -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).
@@ -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;
@@ -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 );
@@ -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
@@ -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
{
@@ -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).
@@ -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.
@@ -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 "|"
@@ -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 );
@@ -297,7 +297,7 @@ void QgsBrowserDockWidget::setDisabledDataItemsKeys( const QStringList &filter )
if ( !mProxyModel )
return;

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

void QgsBrowserDockWidget::removeFavorite()
@@ -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()

0 comments on commit 31aeb6e

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