22#include <QRegularExpression>
24#include "moc_qgsbrowserproxymodel.cpp"
27 : QSortFilterProxyModel( parent )
29 setDynamicSortFilter(
true );
31 setSortCaseSensitivity( Qt::CaseInsensitive );
38 setSourceModel( model );
43 const QModelIndex sourceIndex = mapToSource( index );
44 return mModel ?
mModel->dataItem( sourceIndex ) :
nullptr;
49 if ( mPatternSyntax == syntax )
51 mPatternSyntax = syntax;
57 return mPatternSyntax;
62 if ( mFilter == filter )
75 mCaseSensitivity = sensitivity;
81 return mCaseSensitivity;
84void QgsBrowserProxyModel::updateFilter()
87 switch ( mPatternSyntax )
91 const QStringList filterParts = mFilter.split(
'|' );
92 for (
const QString &f : filterParts )
94 const QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( QStringLiteral(
"*%1*" ).arg( f.trimmed() ) ),
95 mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
102 const QStringList filterParts = mFilter.split(
'|' );
103 for (
const QString &f : filterParts )
105 const QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( f.trimmed() ),
106 mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
107 mREList.append( rx );
113 const QRegularExpression rx( mFilter.trimmed(), mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
114 mREList.append( rx );
121bool QgsBrowserProxyModel::filterAcceptsString(
const QString &value )
const
123 for (
const QRegularExpression &rx : mREList )
125 if ( rx.match( value ).hasMatch() )
134 if ( ( mFilter.isEmpty() && !mFilterByLayerType && mHiddenDataItemsKeys.empty() && mShownDataItemsKeys.empty() ) || !
mModel )
137 const QModelIndex sourceIndex =
mModel->index( sourceRow, 0, sourceParent );
138 if ( !filterAcceptsProviderKey( sourceIndex ) || !filterRootAcceptsProviderKey( sourceIndex ) )
144 if ( qobject_cast< QgsLayerItem * >( item ) )
150 return filterAcceptsItem( sourceIndex ) || filterAcceptsAncestor( sourceIndex ) || filterAcceptsDescendant( sourceIndex );
180bool QgsBrowserProxyModel::filterAcceptsAncestor(
const QModelIndex &sourceIndex )
const
185 if ( mFilterByLayerType )
188 const QModelIndex sourceParentIndex =
mModel->
parent( sourceIndex );
189 if ( !sourceParentIndex.isValid() )
191 if ( filterAcceptsItem( sourceParentIndex ) )
194 return filterAcceptsAncestor( sourceParentIndex );
197bool QgsBrowserProxyModel::filterAcceptsDescendant(
const QModelIndex &sourceIndex )
const
202 for (
int i = 0; i <
mModel->rowCount( sourceIndex ); i++ )
204 const QModelIndex sourceChildIndex =
mModel->index( i, 0, sourceIndex );
205 if ( filterAcceptsItem( sourceChildIndex ) )
207 if ( filterAcceptsDescendant( sourceChildIndex ) )
213bool QgsBrowserProxyModel::filterAcceptsItem(
const QModelIndex &sourceIndex )
const
218 if ( mFilterByLayerType )
220 QgsDataItem *item =
mModel->dataItem( sourceIndex );
221 if ( QgsLayerItem *layerItem = qobject_cast< QgsLayerItem * >( item ) )
223 if ( layerItem->mapLayerType() != mLayerType )
226 else if ( !qobject_cast< QgsDataCollectionItem * >( item ) )
230 if ( !mFilter.isEmpty() )
234 return ( filterAcceptsString(
mModel->data( sourceIndex, Qt::DisplayRole ).toString() )
235 || ( !comment.isEmpty() && filterAcceptsString( comment ) )
242bool QgsBrowserProxyModel::filterAcceptsProviderKey(
const QModelIndex &sourceIndex )
const
248 if ( providerKey.isEmpty() )
251 return !mHiddenDataItemsKeys.contains( providerKey ) && ( mShownDataItemsKeys.isEmpty() || mShownDataItemsKeys.contains( providerKey ) );
254bool QgsBrowserProxyModel::filterRootAcceptsProviderKey(
const QModelIndex &sourceIndex )
const
259 const QModelIndex sourceParentIndex =
mModel->parent( sourceIndex );
260 if ( !sourceParentIndex.isValid() )
262 return filterAcceptsProviderKey( sourceIndex );
265 return filterRootAcceptsProviderKey( sourceParentIndex );
270 mHiddenDataItemsKeys = filter;
276 mShownDataItemsKeys = filter;
283 const bool isFertile { QSortFilterProxyModel::hasChildren( parent ) };
284 if ( isFertile && parent.isValid() )
287 if ( item && !mShowLayers )
292 else if ( mFilterByLayerType && qobject_cast< QgsLayerItem * >( item ) )
LayerType
Types of layers that can be added to a map.
A model for showing available data sources and other items in a structured tree.
QModelIndex parent(const QModelIndex &index) const override
@ Sort
Custom sort role, see QgsDataItem::sortKey().
@ ProviderKey
Data item provider key that created the item, see QgsDataItem::providerKey().
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 setLayerType(Qgis::LayerType type)
Sets the layer type to filter the model by.
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.
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.
Qgis::LayerType layerType() const
Returns the layer type to filter the model by.
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,...