ResourceModel: implement filter functionality

So far all subclasses implemented filtering on their own which doesn't
make much sense. Now one can set filter keywords and a filter type for
all ResourceModels. Subclasses simply have to call
ResourceModel::itemMatchesFilter( ResourceItem ) in order to determine
whether an item should be hidden or not. Additionally they have to
implement the updateFilters() method in order to update their internal
management data each time the filters change.
This commit is contained in:
Tobias Doerffel
2009-08-21 16:49:27 +02:00
parent 36ec30f649
commit d3bb3ff13a
9 changed files with 80 additions and 61 deletions

View File

@@ -29,11 +29,7 @@ ResourceListModel::ResourceListModel( ResourceDB * _db, QObject * _parent ) :
ResourceModel( _db, _parent ),
m_lookupTable()
{
connect( db(), SIGNAL( itemsChanged() ),
this, SLOT( updateLookupTable() ),
Qt::DirectConnection );
updateLookupTable();
updateFilters();
}
@@ -63,27 +59,12 @@ QModelIndex ResourceListModel::index( int _row, int _col,
void ResourceListModel::setFilter( const QString & _s )
{
if( !_s.isEmpty() )
{
m_filterKeywords = _s.toLower().split( " " );
}
else
{
m_filterKeywords.clear();
}
updateLookupTable();
}
// update lookup table so ResourceListModel::index(...) can run with O(1)
void ResourceListModel::updateLookupTable()
void ResourceListModel::updateFilters()
{
// update lookup table so ResourceListModel::index(...) can run with O(1)
int items = 0;
if( m_filterKeywords.isEmpty() )
if( keywordFilter().isEmpty() &&
typeFilter() == ResourceItem::TypeUnknown )
{
// unhide all items if empty filter string given
m_lookupTable.resize( db()->items().size() );
@@ -99,7 +80,7 @@ void ResourceListModel::updateLookupTable()
// filter and count number of non-hidden items
foreach( ResourceItem * item, db()->items() )
{
if( item->keywordMatch( m_filterKeywords ) )
if( itemMatchesFilter( *item ) )
{
item->setHidden( false, this );
++items;
@@ -131,7 +112,4 @@ void ResourceListModel::updateLookupTable()
}
#include "moc_ResourceListModel.cxx"
/* vim: set tw=0 noexpandtab: */