Filter: update current dive if change hides current dive

When changing the filter-criterion and the current dive is
hidden, a new dive is made current. However, when a dive is
hidden because it was edited, it is still shown.

Make this consistent by also selecing a new current dive
in the latter case. Do this by comparing the current_dive
before and after calculating the filter. Since this is now
done in multiple places move this code to the ShownChange
class.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-02-13 22:56:16 +01:00 committed by Dirk Hohndel
parent be3d7bffc6
commit a45c5faa8c

View file

@ -436,6 +436,7 @@ bool DiveTripModelBase::setData(const QModelIndex &index, const QVariant &value,
struct ShownChange { struct ShownChange {
QVector<dive *> newShown; QVector<dive *> newShown;
QVector<dive *> newHidden; QVector<dive *> newHidden;
bool currentChanged;
void filterDive(dive *d, const DiveFilter *filter); void filterDive(dive *d, const DiveFilter *filter);
}; };
@ -455,6 +456,7 @@ void ShownChange::filterDive(dive *d, const DiveFilter *filter)
static ShownChange updateShown(QVector<dive *> &dives) static ShownChange updateShown(QVector<dive *> &dives)
{ {
DiveFilter *filter = DiveFilter::instance(); DiveFilter *filter = DiveFilter::instance();
dive *old_current = current_dive;
ShownChange res; ShownChange res;
for (dive *d: dives) for (dive *d: dives)
res.filterDive(d, filter); res.filterDive(d, filter);
@ -464,6 +466,7 @@ static ShownChange updateShown(QVector<dive *> &dives)
dives.removeAll(d); dives.removeAll(d);
for (dive *d: res.newShown) for (dive *d: res.newShown)
dives.removeAll(d); dives.removeAll(d);
res.currentChanged = old_current != current_dive;
return res; return res;
} }
@ -471,11 +474,13 @@ static ShownChange updateShown(QVector<dive *> &dives)
static ShownChange updateShownAll() static ShownChange updateShownAll()
{ {
DiveFilter *filter = DiveFilter::instance(); DiveFilter *filter = DiveFilter::instance();
dive *old_current = current_dive;
ShownChange res; ShownChange res;
for (int i = 0; i < dive_table.nr; ++i) for (int i = 0; i < dive_table.nr; ++i)
res.filterDive(get_dive(i), filter); res.filterDive(get_dive(i), filter);
if (!res.newShown.empty() || !res.newHidden.empty()) if (!res.newShown.empty() || !res.newHidden.empty())
emit diveListNotifier.numShownChanged(); emit diveListNotifier.numShownChanged();
res.currentChanged = old_current != current_dive;
return res; return res;
} }
@ -779,8 +784,6 @@ void processByTrip(QVector<dive *> dives, Function action)
// a given time! // a given time!
void DiveTripModelTree::filterReset() void DiveTripModelTree::filterReset()
{ {
dive *old_current = current_dive;
ShownChange change = updateShownAll(); ShownChange change = updateShownAll();
processByTrip(change.newHidden, [this] (dive_trip *trip, const QVector<dive *> &divesInTrip) processByTrip(change.newHidden, [this] (dive_trip *trip, const QVector<dive *> &divesInTrip)
{ divesHidden(trip, divesInTrip); }); { divesHidden(trip, divesInTrip); });
@ -789,7 +792,7 @@ void DiveTripModelTree::filterReset()
// If the current dive changed, instruct the UI of the changed selection // If the current dive changed, instruct the UI of the changed selection
// TODO: This is way to heavy, as it reloads the whole selection! // TODO: This is way to heavy, as it reloads the whole selection!
if (old_current != current_dive) if (change.currentChanged)
initSelection(); initSelection();
} }
@ -1150,6 +1153,12 @@ void DiveTripModelTree::divesChangedTrip(dive_trip *trip, const QVector<dive *>
// If necessary, move the trip // If necessary, move the trip
topLevelChanged(idx); topLevelChanged(idx);
} }
// If the current dive changed (because the change caused it to become hidden
// by the filter), instruct the UI of the changed selection.
// TODO: This is way to heavy, as it reloads the whole selection!
if (shownChange.currentChanged)
initSelection();
} }
void DiveTripModelTree::tripChanged(dive_trip *trip, TripField) void DiveTripModelTree::tripChanged(dive_trip *trip, TripField)
@ -1378,15 +1387,13 @@ dive *DiveTripModelList::diveOrNull(const QModelIndex &index) const
// a given time! // a given time!
void DiveTripModelList::filterReset() void DiveTripModelList::filterReset()
{ {
dive *old_current = current_dive;
ShownChange change = updateShownAll(); ShownChange change = updateShownAll();
removeDives(change.newHidden); removeDives(change.newHidden);
addDives(change.newShown); addDives(change.newShown);
// If the current dive changed, instruct the UI of the changed selection // If the current dive changed, instruct the UI of the changed selection
// TODO: This is way to heavy, as it reloads the whole selection! // TODO: This is way to heavy, as it reloads the whole selection!
if (old_current != current_dive) if (change.currentChanged)
initSelection(); initSelection();
} }
@ -1463,6 +1470,12 @@ void DiveTripModelList::divesChanged(const QVector<dive *> &divesIn)
dataChanged(createIndex(from, 0, noParent), createIndex(to - 1, COLUMNS - 1, noParent)); dataChanged(createIndex(from, 0, noParent), createIndex(to - 1, COLUMNS - 1, noParent));
return 0; // No items added or deleted return 0; // No items added or deleted
}); });
// If the current dive changed (because the change caused it to become hidden
// by the filter), instruct the UI of the changed selection.
// TODO: This is way to heavy, as it reloads the whole selection!
if (shownChange.currentChanged)
initSelection();
} }
void DiveTripModelList::divesTimeChanged(timestamp_t delta, const QVector<dive *> &divesIn) void DiveTripModelList::divesTimeChanged(timestamp_t delta, const QVector<dive *> &divesIn)