From c53bab8965a2d5332df55a442a999714b5f700dc Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 30 Oct 2020 22:10:31 +0100 Subject: [PATCH] filter: internalize shown_dives in DiveFilter class one piece of global state removed! Signed-off-by: Berthold Stoeger --- commands/command_divelist.cpp | 4 ++-- core/divefilter.cpp | 11 +++++++---- core/divefilter.h | 6 ++++-- qt-models/mobilelistmodel.cpp | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/commands/command_divelist.cpp b/commands/command_divelist.cpp index 36d4cdcf3..baf81c1ac 100644 --- a/commands/command_divelist.cpp +++ b/commands/command_divelist.cpp @@ -130,7 +130,7 @@ DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSite sitesToAdd.reserve(divesAndSitesToDelete.sites.size()); // 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 // 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); }); - if (oldShown != shown_dives) + if (oldShown != DiveFilter::instance()->shownDives()) emit diveListNotifier.numShownChanged(); return { std::move(divesToAdd), std::move(tripsToAdd), std::move(sitesToAdd) }; diff --git a/core/divefilter.cpp b/core/divefilter.cpp index 1af1a0b9d..ddb77e9fc 100644 --- a/core/divefilter.cpp +++ b/core/divefilter.cpp @@ -13,10 +13,8 @@ #include "qt-models/filtermodels.h" #endif -int shown_dives = 0; - // 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; if (!d) @@ -31,7 +29,7 @@ static bool setFilterStatus(struct dive *d, bool shown) 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 (newStatus) @@ -201,6 +199,11 @@ QString DiveFilter::shownText() const return gettextFromC::tr("%L1 dives").arg(dive_table.nr); } +int DiveFilter::shownDives() const +{ + return shown_dives; +} + void DiveFilter::setFilter(const FilterData &data) { filterData = data; diff --git a/core/divefilter.h b/core/divefilter.h index d4342871b..49503cca0 100644 --- a/core/divefilter.h +++ b/core/divefilter.h @@ -13,8 +13,6 @@ struct dive; struct dive_trip; struct dive_site; -extern int shown_dives; - // Structure describing changes of shown status upon applying the filter struct ShownChange { QVector newShown; @@ -43,6 +41,7 @@ public: void reset(); QString shownText() const; + int shownDives() const; bool diveSiteMode() const; // returns true if we're filtering on dive site (on mobile always returns false) #ifndef SUBSURFACE_MOBILE const QVector &filteredDiveSites() const; @@ -57,9 +56,12 @@ public: private: DiveFilter(); 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_sites; FilterData filterData; + mutable int shown_dives; // 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 diff --git a/qt-models/mobilelistmodel.cpp b/qt-models/mobilelistmodel.cpp index f660af2e6..49e8f4641 100644 --- a/qt-models/mobilelistmodel.cpp +++ b/qt-models/mobilelistmodel.cpp @@ -58,7 +58,7 @@ QHash MobileListModelBase::roleNames() const int MobileListModel::shown() const { - return shown_dives; + return DiveFilter::instance()->shownDives(); } int MobileListModelBase::columnCount(const QModelIndex &parent) const