diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2014-08-21 14:20:03 -0500 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2014-08-21 14:20:03 -0500 |
commit | 512c42e38aec8b424135c967b8f12a92621ba702 (patch) | |
tree | 693d023d058b81d1f6a73ea6567ea313f66fb995 /qt-ui/divelistview.cpp | |
parent | b940f078b5039046118c9b6bb2b8998a2a6088cb (diff) | |
download | subsurface-512c42e38aec8b424135c967b8f12a92621ba702.tar.gz |
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 <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/divelistview.cpp')
-rw-r--r-- | qt-ui/divelistview.cpp | 40 |
1 files changed, 14 insertions, 26 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<void *>(); - rememberSelection(); + QModelIndex t = contextMenuIndex.sibling(contextMenuIndex.row() + delta, 0); + dive_trip_t *trip = (dive_trip_t *)t.data(DiveTripModel::TRIP_ROLE).value<void *>(); - //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(); |