Skip to content
Permalink
Browse files
improve filtering of DB tables (hide useless schemas)
  • Loading branch information
3nids committed Nov 9, 2021
1 parent 622f751 commit 5fb223649181a5f24d10cc1304e7715fdf1c3906
Showing with 55 additions and 6 deletions.
  1. +1 −0 python/core/auto_generated/qgsdbfilterproxymodel.sip.in
  2. +50 −6 src/core/qgsdbfilterproxymodel.cpp
  3. +4 −0 src/core/qgsdbfilterproxymodel.h
@@ -42,6 +42,7 @@ Calls QSortFilterProxyModel.setFilterRegExp and triggers update
protected:
virtual bool filterAcceptsRow( int row, const QModelIndex &source_parent ) const;


};

/************************************************************************
@@ -17,24 +17,68 @@

#include "qgsdbfilterproxymodel.h"

#include <QStandardItemModel>

QgsDatabaseFilterProxyModel::QgsDatabaseFilterProxyModel( QObject *parent ): QSortFilterProxyModel( parent )
{

}

bool QgsDatabaseFilterProxyModel::filterAcceptsRow( int row, const QModelIndex &source_parent ) const
bool QgsDatabaseFilterProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
{
//if parent is valid, we have a toplevel item that should be always shown
if ( !source_parent.isValid() )
if ( filterAcceptsRowItself( source_row, source_parent ) )
return true;

//accept if any of the parents is accepted on it's own merits
QModelIndex parent = source_parent;
while ( parent.isValid() )
{
if ( filterAcceptsRowItself( parent.row(), parent.parent() ) )
return true;
parent = parent.parent();
}

//accept if any of the children is accepted on it's own merits
if ( hasAcceptedChildren( source_row, source_parent ) )
{
return true;
}

//else we have a row that describes a table and that
//should be tested using the given wildcard/regexp
return QSortFilterProxyModel::filterAcceptsRow( row, source_parent );
return false;
}

bool QgsDatabaseFilterProxyModel::filterAcceptsRowItself( int source_row, const QModelIndex &source_parent ) const
{
return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
}

bool QgsDatabaseFilterProxyModel::hasAcceptedChildren( int source_row, const QModelIndex &source_parent ) const
{
QModelIndex item = sourceModel()->index( source_row, 0, source_parent );
if ( !item.isValid() )
{
return false;
}

//check if there are children
int childCount = item.model()->rowCount( item );
if ( childCount == 0 )
return false;

for ( int i = 0; i < childCount; ++i )
{
if ( filterAcceptsRowItself( i, item ) )
return true;
if ( hasAcceptedChildren( i, item ) )
return true;
}

return false;
}




void QgsDatabaseFilterProxyModel::_setFilterWildcard( const QString &pattern )
{
QSortFilterProxyModel::setFilterWildcard( pattern );
@@ -49,6 +49,10 @@ class CORE_EXPORT QgsDatabaseFilterProxyModel: public QSortFilterProxyModel

protected:
bool filterAcceptsRow( int row, const QModelIndex &source_parent ) const override;

private:
bool filterAcceptsRowItself( int source_row, const QModelIndex &source_parent ) const;
bool hasAcceptedChildren( int source_row, const QModelIndex &source_parent ) const;
};

#endif

0 comments on commit 5fb2236

Please sign in to comment.