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,34 +367,10 @@ MultiFilterSortModel::MultiFilterSortModel(QObject *parent) : QSortFilterProxyMo
{ {
} }
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const bool MultiFilterSortModel::showDive(const struct dive *d) const
{ {
bool shouldShow = true;
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 (curr_dive_site) { if (curr_dive_site) {
struct dive_site *ds = NULL; dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
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++) {
QModelIndex child = sourceModel()->index(i, 0, index0);
d = (struct dive *)sourceModel()->data(child, DiveTripModel::DIVE_ROLE).value<void *>();
ds = get_dive_site_by_uuid(d->dive_site_uuid);
if (!ds)
continue;
if (same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid) {
if (ds->uuid != curr_dive_site->uuid) {
qWarning() << "Warning, two different dive sites with same name have a different id"
<< ds->uuid << "and" << curr_dive_site->uuid;
}
showTrip = true; // do not shortcircuit the loop or the counts will be wrong
}
}
return showTrip;
}
ds = get_dive_site_by_uuid(d->dive_site_uuid);
if (!ds) if (!ds)
return false; return false;
return same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid; return same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid;
@ -403,21 +379,56 @@ bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &s
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. for (const FilterModelBase *model: models) {
if (!model->doFilter(d))
return false;
}
return true;
}
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) 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 *>();
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) {
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)) QModelIndex child = sourceModel()->index(i, 0, index0);
d = (struct dive *)sourceModel()->data(child, DiveTripModel::DIVE_ROLE).value<void *>();
dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
if (!ds)
continue;
if (same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid) {
if (ds->uuid != curr_dive_site->uuid) {
qWarning() << "Warning, two different dive sites with same name have a different id"
<< ds->uuid << "and" << curr_dive_site->uuid;
}
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); if (justCleared || models.isEmpty())
return shouldShow; return true;
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;
} }
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: