Filter: break out showDive() function from filterAcceptsRow()

To make dive-filtering accessible from other parts of the code,
break out the actual dive-filtering code into a function that
takes a pointer-to-dive instead of QModelIndex.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-08-14 14:16:25 -04:00
parent 8a394b9db4
commit 13fbca3f55
2 changed files with 46 additions and 34 deletions

View file

@ -367,21 +367,46 @@ MultiFilterSortModel::MultiFilterSortModel(QObject *parent) : QSortFilterProxyMo
{ {
} }
bool MultiFilterSortModel::showDive(const struct dive *d) const
{
if (curr_dive_site) {
dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
if (!ds)
return false;
return same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid;
}
if (justCleared || models.isEmpty())
return true;
for (const FilterModelBase *model: models) {
if (!model->doFilter(d))
return false;
}
return true;
}
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{ {
bool shouldShow = true;
QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent); QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent);
QVariant diveVariant = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE); QVariant diveVariant = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE);
struct dive *d = (struct dive *)diveVariant.value<void *>(); struct dive *d = (struct dive *)diveVariant.value<void *>();
if (d) {
// Current row is a dive
bool show = showDive(d);
filter_dive(d, show);
return show;
}
// From here on, the current row is a trip
if (curr_dive_site) { if (curr_dive_site) {
struct dive_site *ds = NULL;
if (!d) { // It's a trip, only show the ones that have dives to be shown.
bool showTrip = false; bool showTrip = false;
for (int i = 0; i < sourceModel()->rowCount(index0); i++) { for (int i = 0; i < sourceModel()->rowCount(index0); i++) {
QModelIndex child = sourceModel()->index(i, 0, index0); QModelIndex child = sourceModel()->index(i, 0, index0);
d = (struct dive *)sourceModel()->data(child, DiveTripModel::DIVE_ROLE).value<void *>(); d = (struct dive *)sourceModel()->data(child, DiveTripModel::DIVE_ROLE).value<void *>();
ds = get_dive_site_by_uuid(d->dive_site_uuid); dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
if (!ds) if (!ds)
continue; continue;
if (same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid) { if (same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid) {
@ -394,30 +419,16 @@ bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &s
} }
return showTrip; return showTrip;
} }
ds = get_dive_site_by_uuid(d->dive_site_uuid);
if (!ds)
return false;
return same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid;
}
if (justCleared || models.isEmpty()) if (justCleared || models.isEmpty())
return true; return true;
if (!d) { // It's a trip, only show the ones that have dives to be shown.
bool showTrip = false; bool showTrip = false;
for (int i = 0; i < sourceModel()->rowCount(index0); i++) { for (int i = 0; i < sourceModel()->rowCount(index0); i++) {
if (filterAcceptsRow(i, index0)) if (filterAcceptsRow(i, index0))
showTrip = true; // do not shortcircuit the loop or the counts will be wrong showTrip = true; // do not shortcircuit the loop or the counts will be wrong
} }
return showTrip; return showTrip;
}
Q_FOREACH (FilterModelBase *model, models) {
if (!model->doFilter(d))
shouldShow = false;
}
filter_dive(d, shouldShow);
return shouldShow;
} }
void MultiFilterSortModel::myInvalidate() void MultiFilterSortModel::myInvalidate()

View file

@ -97,6 +97,7 @@ public:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
void addFilterModel(FilterModelBase *model); void addFilterModel(FilterModelBase *model);
void removeFilterModel(FilterModelBase *model); void removeFilterModel(FilterModelBase *model);
bool showDive(const struct dive *d) const;
int divesDisplayed; int divesDisplayed;
public public
slots: slots: