mirror of
https://github.com/LMMS/lmms.git
synced 2026-03-11 02:26:19 -04:00
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:
@@ -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: */
|
||||
|
||||
Reference in New Issue
Block a user