20 : QSortFilterProxyModel( parent )
22 setDynamicSortFilter(
true );
24 setSortCaseSensitivity( Qt::CaseInsensitive );
31 setSourceModel( model );
36 const QModelIndex sourceIndex = mapToSource( index );
37 return mModel ? mModel->
dataItem( sourceIndex ) :
nullptr;
42 if ( mPatternSyntax == syntax )
44 mPatternSyntax = syntax;
50 return mPatternSyntax;
55 if ( mFilter == filter )
68 mCaseSensitivity = sensitivity;
74 return mCaseSensitivity;
77 void QgsBrowserProxyModel::updateFilter()
80 switch ( mPatternSyntax )
84 const QStringList filterParts = mFilter.split(
'|' );
85 for (
const QString &f : filterParts )
87 QRegExp rx( QStringLiteral(
"*%1*" ).arg( f.trimmed() ) );
88 rx.setPatternSyntax( QRegExp::Wildcard );
89 rx.setCaseSensitivity( mCaseSensitivity );
96 const QStringList filterParts = mFilter.split(
'|' );
97 for (
const QString &f : filterParts )
99 QRegExp rx( f.trimmed() );
100 rx.setPatternSyntax( QRegExp::Wildcard );
101 rx.setCaseSensitivity( mCaseSensitivity );
102 mREList.append( rx );
108 QRegExp rx( mFilter.trimmed() );
109 rx.setPatternSyntax( QRegExp::RegExp );
110 rx.setCaseSensitivity( mCaseSensitivity );
111 mREList.append( rx );
118 bool QgsBrowserProxyModel::filterAcceptsString(
const QString &value )
const
120 switch ( mPatternSyntax )
125 for (
const QRegExp &rx : mREList )
127 if ( rx.exactMatch( value ) )
135 for (
const QRegExp &rx : mREList )
137 if ( rx.indexIn( value ) != -1 )
149 if ( ( mFilter.isEmpty() && !mFilterByLayerType && mHiddenDataItemsKeys.empty() && mShownDataItemsKeys.empty() ) || !mModel )
152 QModelIndex sourceIndex = mModel->
index( sourceRow, 0, sourceParent );
153 if ( !filterAcceptsProviderKey( sourceIndex ) || !filterRootAcceptsProviderKey( sourceIndex ) )
159 if ( qobject_cast< QgsLayerItem * >( item ) )
165 return filterAcceptsItem( sourceIndex ) || filterAcceptsAncestor( sourceIndex ) || filterAcceptsDescendant( sourceIndex );
195 bool QgsBrowserProxyModel::filterAcceptsAncestor(
const QModelIndex &sourceIndex )
const
200 if ( mFilterByLayerType )
203 QModelIndex sourceParentIndex = mModel->
parent( sourceIndex );
204 if ( !sourceParentIndex.isValid() )
206 if ( filterAcceptsItem( sourceParentIndex ) )
209 return filterAcceptsAncestor( sourceParentIndex );
212 bool QgsBrowserProxyModel::filterAcceptsDescendant(
const QModelIndex &sourceIndex )
const
217 for (
int i = 0; i < mModel->
rowCount( sourceIndex ); i++ )
219 QModelIndex sourceChildIndex = mModel->
index( i, 0, sourceIndex );
220 if ( filterAcceptsItem( sourceChildIndex ) )
222 if ( filterAcceptsDescendant( sourceChildIndex ) )
228 bool QgsBrowserProxyModel::filterAcceptsItem(
const QModelIndex &sourceIndex )
const
233 if ( mFilterByLayerType )
236 if (
QgsLayerItem *layerItem = qobject_cast< QgsLayerItem * >( item ) )
238 if ( layerItem->mapLayerType() != mLayerType )
241 else if ( !qobject_cast< QgsDataCollectionItem * >( item ) )
245 if ( !mFilter.isEmpty() )
249 return ( filterAcceptsString( mModel->
data( sourceIndex, Qt::DisplayRole ).toString() )
250 || ( !comment.isEmpty() && filterAcceptsString( comment ) ) );
256 bool QgsBrowserProxyModel::filterAcceptsProviderKey(
const QModelIndex &sourceIndex )
const
262 if ( providerKey.isEmpty() )
265 return !mHiddenDataItemsKeys.contains( providerKey ) && ( mShownDataItemsKeys.isEmpty() || mShownDataItemsKeys.contains( providerKey ) );
268 bool QgsBrowserProxyModel::filterRootAcceptsProviderKey(
const QModelIndex &sourceIndex )
const
273 QModelIndex sourceParentIndex = mModel->
parent( sourceIndex );
274 if ( !sourceParentIndex.isValid() )
276 return filterAcceptsProviderKey( sourceIndex );
279 return filterRootAcceptsProviderKey( sourceParentIndex );
284 mHiddenDataItemsKeys = filter;
290 mShownDataItemsKeys = filter;
298 bool isFertile { QSortFilterProxyModel::hasChildren( parent ) };
299 if ( isFertile && ! mShowLayers && parent.isValid() )