From 911edfca712a046944de6d033cc4b8dd50cedfc3 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 13 Nov 2018 09:26:53 +0100 Subject: Dive list: update position of trip if data changed If the date of a dive changed, it might be necessary to reorder the trips, as the date of the trip changed. Although this seems like an odd usecase, move the trip if necessary, for consistency's sake. Signed-off-by: Berthold Stoeger --- qt-models/divetripmodel.cpp | 45 ++++++++++++++++++++++++++++++++++++++------- qt-models/divetripmodel.h | 1 + 2 files changed, 39 insertions(+), 7 deletions(-) (limited to 'qt-models') diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index c83a14e7b..c2d9e62a9 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -688,6 +688,36 @@ int DiveTripModel::findInsertionIndex(const dive_trip *trip) const return items.size(); } +// After a top-level item changed (notably a trip), it might +// need to be reordered. Move the item and send a "data-changed" signal. +void DiveTripModel::topLevelChanged(int idx) +{ + if (idx < 0 || idx >= (int)items.size()) + return; + + // First, try to move backwards + int newIdx = idx; + while (newIdx > 0 && dive_or_trip_less_than(items[idx].d_or_t, items[newIdx - 1].d_or_t)) + --newIdx; + + // If that didn't change, try to move forward + if (newIdx == idx) { + while (newIdx <= (int)items.size() && !dive_or_trip_less_than(items[idx].d_or_t, items[newIdx + 1].d_or_t)) + ++newIdx; + } + + // If index changed, move items + if (newIdx != idx) { + beginMoveRows(QModelIndex(), idx, idx, QModelIndex(), newIdx); + moveInVector(items, idx, idx + 1, newIdx); + endMoveRows(); + } + + // Finally, inform UI of changed trip header + QModelIndex tripIdx = createIndex(newIdx, 0, noParent); + dataChanged(tripIdx, tripIdx); +} + // Add items from vector "v2" to vector "v1" in batches of contiguous objects. // The items are inserted at places according to a sort order determined by "comp". // "v1" and "v2" are supposed to be ordered accordingly. @@ -739,6 +769,9 @@ void DiveTripModel::addDivesToTrip(int trip, const QVector &dives) items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to); endInsertRows(); }); + + // If necessary, move the trip + topLevelChanged(trip); } // This function is used to compare a dive to an arbitrary entry (dive or trip). @@ -783,10 +816,6 @@ void DiveTripModel::divesAdded(dive_trip *trip, bool addTrip, const QVector &dives) dataChanged(createIndex(from, 0, idx), createIndex(to - 1, COLUMNS - 1, idx)); return 0; // No items added or deleted }); + + // If necessary, move the trip + topLevelChanged(idx); } } diff --git a/qt-models/divetripmodel.h b/qt-models/divetripmodel.h index 6d34d30bb..aab154069 100644 --- a/qt-models/divetripmodel.h +++ b/qt-models/divetripmodel.h @@ -115,6 +115,7 @@ private: // Addition and deletion of dives void addDivesToTrip(int idx, const QVector &dives); + void topLevelChanged(int idx); dive *diveOrNull(const QModelIndex &index) const; // Returns a dive if this index represents a dive, null otherwise dive_or_trip tripOrDive(const QModelIndex &index) const; -- cgit v1.2.3-70-g09d2