17#include "moc_qgsbrowserproxymodel.cpp" 
   22#include <QRegularExpression> 
   25  : QSortFilterProxyModel( parent )
 
   27  setDynamicSortFilter( 
true );
 
   29  setSortCaseSensitivity( Qt::CaseInsensitive );
 
 
   36  setSourceModel( model );
 
 
   41  const QModelIndex sourceIndex = mapToSource( index );
 
 
   47  if ( mPatternSyntax == syntax )
 
   49  mPatternSyntax = syntax;
 
 
   55  return mPatternSyntax;
 
 
   60  if ( mFilter == filter )
 
 
   73  mCaseSensitivity = sensitivity;
 
 
   79  return mCaseSensitivity;
 
 
   82void QgsBrowserProxyModel::updateFilter()
 
   85  switch ( mPatternSyntax )
 
   89      const QStringList filterParts = mFilter.split( 
'|' );
 
   90      for ( 
const QString &f : filterParts )
 
   92        const QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( QStringLiteral( 
"*%1*" ).arg( f.trimmed() ) ),
 
   93                                     mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
 
  100      const QStringList filterParts = mFilter.split( 
'|' );
 
  101      for ( 
const QString &f : filterParts )
 
  103        const QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( f.trimmed() ),
 
  104                                     mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
 
  105        mREList.append( rx );
 
  111      const QRegularExpression rx( mFilter.trimmed(), mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
 
  112      mREList.append( rx );
 
  119bool QgsBrowserProxyModel::filterAcceptsString( 
const QString &value )
 const 
  121  for ( 
const QRegularExpression &rx : mREList )
 
  123    if ( rx.match( value ).hasMatch() )
 
  132  if ( ( mFilter.isEmpty() && !mFilterByLayerType && mHiddenDataItemsKeys.empty() && mShownDataItemsKeys.empty() ) || !
mModel )
 
  135  const QModelIndex sourceIndex = 
mModel->
index( sourceRow, 0, sourceParent );
 
  136  if ( !filterAcceptsProviderKey( sourceIndex ) || !filterRootAcceptsProviderKey( sourceIndex ) )
 
  142    if ( qobject_cast< QgsLayerItem * >( item ) )
 
  148  return filterAcceptsItem( sourceIndex ) || filterAcceptsAncestor( sourceIndex ) || filterAcceptsDescendant( sourceIndex );
 
 
  178bool QgsBrowserProxyModel::filterAcceptsAncestor( 
const QModelIndex &sourceIndex )
 const 
  183  if ( mFilterByLayerType )
 
  186  const QModelIndex sourceParentIndex = 
mModel->
parent( sourceIndex );
 
  187  if ( !sourceParentIndex.isValid() )
 
  189  if ( filterAcceptsItem( sourceParentIndex ) )
 
  192  return filterAcceptsAncestor( sourceParentIndex );
 
  195bool QgsBrowserProxyModel::filterAcceptsDescendant( 
const QModelIndex &sourceIndex )
 const 
  202    const QModelIndex sourceChildIndex = 
mModel->
index( i, 0, sourceIndex );
 
  203    if ( filterAcceptsItem( sourceChildIndex ) )
 
  205    if ( filterAcceptsDescendant( sourceChildIndex ) )
 
  211bool QgsBrowserProxyModel::filterAcceptsItem( 
const QModelIndex &sourceIndex )
 const 
  216  if ( mFilterByLayerType )
 
  219    if ( 
QgsLayerItem *layerItem = qobject_cast< QgsLayerItem * >( item ) )
 
  221      if ( layerItem->mapLayerType() != mLayerType )
 
  224    else if ( !qobject_cast< QgsDataCollectionItem * >( item ) )
 
  228  if ( !mFilter.isEmpty() )
 
  232    return ( filterAcceptsString( 
mModel->
data( sourceIndex, Qt::DisplayRole ).toString() )
 
  233             || ( !comment.isEmpty() && filterAcceptsString( comment ) )
 
  240bool QgsBrowserProxyModel::filterAcceptsProviderKey( 
const QModelIndex &sourceIndex )
 const 
  246  if ( providerKey.isEmpty() )
 
  249  return !mHiddenDataItemsKeys.contains( providerKey ) && ( mShownDataItemsKeys.isEmpty() || mShownDataItemsKeys.contains( providerKey ) );
 
  252bool QgsBrowserProxyModel::filterRootAcceptsProviderKey( 
const QModelIndex &sourceIndex )
 const 
  257  const QModelIndex sourceParentIndex = 
mModel->
parent( sourceIndex );
 
  258  if ( !sourceParentIndex.isValid() )
 
  260    return filterAcceptsProviderKey( sourceIndex );
 
  263  return filterRootAcceptsProviderKey( sourceParentIndex );
 
  268  mHiddenDataItemsKeys = filter;
 
 
  274  mShownDataItemsKeys = filter;
 
 
  281  const bool isFertile { QSortFilterProxyModel::hasChildren( parent ) };
 
  282  if ( isFertile && parent.isValid() )
 
  290    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.
 
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
 
@ 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,...
 
A browser item that represents a layer that can be opened with one of the providers.