22 : QSortFilterProxyModel( parent )
24 setDynamicSortFilter(
true );
26 setSortCaseSensitivity( Qt::CaseInsensitive );
33 setSourceModel( model );
38 const QModelIndex sourceIndex = mapToSource( index );
44 if ( mPatternSyntax == syntax )
46 mPatternSyntax = syntax;
52 return mPatternSyntax;
57 if ( mFilter == filter )
70 mCaseSensitivity = sensitivity;
76 return mCaseSensitivity;
79 void QgsBrowserProxyModel::updateFilter()
82 switch ( mPatternSyntax )
86 const QStringList filterParts = mFilter.split(
'|' );
87 for (
const QString &f : filterParts )
89 QRegExp rx( QStringLiteral(
"*%1*" ).arg( f.trimmed() ) );
90 rx.setPatternSyntax( QRegExp::Wildcard );
91 rx.setCaseSensitivity( mCaseSensitivity );
98 const QStringList filterParts = mFilter.split(
'|' );
99 for (
const QString &f : filterParts )
101 QRegExp rx( f.trimmed() );
102 rx.setPatternSyntax( QRegExp::Wildcard );
103 rx.setCaseSensitivity( mCaseSensitivity );
104 mREList.append( rx );
110 QRegExp rx( mFilter.trimmed() );
111 rx.setPatternSyntax( QRegExp::RegExp );
112 rx.setCaseSensitivity( mCaseSensitivity );
113 mREList.append( rx );
120 bool QgsBrowserProxyModel::filterAcceptsString(
const QString &value )
const
122 switch ( mPatternSyntax )
127 for (
const QRegExp &rx : mREList )
129 if ( rx.exactMatch( value ) )
137 for (
const QRegExp &rx : mREList )
139 if ( rx.indexIn( value ) != -1 )
151 if ( ( mFilter.isEmpty() && !mFilterByLayerType && mHiddenDataItemsKeys.empty() && mShownDataItemsKeys.empty() ) || !
mModel )
154 QModelIndex sourceIndex =
mModel->
index( sourceRow, 0, sourceParent );
155 if ( !filterAcceptsProviderKey( sourceIndex ) || !filterRootAcceptsProviderKey( sourceIndex ) )
161 if ( qobject_cast< QgsLayerItem * >( item ) )
167 return filterAcceptsItem( sourceIndex ) || filterAcceptsAncestor( sourceIndex ) || filterAcceptsDescendant( sourceIndex );
197 bool QgsBrowserProxyModel::filterAcceptsAncestor(
const QModelIndex &sourceIndex )
const
202 if ( mFilterByLayerType )
205 QModelIndex sourceParentIndex =
mModel->
parent( sourceIndex );
206 if ( !sourceParentIndex.isValid() )
208 if ( filterAcceptsItem( sourceParentIndex ) )
211 return filterAcceptsAncestor( sourceParentIndex );
214 bool QgsBrowserProxyModel::filterAcceptsDescendant(
const QModelIndex &sourceIndex )
const
221 QModelIndex sourceChildIndex =
mModel->
index( i, 0, sourceIndex );
222 if ( filterAcceptsItem( sourceChildIndex ) )
224 if ( filterAcceptsDescendant( sourceChildIndex ) )
230 bool QgsBrowserProxyModel::filterAcceptsItem(
const QModelIndex &sourceIndex )
const
235 if ( mFilterByLayerType )
238 if (
QgsLayerItem *layerItem = qobject_cast< QgsLayerItem * >( item ) )
240 if ( layerItem->mapLayerType() != mLayerType )
243 else if ( !qobject_cast< QgsDataCollectionItem * >( item ) )
247 if ( !mFilter.isEmpty() )
251 return ( filterAcceptsString(
mModel->
data( sourceIndex, Qt::DisplayRole ).toString() )
252 || ( !comment.isEmpty() && filterAcceptsString( comment ) ) );
258 bool QgsBrowserProxyModel::filterAcceptsProviderKey(
const QModelIndex &sourceIndex )
const
264 if ( providerKey.isEmpty() )
267 return !mHiddenDataItemsKeys.contains( providerKey ) && ( mShownDataItemsKeys.isEmpty() || mShownDataItemsKeys.contains( providerKey ) );
270 bool QgsBrowserProxyModel::filterRootAcceptsProviderKey(
const QModelIndex &sourceIndex )
const
275 QModelIndex sourceParentIndex =
mModel->
parent( sourceIndex );
276 if ( !sourceParentIndex.isValid() )
278 return filterAcceptsProviderKey( sourceIndex );
281 return filterRootAcceptsProviderKey( sourceParentIndex );
286 mHiddenDataItemsKeys = filter;
292 mShownDataItemsKeys = filter;
299 bool isFertile { QSortFilterProxyModel::hasChildren( parent ) };
300 if ( isFertile && parent.isValid() )
308 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.