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() ) || !mModel )
152 QModelIndex sourceIndex = mModel->
index( sourceRow, 0, sourceParent );
153 if ( !filterAcceptsProviderKey( sourceIndex ) || !filterRootAcceptsProviderKey( sourceIndex ) )
156 return filterAcceptsItem( sourceIndex ) || filterAcceptsAncestor( sourceIndex ) || filterAcceptsDescendant( sourceIndex );
176 bool QgsBrowserProxyModel::filterAcceptsAncestor(
const QModelIndex &sourceIndex )
const 181 if ( mFilterByLayerType )
184 QModelIndex sourceParentIndex = mModel->
parent( sourceIndex );
185 if ( !sourceParentIndex.isValid() )
187 if ( filterAcceptsItem( sourceParentIndex ) )
190 return filterAcceptsAncestor( sourceParentIndex );
193 bool QgsBrowserProxyModel::filterAcceptsDescendant(
const QModelIndex &sourceIndex )
const 198 for (
int i = 0; i < mModel->
rowCount( sourceIndex ); i++ )
200 QModelIndex sourceChildIndex = mModel->
index( i, 0, sourceIndex );
201 if ( filterAcceptsItem( sourceChildIndex ) )
203 if ( filterAcceptsDescendant( sourceChildIndex ) )
209 bool QgsBrowserProxyModel::filterAcceptsItem(
const QModelIndex &sourceIndex )
const 214 if ( mFilterByLayerType )
217 if (
QgsLayerItem *layerItem = qobject_cast< QgsLayerItem * >( item ) )
219 if ( layerItem->mapLayerType() != mLayerType )
222 else if ( !qobject_cast< QgsDataCollectionItem * >( item ) )
226 if ( !mFilter.isEmpty() )
230 return ( filterAcceptsString( mModel->
data( sourceIndex, Qt::DisplayRole ).toString() )
231 || ( !comment.isEmpty() && filterAcceptsString( comment ) ) );
237 bool QgsBrowserProxyModel::filterAcceptsProviderKey(
const QModelIndex &sourceIndex )
const 243 if ( providerKey.isEmpty() )
246 return !mHiddenDataItemsKeys.contains( providerKey );
249 bool QgsBrowserProxyModel::filterRootAcceptsProviderKey(
const QModelIndex &sourceIndex )
const 254 QModelIndex sourceParentIndex = mModel->
parent( sourceIndex );
255 if ( !sourceParentIndex.isValid() )
257 return filterAcceptsProviderKey( sourceIndex );
260 return filterRootAcceptsProviderKey( sourceParentIndex );
265 mHiddenDataItemsKeys = filter;
Qt::CaseSensitivity caseSensitivity() const
Returns whether item filtering is case sensitive.
FilterSyntax filterSyntax() const
Returns the filter syntax.
FilterSyntax
Filter syntax options.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void setLayerType(QgsMapLayerType type)
Sets the layer type to filter the model by.
void setDataItemProviderKeyFilter(const QStringList &filter)
Sets the customization filters for data items based on item's data provider key.
void setBrowserModel(QgsBrowserModel *model)
Sets the underlying browser model.
QgsMapLayerType layerType() const
Returns the layer type to filter the model by.
QgsBrowserProxyModel(QObject *parent=nullptr)
Constructor for QgsBrowserProxyModel, with the specified parent object.
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
QModelIndex parent(const QModelIndex &index) const override
Regular expression filtering.
void setFilterString(const QString &filter)
Sets the filter string to use when filtering items in the model.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index...
QString filterString() const
Returns the filter string used when filtering items in the model.
void setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
bool filterByLayerType() const
Returns true if the model is filtered by map layer type.
Base class for all items in the model.
Custom sort role, see QgsDataItem::sortKey()
Data item provider key that created the item,.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Standard string filtering.
A model for showing available data sources and other items in a structured tree.
Item that represents a layer that can be opened with one of the providers.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
void setFilterByLayerType(bool enabled)
Sets whether the model is filtered by map layer type.
QgsMapLayerType
Types of layers that can be added to a map.
QgsDataItem * dataItem(const QModelIndex &index) const
Returns the data item at the specified proxy index, or nullptr if no item exists at the index...