From 512c42e38aec8b424135c967b8f12a92621ba702 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 21 Aug 2014 14:20:03 -0500 Subject: Dive list: seriously simplify the "add to trip" code The existing code seemed way complicated, made way too many assumptions and apparently was broken in certain cases. This code seems very simple, looks correct and should fail gracefully (i.e. simply do nothing) if things get confused. Fixes #706 (I hope) Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 40 ++++++++++++++-------------------------- qt-ui/divelistview.h | 2 +- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 8d771b5df..117d02247 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -572,43 +572,31 @@ void DiveListView::newTripAbove() void DiveListView::addToTripBelow() { - addToTrip(true); + addToTrip(1); } void DiveListView::addToTripAbove() { - addToTrip(false); + addToTrip(-1); } -void DiveListView::addToTrip(bool below) +void DiveListView::addToTrip(int delta) { - int delta = (currentOrder == Qt::AscendingOrder) ? -1 : +1; + // if there is a trip above / below, then it's a sibling at the same + // level as this dive. So let's take a look struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); - rememberSelection(); + QModelIndex t = contextMenuIndex.sibling(contextMenuIndex.row() + delta, 0); + dive_trip_t *trip = (dive_trip_t *)t.data(DiveTripModel::TRIP_ROLE).value(); - //TODO: This part should be moved to C-code. - int idx; - dive_trip_t *trip = NULL; - struct dive *pd = NULL; - if (below) // Should we add to the trip below instead? - delta *= -1; - - if (d->selected) { // we are right-clicking on one of possibly many selected dive(s) - // find the top selected dive, depending on the list order - if (delta == 1) - d = last_selected_dive(); - else - d = first_selected_dive(); - } - // now find the trip "above" in the dive list - if ((pd = get_dive(get_divenr(d) + delta)) != NULL) { - trip = pd->divetrip; - } - if (!pd || !trip) - // previous dive wasn't in a trip, so something is wrong + if (!trip || !d) + // no dive, no trip? get me out of here return; + + rememberSelection(); + add_dive_to_trip(d, trip); if (d->selected) { // there are possibly other selected dives that we should add + int idx; for_each_dive (idx, d) { if (d->selected) add_dive_to_trip(d, trip); @@ -616,7 +604,7 @@ void DiveListView::addToTrip(bool below) } trip->expanded = 1; mark_divelist_changed(true); - //This part stays at C++ code. + reload(currentLayout, false); restoreSelection(); fixMessyQtModelBehaviour(); diff --git a/qt-ui/divelistview.h b/qt-ui/divelistview.h index 7ab409b42..332c1795f 100644 --- a/qt-ui/divelistview.h +++ b/qt-ui/divelistview.h @@ -79,7 +79,7 @@ private: void updateLastUsedImageDir(const QString &s); void updateLastImageTimeOffset(int offset); int lastImageTimeOffset(); - void addToTrip(bool); + void addToTrip(int delta); }; #endif // DIVELISTVIEW_H -- cgit v1.2.3-70-g09d2