diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-02-13 23:39:44 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-02-22 15:18:31 -0800 |
commit | ee553e059d55f3376a7026a217e879f579b90e17 (patch) | |
tree | 6ee0db846caa3fb088a3cea85b03c746976c5b76 /core | |
parent | a45c5faa8c7aab7d4263eb98d85d5e7cd589ef73 (diff) | |
download | subsurface-ee553e059d55f3376a7026a217e879f579b90e17.tar.gz |
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>
Diffstat (limited to 'core')
-rw-r--r-- | core/divefilter.cpp | 39 | ||||
-rw-r--r-- | core/divefilter.h | 20 |
2 files changed, 52 insertions, 7 deletions
diff --git a/core/divefilter.cpp b/core/divefilter.cpp index 5bb05b947..7f47e45ac 100644 --- a/core/divefilter.cpp +++ b/core/divefilter.cpp @@ -8,10 +8,12 @@ DiveFilter::DiveFilter() { } -bool DiveFilter::showDive(const struct dive *d) const +ShownChange DiveFilter::update(const QVector<dive *> &) const +{ +} + +ShownChange DiveFilter::updateAll() const { - // TODO: Do something useful - return true; } #else // SUBSURFACE_MOBILE @@ -24,6 +26,37 @@ bool DiveFilter::showDive(const struct dive *d) const #include "core/divesite.h" #include "qt-models/filtermodels.h" +void DiveFilter::updateDiveStatus(dive *d, ShownChange &change) const +{ + bool newStatus = showDive(d); + if (filter_dive(d, newStatus)) { + if (newStatus) + change.newShown.push_back(d); + else + change.newHidden.push_back(d); + } +} + +ShownChange DiveFilter::update(const QVector<dive *> &dives) const +{ + dive *old_current = current_dive; + ShownChange res; + for (dive *d: dives) + updateDiveStatus(d, res); + res.currentChanged = old_current != current_dive; + return res; +} + +ShownChange DiveFilter::updateAll() const +{ + dive *old_current = current_dive; + ShownChange res; + for (int i = 0; i < dive_table.nr; ++i) + updateDiveStatus(get_dive(i), res); + res.currentChanged = old_current != current_dive; + return res; +} + namespace { // Pointer to function that takes two strings and returns whether // the first matches the second according to a criterion (substring, starts-with, exact). diff --git a/core/divefilter.h b/core/divefilter.h index 2ad3c4b9b..aa166d022 100644 --- a/core/divefilter.h +++ b/core/divefilter.h @@ -3,6 +3,16 @@ #ifndef DIVE_FILTER_H #define DIVE_FILTER_H +#include <QVector> +struct dive; + +// Structure describing changes of shown status upon applying the filter +struct ShownChange { + QVector<dive *> newShown; + QVector<dive *> newHidden; + bool currentChanged; +}; + // The dive filter for mobile is currently much simpler than for desktop. // Therefore, for now we have two completely separate implementations. // This should be unified in the future. @@ -12,7 +22,8 @@ class DiveFilter { public: static DiveFilter *instance(); - bool showDive(const struct dive *d) const; + ShownChange update(const QVector<dive *> &dives) const; // Update filter status of given dives and return dives whose status changed + ShownChange updateAll() const; // Update filter status of all dives and return dives whose status changed private: DiveFilter(); }; @@ -21,9 +32,7 @@ private: #include <QDateTime> #include <QStringList> -#include <QVector> -struct dive; struct dive_trip; struct dive_site; @@ -82,15 +91,18 @@ class DiveFilter { public: static DiveFilter *instance(); - bool showDive(const struct dive *d) const; bool diveSiteMode() const; // returns true if we're filtering on dive site const QVector<dive_site *> &filteredDiveSites() const; void startFilterDiveSites(QVector<dive_site *> ds); void setFilterDiveSite(QVector<dive_site *> ds); void stopFilterDiveSites(); void setFilter(const FilterData &data); + ShownChange update(const QVector<dive *> &dives) const; // Update filter status of given dives and return dives whose status changed + ShownChange updateAll() const; // Update filter status of all dives and return dives whose status changed private: DiveFilter(); + void updateDiveStatus(dive *d, ShownChange &change) const; + bool showDive(const struct dive *d) const; // Should that dive be shown? QVector<dive_site *> dive_sites; FilterData filterData; |