summaryrefslogtreecommitdiffstats
path: root/qt-models/filtermodels.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2017-11-25 16:15:57 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2017-11-25 08:24:36 -0800
commitdd2466f51899aae406dc8c13904787710f30ec1c (patch)
tree45a06a318509b0e1c7957ba09b4ca6ce0030de83 /qt-models/filtermodels.cpp
parent4da6a0a73261a34a71e8f95261769128c3c1df0b (diff)
downloadsubsurface-dd2466f51899aae406dc8c13904787710f30ec1c.tar.gz
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 <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models/filtermodels.cpp')
-rw-r--r--qt-models/filtermodels.cpp61
1 files changed, 41 insertions, 20 deletions
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<char> 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) :