aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-11-19 21:40:03 +0100
committerGravatar bstoeger <32835590+bstoeger@users.noreply.github.com>2019-12-04 13:00:23 +0100
commit4ca65894c842a042b0d21a5449355be475f6fe7c (patch)
tree530b93502ab46a8db73445cc36f1d1032986637d
parent3cc6576913b4063b88f510f22a063aee76fa5870 (diff)
downloadsubsurface-4ca65894c842a042b0d21a5449355be475f6fe7c.tar.gz
Dive trip model: send changed signals if visibility changed in trips
To avoid having to do full filter reloads, send dive-changed signals for dives in trips when the shown-status changed. But only for trips where not all dives are hidden. Because for those, the plan is to hide the trip as a whole. Implement the signal sending in its own function so that it can be reused for top-level items and the list-view. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r--qt-models/divetripmodel.cpp38
-rw-r--r--qt-models/divetripmodel.h4
2 files changed, 38 insertions, 4 deletions
diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp
index 1d4a9a2a0..55ba37a5c 100644
--- a/qt-models/divetripmodel.cpp
+++ b/qt-models/divetripmodel.cpp
@@ -691,14 +691,43 @@ dive *DiveTripModelTree::diveOrNull(const QModelIndex &index) const
return tripOrDive(index).dive;
}
-static bool calculateFilterForTrip(const std::vector<dive *> &dives, const DiveFilter *filter)
+// Send data changed signals blockwise. Which entries are changed is collected
+// in the first parameter "changed".
+void DiveTripModelBase::sendShownChangedSignals(const std::vector<char> &changed, quintptr parentIndex)
+{
+ static const QVector<int> roles { SHOWN_ROLE };
+ for (size_t i = 0; i < changed.size(); ++i) {
+ // Find first and last block of changed items
+ if (!changed[i])
+ continue;
+ size_t j;
+ for (j = i + 1; j < changed.size() && changed[j]; ++j)
+ ; // Pass
+ dataChanged(createIndex(i, 0, parentIndex), createIndex(j - 1, 0, parentIndex), roles);
+ i = j - 1;
+ }
+}
+
+// Applying the filter to trip-items is a bit tricky:
+// We only want to send changed-signals if one of the dives remains visible.
+// Because if no dive remains visible, we'll simply send a signal on the parent trip,
+// which will then be hidden and all the dives will be hidden implicitly as well.
+// Thus, do this in two passes: collect changed dives and only if any dive is visible,
+// send the signals.
+bool DiveTripModelTree::calculateFilterForTrip(const std::vector<dive *> &dives, const DiveFilter *filter, int parentIndex)
{
bool showTrip = false;
+ std::vector<char> changed;
+ changed.reserve(dives.size());
for (dive *d: dives) {
bool shown = filter->showDive(d);
- filter_dive(d, shown);
+ changed.push_back(filter_dive(d, shown));
showTrip |= shown;
}
+
+ // If any dive is shown, send changed-signals
+ if (showTrip)
+ sendShownChangedSignals(changed, parentIndex);
return showTrip;
}
@@ -714,14 +743,15 @@ void DiveTripModelTree::recalculateFilter()
// as a consequence of the filterReset signal right after the local scope.
auto marker = diveListNotifier.enterCommand();
DiveFilter *filter = DiveFilter::instance();
- for (Item &item: items) {
+ for (size_t i = 0; i < items.size(); ++i) {
+ Item &item = items[i];
if (item.d_or_t.dive) {
dive *d = item.d_or_t.dive;
item.shown = filter->showDive(item.d_or_t.dive);
filter_dive(d, item.shown);
} else {
// Trips are shown if any of the dives is shown
- item.shown = calculateFilterForTrip(item.dives, filter);
+ item.shown = calculateFilterForTrip(item.dives, filter, i);
}
}
}
diff --git a/qt-models/divetripmodel.h b/qt-models/divetripmodel.h
index c7e488030..5952fa558 100644
--- a/qt-models/divetripmodel.h
+++ b/qt-models/divetripmodel.h
@@ -6,6 +6,8 @@
#include "core/subsurface-qt/DiveListNotifier.h"
#include <QAbstractItemModel>
+struct DiveFilter;
+
// There are two different representations of the dive list:
// 1) Tree view: two-level model where dives are grouped by trips
// 2) List view: one-level model where dives are sorted by one out
@@ -98,6 +100,7 @@ protected:
// Access trip and dive data
static QVariant diveData(const struct dive *d, int column, int role);
static QVariant tripData(const dive_trip *trip, int column, int role);
+ void sendShownChangedSignals(const std::vector<char> &changed, quintptr parentIndex);
virtual dive *diveOrNull(const QModelIndex &index) const = 0; // Returns a dive if this index represents a dive, null otherwise
virtual void clearData() = 0;
@@ -130,6 +133,7 @@ private:
void recalculateFilter();
void divesChangedTrip(dive_trip *trip, const QVector<dive *> &dives);
void divesTimeChangedTrip(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives);
+ bool calculateFilterForTrip(const std::vector<dive *> &dives, const DiveFilter *filter, int parentIndex);
// The tree model has two levels. At the top level, we have either trips or dives
// that do not belong to trips. Such a top-level item is represented by the "Item"