Filter: move actual filtering loop to core/divefilter.cpp

The DiveFilter class defined the showDive() function to test
whether a dive should be filtered or not. This was used in
DiveTripModel to loop over all dives or all dives affected by
an editing action.

This restricts us in how we do filtering: We can't use indexes
that give us directly the result. To make the filtering more
flexible, move the actual loops that do the filtering to
the DiveFilter class.

The undo-commands likewise called directly the showDive()
function to check whether newly added dives are shown.
Use the new interface here as well.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-02-13 23:39:44 +01:00 committed by Dirk Hohndel
parent a45c5faa8c
commit ee553e059d
4 changed files with 63 additions and 45 deletions

View file

@ -432,41 +432,18 @@ bool DiveTripModelBase::setData(const QModelIndex &index, const QVariant &value,
return true;
}
// Structure describing changes of shown status
struct ShownChange {
QVector<dive *> newShown;
QVector<dive *> newHidden;
bool currentChanged;
void filterDive(dive *d, const DiveFilter *filter);
};
void ShownChange::filterDive(dive *d, const DiveFilter *filter)
{
bool newStatus = filter->showDive(d);
if (filter_dive(d, newStatus)) {
if (newStatus)
newShown.push_back(d);
else
newHidden.push_back(d);
}
}
// Update visibility status of dive and return dives whose visibility changed.
// Attention: the changed dives are removed from the original vector!
static ShownChange updateShown(QVector<dive *> &dives)
{
DiveFilter *filter = DiveFilter::instance();
dive *old_current = current_dive;
ShownChange res;
for (dive *d: dives)
res.filterDive(d, filter);
ShownChange res = filter->update(dives);
if (!res.newShown.empty() || !res.newHidden.empty())
emit diveListNotifier.numShownChanged();
for (dive *d: res.newHidden)
dives.removeAll(d);
for (dive *d: res.newShown)
dives.removeAll(d);
res.currentChanged = old_current != current_dive;
return res;
}
@ -474,13 +451,9 @@ static ShownChange updateShown(QVector<dive *> &dives)
static ShownChange updateShownAll()
{
DiveFilter *filter = DiveFilter::instance();
dive *old_current = current_dive;
ShownChange res;
for (int i = 0; i < dive_table.nr; ++i)
res.filterDive(get_dive(i), filter);
ShownChange res = filter->updateAll();
if (!res.newShown.empty() || !res.newHidden.empty())
emit diveListNotifier.numShownChanged();
res.currentChanged = old_current != current_dive;
return res;
}