filter: internalize shown_dives in DiveFilter class

one piece of global state removed!

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-10-30 22:10:31 +01:00 committed by Dirk Hohndel
parent 51d0c42a5c
commit c53bab8965
4 changed files with 14 additions and 9 deletions

View file

@ -130,7 +130,7 @@ DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSite
sitesToAdd.reserve(divesAndSitesToDelete.sites.size()); sitesToAdd.reserve(divesAndSitesToDelete.sites.size());
// Remember old number of shown dives // Remember old number of shown dives
int oldShown = shown_dives; int oldShown = DiveFilter::instance()->shownDives();
// Make sure that the dive list is sorted. The added dives will be sent in a signal // Make sure that the dive list is sorted. The added dives will be sent in a signal
// and the recipients assume that the dives are sorted the same way as they are // and the recipients assume that the dives are sorted the same way as they are
@ -164,7 +164,7 @@ DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSite
emit diveListNotifier.divesDeleted(trip, deleteTrip, divesInTrip); emit diveListNotifier.divesDeleted(trip, deleteTrip, divesInTrip);
}); });
if (oldShown != shown_dives) if (oldShown != DiveFilter::instance()->shownDives())
emit diveListNotifier.numShownChanged(); emit diveListNotifier.numShownChanged();
return { std::move(divesToAdd), std::move(tripsToAdd), std::move(sitesToAdd) }; return { std::move(divesToAdd), std::move(tripsToAdd), std::move(sitesToAdd) };

View file

@ -13,10 +13,8 @@
#include "qt-models/filtermodels.h" #include "qt-models/filtermodels.h"
#endif #endif
int shown_dives = 0;
// Set filter status of dive and return whether it has been changed // Set filter status of dive and return whether it has been changed
static bool setFilterStatus(struct dive *d, bool shown) bool DiveFilter::setFilterStatus(struct dive *d, bool shown) const
{ {
bool old_shown, changed; bool old_shown, changed;
if (!d) if (!d)
@ -31,7 +29,7 @@ static bool setFilterStatus(struct dive *d, bool shown)
return changed; return changed;
} }
static void updateDiveStatus(dive *d, bool newStatus, ShownChange &change) void DiveFilter::updateDiveStatus(dive *d, bool newStatus, ShownChange &change) const
{ {
if (setFilterStatus(d, newStatus)) { if (setFilterStatus(d, newStatus)) {
if (newStatus) if (newStatus)
@ -201,6 +199,11 @@ QString DiveFilter::shownText() const
return gettextFromC::tr("%L1 dives").arg(dive_table.nr); return gettextFromC::tr("%L1 dives").arg(dive_table.nr);
} }
int DiveFilter::shownDives() const
{
return shown_dives;
}
void DiveFilter::setFilter(const FilterData &data) void DiveFilter::setFilter(const FilterData &data)
{ {
filterData = data; filterData = data;

View file

@ -13,8 +13,6 @@ struct dive;
struct dive_trip; struct dive_trip;
struct dive_site; struct dive_site;
extern int shown_dives;
// Structure describing changes of shown status upon applying the filter // Structure describing changes of shown status upon applying the filter
struct ShownChange { struct ShownChange {
QVector<dive *> newShown; QVector<dive *> newShown;
@ -43,6 +41,7 @@ public:
void reset(); void reset();
QString shownText() const; QString shownText() const;
int shownDives() const;
bool diveSiteMode() const; // returns true if we're filtering on dive site (on mobile always returns false) bool diveSiteMode() const; // returns true if we're filtering on dive site (on mobile always returns false)
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE
const QVector<dive_site *> &filteredDiveSites() const; const QVector<dive_site *> &filteredDiveSites() const;
@ -57,9 +56,12 @@ public:
private: private:
DiveFilter(); DiveFilter();
bool showDive(const struct dive *d) const; // Should that dive be shown? bool showDive(const struct dive *d) const; // Should that dive be shown?
bool setFilterStatus(struct dive *d, bool shown) const;
void updateDiveStatus(dive *d, bool newStatus, ShownChange &change) const;
QVector<dive_site *> dive_sites; QVector<dive_site *> dive_sites;
FilterData filterData; FilterData filterData;
mutable int shown_dives;
// We use ref-counting for the dive site mode. The reason is that when switching // We use ref-counting for the dive site mode. The reason is that when switching
// between two tabs that both need dive site mode, the following course of // between two tabs that both need dive site mode, the following course of

View file

@ -58,7 +58,7 @@ QHash<int, QByteArray> MobileListModelBase::roleNames() const
int MobileListModel::shown() const int MobileListModel::shown() const
{ {
return shown_dives; return DiveFilter::instance()->shownDives();
} }
int MobileListModelBase::columnCount(const QModelIndex &parent) const int MobileListModelBase::columnCount(const QModelIndex &parent) const