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

@ -68,11 +68,9 @@ dive *DiveListBase::addDive(DiveToAdd &d)
}
dive *res = d.dive.release(); // Give up ownership of dive
// Set the filter flag according to current filter settings
bool show = DiveFilter::instance()->showDive(res);
res->hidden_by_filter = !show;
if (show)
++shown_dives;
// When we add dives, we start in hidden-by-filter status. Once all
// dives have been added, their status will be updated.
res->hidden_by_filter = true;
int idx = dive_table_get_insertion_index(&dive_table, res);
add_to_dive_table(&dive_table, idx, res); // Return ownership to backend
@ -185,19 +183,21 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
[](const DiveToAdd &d, const DiveToAdd &d2)
{ return dive_less_than(d.dive.get(), d2.dive.get()); });
// Remember old number of shown dives
int oldShown = shown_dives;
// Now, add the dives
// Note: the idiomatic STL-way would be std::transform, but let's use a loop since
// that is closer to classical C-style.
auto it2 = res.rbegin();
QVector<dive *> divesToFilter;
divesToFilter.reserve(toAdd.dives.size());
for (auto it = toAdd.dives.rbegin(); it != toAdd.dives.rend(); ++it, ++it2) {
*it2 = addDive(*it);
dives.push_back({ (*it2)->divetrip, *it2 });
divesToFilter.push_back(*it2);
}
toAdd.dives.clear();
ShownChange change = DiveFilter::instance()->update(divesToFilter);
// If the dives belong to new trips, add these as well.
// Remember the pointers so that we can later check if a trip was newly added
std::vector<dive_trip *> addedTrips;
@ -224,7 +224,7 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
emit diveListNotifier.divesAdded(trip, createTrip, divesInTrip);
});
if (oldShown != shown_dives)
if (!change.newShown.empty() || !change.newHidden.empty())
emit diveListNotifier.numShownChanged();
return { res, sites };