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>
This commit is contained in:
Dirk Hohndel 2020-02-20 13:22:57 -08:00
parent 0cd275af67
commit ccf5bf6445
4 changed files with 36 additions and 3 deletions

View file

@ -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);

View file

@ -77,7 +77,6 @@ public:
// Used for sorting. This is a bit of a layering violation, as sorting should be performed
// by the higher-up QSortFilterProxyModel, but it makes things so much easier!
virtual bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const = 0;
signals:
// The propagation of selection changes is complex.
// The control flow of dive-selection goes:
@ -120,6 +119,7 @@ public slots:
public:
DiveTripModelTree(QObject *parent = nullptr);
int tripInDirection(const struct dive *d, int direction) const;
private:
int rowCount(const QModelIndex &parent) const override;
void clearData() override;

View file

@ -47,6 +47,8 @@ QHash<int, QByteArray> MobileListModelBase::roleNames() const
roles[FirstGasRole] = "firstGas";
roles[SelectedRole] = "selected";
roles[DiveInTripRole] = "diveInTrip";
roles[TripAbove] = "tripAbove";
roles[TripBelow] = "tripBelow";
return roles;
}

View file

@ -55,6 +55,8 @@ public:
FirstGasRole,
SelectedRole,
DiveInTripRole,
TripAbove,
TripBelow,
};
QHash<int, QByteArray> roleNames() const override;
protected: