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

@@ -213,7 +213,7 @@ public:
void reload();
// returns true if all given keywords match name, tags etc.
bool keywordMatch( const QStringList & _keywords );
bool keywordMatch( const QStringList & _keywords ) const;
// return true, if given ResourceItem is equal
bool operator==( const ResourceItem & _other ) const;

View File

@@ -1,5 +1,5 @@
/*
* ResourceListModel.h - a tree model implementation for resources
* ResourceListModel.h - a list model implementation for resources
*
* Copyright (c) 2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
@@ -32,14 +32,13 @@
class ResourceListModel : public ResourceModel
{
Q_OBJECT
public:
ResourceListModel( ResourceDB * _db, QObject * _parent = NULL );
virtual ~ResourceListModel()
{
}
int rowCount( const QModelIndex & _parent = QModelIndex() ) const;
virtual int rowCount( const QModelIndex & _parent = QModelIndex() ) const;
virtual QModelIndex index( int _row, int _col,
const QModelIndex & _parent = QModelIndex() ) const;
@@ -49,15 +48,10 @@ public:
return QModelIndex();
}
virtual void setFilter( const QString & _s );
private slots:
void updateLookupTable();
virtual void updateFilters();
private:
QStringList m_filterKeywords;
QVector<ResourceItem *> m_lookupTable;
} ;

View File

@@ -27,6 +27,7 @@
#include <QtCore/QAbstractItemModel>
#include "lmms_basics.h"
#include "ResourceDB.h"
@@ -70,12 +71,32 @@ public:
int totalItems() const;
int shownItems() const;
QStringList keywordFilter() const
{
return m_keywordFilter;
}
ResourceItem::Type typeFilter() const
{
return m_typeFilter;
}
public slots:
virtual void setFilter( const QString & _s ) = 0;
virtual void updateFilters() = 0;
void setKeywordFilter( const QString & _keywords );
void setTypeFilter( ResourceItem::Type _type = ResourceItem::TypeUnknown );
protected:
inline bool itemMatchesFilter( const ResourceItem & _item ) const
{
return ( likely( typeFilter() == ResourceItem::TypeUnknown ) ||
typeFilter() == _item.type() ) &&
( likely( m_keywordFilterSet == false ) ||
_item.keywordMatch( keywordFilter() ) );
}
ResourceDB * db() const
{
return m_db;
@@ -84,6 +105,9 @@ protected:
private:
ResourceDB * m_db;
QStringList m_keywordFilter;
bool m_keywordFilterSet;
ResourceItem::Type m_typeFilter;
signals:

View File

@@ -36,20 +36,19 @@ public:
{
}
int rowCount( const QModelIndex & _parent = QModelIndex() ) const;
virtual int rowCount( const QModelIndex & _parent = QModelIndex() ) const;
virtual QModelIndex index( int _row, int _col,
const QModelIndex & _parent = QModelIndex() ) const;
virtual QModelIndex parent( const QModelIndex & _index ) const;
virtual void setFilter( const QString & _s );
virtual void updateFilters();
private:
bool filterItems( ResourceItem::Relation * _item,
const QModelIndex & _parent,
const QStringList & _keywords );
const QModelIndex & _parent );
void setHidden( ResourceItem::Relation * _item,
const QModelIndex & _parent,
bool _hidden,