summaryrefslogtreecommitdiffstats
path: root/qt-models/divetripmodel.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-08-26 14:42:38 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-10-11 16:22:27 -0700
commitf836b9ae97bc8109e1acdd1859bd41a5dc53f7f3 (patch)
treed7e0ce6ca0265ebf01103273384a4594f99e0838 /qt-models/divetripmodel.cpp
parent0cca36377b01fc10063ecc67f5471ce80d133991 (diff)
downloadsubsurface-f836b9ae97bc8109e1acdd1859bd41a5dc53f7f3.tar.gz
Dive list: unify sorting in core and Qt-model
Ultimately, we want to use a single dive-list and not replicate it in the Qt-model code. To this goal, let's start with using the same sort function. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models/divetripmodel.cpp')
-rw-r--r--qt-models/divetripmodel.cpp17
1 files changed, 15 insertions, 2 deletions
diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp
index b6c69af0f..94f4b6b51 100644
--- a/qt-models/divetripmodel.cpp
+++ b/qt-models/divetripmodel.cpp
@@ -926,7 +926,7 @@ void DiveTripModel::addDivesToTrip(int trip, const QVector<dive *> &dives)
// Either this is outside of a trip or we're in list mode.
// Thus, add dives at the top-level in batches
addInBatches(items[trip].dives, dives,
- [](dive *d, dive *d2) { return d->when >= d2->when; }, // comp
+ [](dive *d, dive *d2) { return !dive_less_than(d, d2); }, // comp
[&](std::vector<dive *> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter
beginInsertRows(parent, idx, idx + to - from - 1);
items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to);
@@ -934,6 +934,19 @@ void DiveTripModel::addDivesToTrip(int trip, const QVector<dive *> &dives)
});
}
+// This function is used to compare a dive to an arbitrary entry (dive or trip).
+// For comparing two dives, use the core function dive_less_than_entry, which
+// effectively sorts by timestamp.
+// If comparing to a trip, the policy for equal-times is to place the dives
+// before the trip in the case of equal timestamps.
+bool DiveTripModel::dive_before_entry(const dive *d, const Item &entry)
+{
+ // Dives at the same time come before trips, therefore use the "<=" operator.
+ if (entry.trip)
+ return d->when <= entry.trip->when;
+ return !dive_less_than(d, entry.getDive());
+}
+
void DiveTripModel::divesAdded(dive_trip *trip, bool addTrip, const QVector<dive *> &divesIn)
{
// The dives come from the backend sorted by start-time. But our model is sorted
@@ -946,7 +959,7 @@ void DiveTripModel::divesAdded(dive_trip *trip, bool addTrip, const QVector<dive
// Either this is outside of a trip or we're in list mode.
// Thus, add dives at the top-level in batches
addInBatches(items, dives,
- [](dive *d, const Item &entry) { return d->when >= entry.when(); }, // comp
+ &dive_before_entry, // comp
[&](std::vector<Item> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter
beginInsertRows(QModelIndex(), idx, idx + to - from - 1);
items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to);