Correctly unselect trips when dive list filters change

Oddly Qt left the trips selected (but all dives where unselected in the
UI). This got our internal state rather confused. With this change we
clean up that mess and go back to just having those dives that were
originally selected and are still visible show up as selected.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-11-03 17:52:04 -08:00
parent a3f1dc7681
commit 0dd87989a8
3 changed files with 39 additions and 5 deletions

View file

@ -234,6 +234,29 @@ void DiveListView::selectTrip(dive_trip_t *trip)
expand(idx); expand(idx);
} }
// this is an odd one - when filtering the dive list the selection status of the trips
// is kept - but all other selections are lost. That's gets us into rather inconsistent state
// we call this function which clears the selection state of the trips as well, but does so
// without updating our internal "->selected" state. So once we called this function we can
// go back and select those dives that are still visible under the filter and everything
// works as expected
void DiveListView::clearTripSelection()
{
// we want to make sure no trips are selected
disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(selectionChanged(QItemSelection, QItemSelection)));
disconnect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(currentChanged(QModelIndex, QModelIndex)));
Q_FOREACH (const QModelIndex &index, selectionModel()->selectedRows()) {
dive_trip_t *trip = static_cast<dive_trip_t *>(index.data(DiveTripModel::TRIP_ROLE).value<void *>());
if (!trip)
continue;
selectionModel()->select(index, QItemSelectionModel::Deselect);
}
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(selectionChanged(QItemSelection, QItemSelection)));
connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(currentChanged(QModelIndex, QModelIndex)));
}
void DiveListView::unselectDives() void DiveListView::unselectDives()
{ {
// make sure we don't try to redraw the dives during the selection change // make sure we don't try to redraw the dives during the selection change

View file

@ -25,6 +25,7 @@ public:
void reload(DiveTripModel::Layout layout, bool forceSort = true); void reload(DiveTripModel::Layout layout, bool forceSort = true);
bool eventFilter(QObject *, QEvent *); bool eventFilter(QObject *, QEvent *);
void unselectDives(); void unselectDives();
void clearTripSelection();
void selectDive(int dive_table_idx, bool scrollto = false, bool toggle = false); void selectDive(int dive_table_idx, bool scrollto = false, bool toggle = false);
void selectDives(const QList<int> &newDiveSelection); void selectDives(const QList<int> &newDiveSelection);
void rememberSelection(); void rememberSelection();

View file

@ -2617,18 +2617,28 @@ void MultiFilterSortModel::myInvalidate()
{ {
int i; int i;
struct dive *d; struct dive *d;
DiveListView *dlv = MainWindow::instance()->dive_list();
invalidate(); invalidate();
for_each_dive (i, d) { // first make sure the trips are no longer shown as selected
if(d->selected) // (but without updating the selection state of the dives... this just cleans
MainWindow::instance()->dive_list()->selectDive(get_idx_by_uniq_id(d->id)); // up an oddity in the filter handling)
} dlv->clearTripSelection();
// if we have no more selected dives, clean up the display - this later triggers us // if we have no more selected dives, clean up the display - this later triggers us
// to pick one of the dives that are shown in the list as selected dive which is the // to pick one of the dives that are shown in the list as selected dive which is the
// natural behavior // natural behavior
if (amount_selected == 0) if (amount_selected == 0) {
MainWindow::instance()->cleanUpEmpty(); MainWindow::instance()->cleanUpEmpty();
} else {
// otherwise find the dives that should still be selected (the filter above unselected any
// dive that's no longer visible) and select them again
for_each_dive (i, d) {
if(d->selected)
dlv->selectDive(get_idx_by_uniq_id(d->id));
}
}
} }
void MultiFilterSortModel::addFilterModel(MultiFilterInterface *model) void MultiFilterSortModel::addFilterModel(MultiFilterInterface *model)