Improve the filter logic

The MultiFilter itself should walk the dives in a trip. This way the logic
(OR within a category, but AND between categories) is correctly applied
and the flags in the dives are set correctly (which makes the overall
counts for dives filtered work correctly.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-11-16 18:22:35 +00:00
parent ad603b6e9f
commit 05e02b939a
2 changed files with 8 additions and 75 deletions

View file

@ -81,14 +81,6 @@ bool SuitsFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel
return true;
}
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 (filterRow(i, index0, sourceModel))
return true;
}
return false;
}
// Checked means 'Show', Unchecked means 'Hide'.
QString suit(d->suit);
// only show empty suit dives if the user checked that.
@ -112,15 +104,6 @@ bool SuitsFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel
return false;
}
bool SuitsFilterModel::filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const
{
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 *>();
return doFilter(d, index0, sourceModel);
}
void SuitsFilterModel::repopulate()
{
QStringList list;
@ -172,13 +155,6 @@ bool TagFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *
if (!anyChecked) {
return true;
}
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 (filterRow(i, index0, sourceModel))
return true;
}
return false;
}
// Checked means 'Show', Unchecked means 'Hide'.
struct tag_entry *head = d->tag_list;
@ -204,17 +180,6 @@ bool TagFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *
return false;
}
bool TagFilterModel::filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const
{
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 *>();
bool show = doFilter(d, index0, sourceModel);
filter_dive(d, show);
return show;
}
BuddyFilterModel::BuddyFilterModel(QObject *parent) : QStringListModel(parent)
{
}
@ -225,14 +190,6 @@ bool BuddyFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel
if (!anyChecked) {
return true;
}
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 (filterRow(i, index0, sourceModel))
return true;
}
return false;
}
// Checked means 'Show', Unchecked means 'Hide'.
QString diveBuddy(d->buddy);
QString divemaster(d->divemaster);
@ -257,15 +214,6 @@ bool BuddyFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel
return false;
}
bool BuddyFilterModel::filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const
{
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 *>();
return doFilter(d, index0, sourceModel);
}
void BuddyFilterModel::repopulate()
{
QStringList list;
@ -299,15 +247,6 @@ bool LocationFilterModel::doFilter(struct dive *d, QModelIndex &index0, QAbstrac
if (!anyChecked) {
return true;
}
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 (filterRow(i, index0, sourceModel))
return true;
}
return false;
}
// Checked means 'Show', Unchecked means 'Hide'.
QString location(d->location);
// only show empty location dives if the user checked that.
@ -331,15 +270,6 @@ bool LocationFilterModel::doFilter(struct dive *d, QModelIndex &index0, QAbstrac
return false;
}
bool LocationFilterModel::filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const
{
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 *>();
return doFilter(d, index0, sourceModel);
}
void LocationFilterModel::repopulate()
{
QStringList list;
@ -375,6 +305,14 @@ bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &s
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.
bool showTrip = false;
for (int i = 0; i < sourceModel()->rowCount(index0); i++) {
if (filterAcceptsRow(i, index0))
showTrip = true; // do not shortcircuit the loop or the counts will be wrong
}
return showTrip;
}
Q_FOREACH (MultiFilterInterface *model, models) {
if (!model->doFilter(d, index0, sourceModel()))
shouldShow = false;

View file

@ -7,7 +7,6 @@
class MultiFilterInterface {
public:
MultiFilterInterface() : checkState(NULL){};
virtual bool filterRow(int source_row, const QModelIndex &source_parent, QAbstractItemModel *sourceModel) const = 0;
virtual bool doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const = 0;
virtual void clearFilter() = 0;
bool *checkState;
@ -21,7 +20,6 @@ public:
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 doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public
@ -39,7 +37,6 @@ public:
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 doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public
@ -57,7 +54,6 @@ public:
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 doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public
@ -75,7 +71,6 @@ public:
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 doFilter(struct dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const;
void clearFilter();
public