Add possibility to filter by more than one criteria at a time

This new version of the TagFilterSortModel actually accepts
*any* new MultiFilterInterface.

So, how to use it to create a new filter:

Implement a class that inherits from MultiFilterInterface

Implement the filterRow method

TagFilterSortModel::instance->add( myClass );

and you are done.

[Dirk Hohndel: removed some debug code and did whitespace cleanup]

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-10-01 16:23:02 -03:00 committed by Dirk Hohndel
parent 28800622f0
commit e0b60167f2
5 changed files with 86 additions and 19 deletions

View file

@ -34,7 +34,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
setItemDelegate(new DiveListDelegate(this));
setUniformRowHeights(true);
setItemDelegateForColumn(DiveTripModel::RATING, new StarWidgetsDelegate(this));
TagFilterSortModel *model = new TagFilterSortModel(this);
TagFilterSortModel *model = TagFilterSortModel::instance();
model->setSortRole(DiveTripModel::SORT_ROLE);
model->setFilterKeyColumn(-1); // filter all columns
model->setFilterCaseSensitivity(Qt::CaseInsensitive);

View file

@ -2193,25 +2193,20 @@ bool TagFilterModel::setData(const QModelIndex &index, const QVariant &value, in
return false;
}
TagFilterSortModel::TagFilterSortModel(QObject *parent) : QSortFilterProxyModel(parent)
{
connect(TagFilterModel::instance(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(invalidate()));
}
bool TagFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
bool TagFilterModel::filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const
{
// If there's nothing checked, this should show everythin.
if (!TagFilterModel::instance()->anyChecked) {
if (!anyChecked) {
return true;
}
QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent);
QVariant diveVariant = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE);
QModelIndex index0 = sourceModel->index(source_row, 0, source_parent);
QVariant diveVariant = sourceModel->data(index0, DiveTripModel::DIVE_ROLE);
struct dive *d = (struct dive *)diveVariant.value<void *>();
if (!d) { // It's a trip, only show the ones that have dives to be shown.
for (int i = 0; i < sourceModel()->rowCount(index0); i++) {
if (filterAcceptsRow(i, index0))
for (int i = 0; i < sourceModel->rowCount(index0); i++) {
if (filterRow(i, index0, sourceModel))
return true;
}
return false;
@ -2220,23 +2215,69 @@ bool TagFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &sou
struct tag_entry *head = d->tag_list;
if (!head) { // last tag means "Show empty tags";
if (TagFilterModel::instance()->rowCount() > 0)
return TagFilterModel::instance()->checkState[TagFilterModel::instance()->rowCount() - 1];
if (rowCount() > 0)
return checkState[rowCount() - 1];
else
return true;
}
// have at least one tag.
QStringList tagList = TagFilterModel::instance()->stringList();
QStringList tagList = stringList();
if (!tagList.isEmpty()) {
tagList.removeLast(); // remove the "Show Empty Tags";
while (head) {
QString tagName(head->tag->name);
int index = tagList.indexOf(tagName);
if (TagFilterModel::instance()->checkState[index])
if (checkState[index])
return true;
head = head->next;
}
}
return false;
}
TagFilterSortModel *TagFilterSortModel::instance()
{
static TagFilterSortModel *self = new TagFilterSortModel();
return self;
}
TagFilterSortModel::TagFilterSortModel(QObject *parent) : QSortFilterProxyModel(parent)
{
}
bool TagFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if (models.isEmpty()) {
return true;
}
Q_FOREACH (MultiFilterInterface *model, models) {
if (model->filterRow(source_row, source_parent, sourceModel())) {
return true;
}
}
return false;
}
void TagFilterSortModel::myInvalidate()
{
invalidate();
}
void TagFilterSortModel::addFilterModel(MultiFilterInterface *model)
{
QAbstractItemModel *itemModel = dynamic_cast<QAbstractItemModel *>(model);
Q_ASSERT(itemModel);
models.append(model);
connect(itemModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(myInvalidate()));
}
void TagFilterSortModel::removeFilterModel(MultiFilterInterface *model)
{
QAbstractItemModel *itemModel = dynamic_cast<QAbstractItemModel *>(model);
Q_ASSERT(itemModel);
models.removeAll(model);
disconnect(itemModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(myInvalidate()));
}

View file

@ -417,13 +417,19 @@ private:
QStringList languages;
};
class TagFilterModel : public QStringListModel {
class MultiFilterInterface {
public:
virtual bool filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const = 0;
};
class TagFilterModel : public QStringListModel, public MultiFilterInterface{
Q_OBJECT
public:
static TagFilterModel *instance();
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
virtual bool filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const;
bool *checkState;
bool anyChecked;
public
@ -437,7 +443,14 @@ private:
class TagFilterSortModel : public QSortFilterProxyModel {
Q_OBJECT
public:
TagFilterSortModel(QObject *parent = 0);
static TagFilterSortModel *instance();
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
void addFilterModel(MultiFilterInterface *model);
void removeFilterModel(MultiFilterInterface *model);
public slots:
void myInvalidate();
private:
TagFilterSortModel(QObject *parent = 0);
QList<MultiFilterInterface*> models;
};
#endif // MODELS_H

View file

@ -464,3 +464,15 @@ TagFilter::TagFilter(QWidget *parent) : QWidget(parent)
connect(ui.filterTag, SIGNAL(textChanged(QString)), filter, SLOT(setFilterFixedString(QString)));
ui.tagView->setModel(filter);
}
void TagFilter::showEvent(QShowEvent *event)
{
TagFilterSortModel::instance()->addFilterModel(TagFilterModel::instance());
QWidget::showEvent(event);
}
void TagFilter::hideEvent(QHideEvent *event)
{
TagFilterSortModel::instance()->removeFilterModel(TagFilterModel::instance());
QWidget::hideEvent(event);
}

View file

@ -134,7 +134,8 @@ class TagFilter : public QWidget {
Q_OBJECT
public:
TagFilter(QWidget *parent = 0);
virtual void showEvent(QShowEvent *);
virtual void hideEvent(QHideEvent *);
private:
Ui::TagFilter ui;
};