Skip to content

Commit

Permalink
improve filtering of DB tables (hide useless schemas)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Nov 9, 2021
1 parent 622f751 commit 5fb2236
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsdbfilterproxymodel.sip.in
Expand Up @@ -42,6 +42,7 @@ Calls QSortFilterProxyModel.setFilterRegExp and triggers update
protected:
virtual bool filterAcceptsRow( int row, const QModelIndex &source_parent ) const;


};

/************************************************************************
Expand Down
56 changes: 50 additions & 6 deletions src/core/qgsdbfilterproxymodel.cpp
Expand Up @@ -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 );
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsdbfilterproxymodel.h
Expand Up @@ -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.