21 #include <QRegularExpression>
24 : QSortFilterProxyModel( parent )
26 setDynamicSortFilter(
true );
28 setSortCaseSensitivity( Qt::CaseInsensitive );
35 setSourceModel( model );
40 const QModelIndex sourceIndex = mapToSource( index );
46 if ( mPatternSyntax == syntax )
48 mPatternSyntax = syntax;
54 return mPatternSyntax;
59 if ( mFilter == filter )
72 mCaseSensitivity = sensitivity;
78 return mCaseSensitivity;
81 void QgsBrowserProxyModel::updateFilter()
84 switch ( mPatternSyntax )
88 const QStringList filterParts = mFilter.split(
'|' );
89 for (
const QString &f : filterParts )
91 const QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( QStringLiteral(
"*%1*" ).arg( f.trimmed() ) ),
92 mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
99 const QStringList filterParts = mFilter.split(
'|' );
100 for (
const QString &f : filterParts )
102 const QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( f.trimmed() ),
103 mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
104 mREList.append( rx );
110 const QRegularExpression rx( mFilter.trimmed(), mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
111 mREList.append( rx );
118 bool QgsBrowserProxyModel::filterAcceptsString(
const QString &value )
const
120 for (
const QRegularExpression &rx : mREList )
122 if ( rx.match( value ).hasMatch() )
131 if ( ( mFilter.isEmpty() && !mFilterByLayerType && mHiddenDataItemsKeys.empty() && mShownDataItemsKeys.empty() ) || !
mModel )
134 const QModelIndex sourceIndex =
mModel->
index( sourceRow, 0, sourceParent );
135 if ( !filterAcceptsProviderKey( sourceIndex ) || !filterRootAcceptsProviderKey( sourceIndex ) )
141 if ( qobject_cast< QgsLayerItem * >( item ) )
147 return filterAcceptsItem( sourceIndex ) || filterAcceptsAncestor( sourceIndex ) || filterAcceptsDescendant( sourceIndex );
177 bool QgsBrowserProxyModel::filterAcceptsAncestor(
const QModelIndex &sourceIndex )
const
182 if ( mFilterByLayerType )
185 const QModelIndex sourceParentIndex =
mModel->
parent( sourceIndex );
186 if ( !sourceParentIndex.isValid() )
188 if ( filterAcceptsItem( sourceParentIndex ) )
191 return filterAcceptsAncestor( sourceParentIndex );
194 bool QgsBrowserProxyModel::filterAcceptsDescendant(
const QModelIndex &sourceIndex )
const
201 const QModelIndex sourceChildIndex =
mModel->
index( i, 0, sourceIndex );
202 if ( filterAcceptsItem( sourceChildIndex ) )
204 if ( filterAcceptsDescendant( sourceChildIndex ) )
210 bool QgsBrowserProxyModel::filterAcceptsItem(
const QModelIndex &sourceIndex )
const
215 if ( mFilterByLayerType )
218 if (
QgsLayerItem *layerItem = qobject_cast< QgsLayerItem * >( item ) )
220 if ( layerItem->mapLayerType() != mLayerType )
223 else if ( !qobject_cast< QgsDataCollectionItem * >( item ) )
227 if ( !mFilter.isEmpty() )
231 return ( filterAcceptsString(
mModel->
data( sourceIndex, Qt::DisplayRole ).toString() )
232 || ( !comment.isEmpty() && filterAcceptsString( comment ) ) );
238 bool QgsBrowserProxyModel::filterAcceptsProviderKey(
const QModelIndex &sourceIndex )
const
244 if ( providerKey.isEmpty() )
247 return !mHiddenDataItemsKeys.contains( providerKey ) && ( mShownDataItemsKeys.isEmpty() || mShownDataItemsKeys.contains( providerKey ) );
250 bool QgsBrowserProxyModel::filterRootAcceptsProviderKey(
const QModelIndex &sourceIndex )
const
255 const QModelIndex sourceParentIndex =
mModel->
parent( sourceIndex );
256 if ( !sourceParentIndex.isValid() )
258 return filterAcceptsProviderKey( sourceIndex );
261 return filterRootAcceptsProviderKey( sourceParentIndex );
266 mHiddenDataItemsKeys = filter;
272 mShownDataItemsKeys = filter;
279 const bool isFertile { QSortFilterProxyModel::hasChildren( parent ) };
280 if ( isFertile && parent.isValid() )
288 else if ( mFilterByLayerType && qobject_cast< QgsLayerItem * >( item ) )
A model for showing available data sources and other items in a structured tree.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex parent(const QModelIndex &index) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
@ CommentRole
Item comment.
@ ProviderKeyRole
Data item provider key that created the item, see QgsDataItem::providerKey()
@ SortRole
Custom sort role, see QgsDataItem::sortKey()
void setShowLayers(bool showLayers)
Sets show layers to showLayers.
FilterSyntax
Filter syntax options.
@ RegularExpression
Regular expression filtering.
@ Wildcards
Wildcard filtering.
@ Normal
Standard string filtering.
void setShownDataItemProviderKeyFilter(const QStringList &shownItemsFilter)
Sets a filter to show data items based on QgsDataItem::providerKey() associated with the item.
void setHiddenDataItemProviderKeyFilter(const QStringList &hiddenItemsFilter)
Sets a filter to hide data items based on QgsDataItem::providerKey() associated with the item.
void setFilterByLayerType(bool enabled)
Sets whether the model is filtered by map layer type.
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
void setFilterString(const QString &filter)
Sets the filter string to use when filtering items in the model.
QgsDataItem * dataItem(const QModelIndex &index) const
Returns the data item at the specified proxy index, or nullptr if no item exists at the index.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
void setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
QString filterString() const
Returns the filter string used when filtering items in the model.
QgsBrowserProxyModel(QObject *parent=nullptr)
Constructor for QgsBrowserProxyModel, with the specified parent object.
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
QgsMapLayerType layerType() const
Returns the layer type to filter the model by.
void setLayerType(QgsMapLayerType type)
Sets the layer type to filter the model by.
Qt::CaseSensitivity caseSensitivity() const
Returns whether item filtering is case sensitive.
bool filterByLayerType() const
Returns true if the model is filtered by map layer type.
bool showLayers() const
Returns true if layers must be shown, this flag is TRUE by default.
FilterSyntax filterSyntax() const
Returns the filter syntax.
void setBrowserModel(QgsBrowserModel *model)
Sets the underlying browser model.
QgsBrowserModel * mModel
Reference to associated browser model.
Base class for all items in the model.
virtual bool layerCollection() const
Returns true if the data item is a collection of layers The default implementation returns false,...
Item that represents a layer that can be opened with one of the providers.
QgsMapLayerType
Types of layers that can be added to a map.