diff options
Diffstat (limited to 'qt-models')
-rw-r--r-- | qt-models/divetripmodel.cpp | 17 | ||||
-rw-r--r-- | qt-models/divetripmodel.h | 10 |
2 files changed, 25 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); diff --git a/qt-models/divetripmodel.h b/qt-models/divetripmodel.h index c8050a6da..fb3c3395d 100644 --- a/qt-models/divetripmodel.h +++ b/qt-models/divetripmodel.h @@ -138,6 +138,14 @@ private: // quite inconvenient to access. // 2) If "trip" is null, this is a dive and dives is supposed to contain exactly // one element, which is the corresponding dive. + // + // Top-level items are ordered by timestamp. For dives, the core function + // dive_less_than is used, which guarantees a stable ordering even in the + // case of equal timestamps. For dives and trips, place dives before trips + // in the case of an equal timestamp. For trips with equal timestamps, the + // order is currently undefined. This is currently not a problem, because + // the core doesn't have a list of sorted trips. But nevertheless something + // to keep in mind. struct Item { dive_trip *trip; std::vector<dive *> dives; // std::vector<> instead of QVector for insert() with three iterators @@ -148,6 +156,8 @@ private: dive *getDive() const; // Helper function: returns top-level-dive or null timestamp_t when() const; // Helper function: start time of dive *or* trip }; + // Comparison function between dive and arbitrary entry + static bool dive_before_entry(const dive *d, const Item &entry); // Access trips and dives int findTripIdx(const dive_trip *trip) const; |