summaryrefslogtreecommitdiffstats
path: root/qt-models/divetripmodel.cpp
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2020-02-20 13:22:57 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-03-10 09:25:57 -0700
commitccf5bf6445e4a0897a6b4141881a3f61d96b9b45 (patch)
treeab8672ba0404e297c0154126362a7e1a43517622 /qt-models/divetripmodel.cpp
parent0cd275af67192759414f8e1d0666977f10852d6f (diff)
downloadsubsurface-ccf5bf6445e4a0897a6b4141881a3f61d96b9b45.tar.gz
dive models: add helper role to find trip above or below dive
This is only used in the mobile UI where the sort direction is fixed and we refer to dives based on the tree model. So the terms used and the concepts that these rely on should be guaranteed to be valid. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-models/divetripmodel.cpp')
-rw-r--r--qt-models/divetripmodel.cpp33
1 files changed, 31 insertions, 2 deletions
diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp
index 7c6655ba8..1281ed84c 100644
--- a/qt-models/divetripmodel.cpp
+++ b/qt-models/divetripmodel.cpp
@@ -964,10 +964,19 @@ QVariant DiveTripModelTree::data(const QModelIndex &index, int role) const
const Item &item = items[index.row()];
return std::find(item.dives.begin(), item.dives.end(), current_dive) != item.dives.end();
}
- if (entry.trip)
+ if (entry.trip) {
return tripData(entry.trip, index.column(), role);
- else
+ } else if (entry.dive) {
+#if defined(SUBSURFACE_MOBILE)
+ if (role == MobileListModel::TripAbove)
+ return tripInDirection(entry.dive, +1);
+ if (role == MobileListModel::TripBelow)
+ return tripInDirection(entry.dive, -1);
+#endif
return diveData(entry.dive, index.column(), role);
+ } else {
+ return QVariant();
+ }
}
// After a trip changed, the top level might need to be reordered.
@@ -1136,6 +1145,26 @@ static QVector<dive *> visibleDives(const QVector<dive *> &dives)
return res;
}
+#ifdef SUBSURFACE_MOBILE
+int DiveTripModelTree::tripInDirection(const struct dive *d, int direction) const
+{
+ for (int i = 0; i < (int)items.size(); ++i) {
+ if (items[i].d_or_t.dive == d || (items[i].d_or_t.trip && findDiveInTrip(i, d) != -1)) {
+ // now walk in the direction given to find a trip
+ int offset = direction;
+ while (i + offset >= 0 && i + offset < (int)items.size()) {
+ if (items[i + offset].d_or_t.trip && (!d->divetrip || items[i + offset].d_or_t.trip->id != d->divetrip->id))
+ // we found a trip (and if the dive is already in a trip, we make sure this is a different trip)
+ return items[i + offset].d_or_t.trip->id;
+ offset += direction;
+ }
+ break;
+ }
+ }
+ return -1;
+}
+#endif
+
void DiveTripModelTree::divesAdded(dive_trip *trip, bool newTrip, const QVector<dive *> &divesIn)
{
QVector <dive *> dives = visibleDives(divesIn);