From dd2466f51899aae406dc8c13904787710f30ec1c Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 25 Nov 2017 16:15:57 +0100 Subject: Update filters on refreshDisplay and remember old selecttions Update the filters if the list of dives is updated by calling MultiFilterSortModel::instance()->myInvalidate(); This had the side effect of clearing all selections. Thus, in the repopulate() methods of the FilterModels, check those entries that were checked previously. Since all the filter models use the same code, introduce a base class FilterModelBase. Signed-off-by: Berthold Stoeger --- qt-models/filtermodels.cpp | 61 +++++++++++++++++++++++++++++++--------------- qt-models/filtermodels.h | 14 ++++++++--- 2 files changed, 51 insertions(+), 24 deletions(-) (limited to 'qt-models') diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp index 684999fbd..217d456bf 100644 --- a/qt-models/filtermodels.cpp +++ b/qt-models/filtermodels.cpp @@ -78,7 +78,40 @@ CREATE_COMMON_METHODS_FOR_FILTER(SuitsFilterModel, count_dives_with_suit) CREATE_INSTANCE_METHOD(MultiFilterSortModel) -SuitsFilterModel::SuitsFilterModel(QObject *parent) : QStringListModel(parent) +FilterModelBase::FilterModelBase(QObject *parent) : QStringListModel(parent) +{ +} + +// Update the stringList and the checkState array. +// The last item is supposed to be the "Show Empty Tags" entry. +void FilterModelBase::updateList(const QStringList &newList) +{ + // Keep copy of old checkState array to reimport them later. + std::vector oldCheckState = checkState; + checkState.resize(newList.count()); + std::fill(checkState.begin(), checkState.end(), false); + anyChecked = false; + + // Ignore last item, since this is the "Show Empty Tags" entry. + for (int i = 0; i < rowCount() - 1; i++) { + if (oldCheckState[i]) { + int ind = newList.indexOf(stringList()[i]); + if (ind >= 0 && ind < newList.count() - 1) { + checkState[ind] = true; + anyChecked = true; + } + } + } + + // On program startup, the old list is empty. + if (rowCount() > 0 && oldCheckState.back()) { + checkState.back() = true; + anyChecked = true; + } + setStringList(newList); +} + +SuitsFilterModel::SuitsFilterModel(QObject *parent) : FilterModelBase(parent) { } @@ -127,13 +160,10 @@ void SuitsFilterModel::repopulate() } qSort(list); list << tr("No suit set"); - setStringList(list); - checkState.resize(list.count()); - std::fill(checkState.begin(), checkState.end(), false); - anyChecked = false; + updateList(list); } -TagFilterModel::TagFilterModel(QObject *parent) : QStringListModel(parent) +TagFilterModel::TagFilterModel(QObject *parent) : FilterModelBase(parent) { } @@ -150,10 +180,7 @@ void TagFilterModel::repopulate() } qSort(list); list << tr("Empty tags"); - setStringList(list); - checkState.resize(list.count()); - std::fill(checkState.begin(), checkState.end(), false); - anyChecked = false; + updateList(list); } bool TagFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel *sourceModel) const @@ -190,7 +217,7 @@ bool TagFilterModel::doFilter(dive *d, QModelIndex &index0, QAbstractItemModel * return false; } -BuddyFilterModel::BuddyFilterModel(QObject *parent) : QStringListModel(parent) +BuddyFilterModel::BuddyFilterModel(QObject *parent) : FilterModelBase(parent) { } @@ -243,13 +270,10 @@ void BuddyFilterModel::repopulate() } qSort(list); list << tr("No buddies"); - setStringList(list); - checkState.resize(list.count()); - std::fill(checkState.begin(), checkState.end(), false); - anyChecked = false; + updateList(list); } -LocationFilterModel::LocationFilterModel(QObject *parent) : QStringListModel(parent) +LocationFilterModel::LocationFilterModel(QObject *parent) : FilterModelBase(parent) { } @@ -294,10 +318,7 @@ void LocationFilterModel::repopulate() } qSort(list); list << tr("No location set"); - setStringList(list); - checkState.resize(list.count()); - std::fill(checkState.begin(), checkState.end(), false); - anyChecked = false; + updateList(list); } MultiFilterSortModel::MultiFilterSortModel(QObject *parent) : diff --git a/qt-models/filtermodels.h b/qt-models/filtermodels.h index cda6e2369..3060f7cf3 100644 --- a/qt-models/filtermodels.h +++ b/qt-models/filtermodels.h @@ -16,7 +16,13 @@ public: bool anyChecked; }; -class TagFilterModel : public QStringListModel, public MultiFilterInterface { +class FilterModelBase : public QStringListModel, public MultiFilterInterface { +protected: + explicit FilterModelBase(QObject *parent = 0); + void updateList(const QStringList &new_list); +}; + +class TagFilterModel : public FilterModelBase { Q_OBJECT public: static TagFilterModel *instance(); @@ -33,7 +39,7 @@ private: explicit TagFilterModel(QObject *parent = 0); }; -class BuddyFilterModel : public QStringListModel, public MultiFilterInterface { +class BuddyFilterModel : public FilterModelBase { Q_OBJECT public: static BuddyFilterModel *instance(); @@ -50,7 +56,7 @@ private: explicit BuddyFilterModel(QObject *parent = 0); }; -class LocationFilterModel : public QStringListModel, public MultiFilterInterface { +class LocationFilterModel : public FilterModelBase { Q_OBJECT public: static LocationFilterModel *instance(); @@ -67,7 +73,7 @@ private: explicit LocationFilterModel(QObject *parent = 0); }; -class SuitsFilterModel : public QStringListModel, public MultiFilterInterface { +class SuitsFilterModel : public FilterModelBase { Q_OBJECT public: static SuitsFilterModel *instance(); -- cgit v1.2.3-70-g09d2