From a45c5faa8c7aab7d4263eb98d85d5e7cd589ef73 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 13 Feb 2020 22:56:16 +0100 Subject: 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 --- qt-models/divetripmodel.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'qt-models') diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 09755a9e5..e3b8266cf 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -436,6 +436,7 @@ bool DiveTripModelBase::setData(const QModelIndex &index, const QVariant &value, struct ShownChange { QVector newShown; QVector newHidden; + bool currentChanged; void filterDive(dive *d, const DiveFilter *filter); }; @@ -455,6 +456,7 @@ void ShownChange::filterDive(dive *d, const DiveFilter *filter) static ShownChange updateShown(QVector &dives) { DiveFilter *filter = DiveFilter::instance(); + dive *old_current = current_dive; ShownChange res; for (dive *d: dives) res.filterDive(d, filter); @@ -464,6 +466,7 @@ static ShownChange updateShown(QVector &dives) dives.removeAll(d); for (dive *d: res.newShown) dives.removeAll(d); + res.currentChanged = old_current != current_dive; return res; } @@ -471,11 +474,13 @@ static ShownChange updateShown(QVector &dives) static ShownChange updateShownAll() { DiveFilter *filter = DiveFilter::instance(); + dive *old_current = current_dive; ShownChange res; for (int i = 0; i < dive_table.nr; ++i) res.filterDive(get_dive(i), filter); if (!res.newShown.empty() || !res.newHidden.empty()) emit diveListNotifier.numShownChanged(); + res.currentChanged = old_current != current_dive; return res; } @@ -779,8 +784,6 @@ void processByTrip(QVector dives, Function action) // a given time! void DiveTripModelTree::filterReset() { - dive *old_current = current_dive; - ShownChange change = updateShownAll(); processByTrip(change.newHidden, [this] (dive_trip *trip, const QVector &divesInTrip) { divesHidden(trip, divesInTrip); }); @@ -789,7 +792,7 @@ void DiveTripModelTree::filterReset() // If the current dive changed, instruct the UI of the changed selection // TODO: This is way to heavy, as it reloads the whole selection! - if (old_current != current_dive) + if (change.currentChanged) initSelection(); } @@ -1150,6 +1153,12 @@ void DiveTripModelTree::divesChangedTrip(dive_trip *trip, const QVector // If necessary, move the trip 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) @@ -1378,15 +1387,13 @@ dive *DiveTripModelList::diveOrNull(const QModelIndex &index) const // a given time! void DiveTripModelList::filterReset() { - dive *old_current = current_dive; - ShownChange change = updateShownAll(); removeDives(change.newHidden); addDives(change.newShown); // If the current dive changed, instruct the UI of the changed selection // TODO: This is way to heavy, as it reloads the whole selection! - if (old_current != current_dive) + if (change.currentChanged) initSelection(); } @@ -1463,6 +1470,12 @@ void DiveTripModelList::divesChanged(const QVector &divesIn) dataChanged(createIndex(from, 0, noParent), createIndex(to - 1, COLUMNS - 1, noParent)); 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 &divesIn) -- cgit v1.2.3-70-g09d2