From 8307763212c4bbc53b527fe33d997a2ec782f435 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 31 May 2013 13:53:04 +0900 Subject: [PATCH] Scroll to (and re-expand) the selection when sorting and moving The keyboard movement wouldn't scroll to the newly changed selection. Similarly, when sorting the dive list, we'd end up losing the currently selected dive (and generally collapsing the currently exposed trips). This adds the scrolling/exposing logic for those cases. Signed-off-by: Linus Torvalds Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index c384d4872..075f12d4a 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -62,20 +62,19 @@ bool DiveListView::eventFilter(QObject* , QEvent* event) return true; } +// NOTE! This loses trip selection, because while we remember the +// dives, we don't remember the trips (see the "currentSelectedDives" +// list). I haven't figured out how to look up the trip from the +// index. TRIP_ROLE vs DIVE_ROLE? void DiveListView::headerClicked(int i) { - if (currentHeaderClicked == i) { - sortByColumn(i); - return; - } - - if (currentLayout == (i == (int) TreeItemDT::NR ? DiveTripModel::TREE : DiveTripModel::LIST)) { - sortByColumn(i); - return; - } - QItemSelection oldSelection = selectionModel()->selection(); QList currentSelectedDives; + DiveTripModel::Layout newLayout; + bool scrolled = false; + + newLayout = i == (int) TreeItemDT::NR ? DiveTripModel::TREE : DiveTripModel::LIST; + Q_FOREACH(const QModelIndex& index , oldSelection.indexes()) { if (index.column() != 0) // We only care about the dives, so, let's stick to rows and discard columns. continue; @@ -85,10 +84,16 @@ void DiveListView::headerClicked(int i) currentSelectedDives.push_back(d); } - // clear the model, repopulate with new indexes. - reload( i == (int) TreeItemDT::NR ? DiveTripModel::TREE : DiveTripModel::LIST, false); selectionModel()->clearSelection(); - sortByColumn(i, Qt::DescendingOrder); + + /* No layout change? Just re-sort, and scroll to first selection, making sure all selections are expanded */ + if (currentLayout == newLayout) { + sortByColumn(i); + } else { + // clear the model, repopulate with new indexes. + reload(newLayout, false); + sortByColumn(i, Qt::DescendingOrder); + } QSortFilterProxyModel *m = qobject_cast(model()); @@ -96,11 +101,17 @@ void DiveListView::headerClicked(int i) Q_FOREACH(struct dive *d, currentSelectedDives) { QModelIndexList match = m->match(m->index(0,0), TreeItemDT::NR, d->number, 1, Qt::MatchRecursive); QModelIndex idx = match.first(); - if (i == (int) TreeItemDT::NR && idx.parent().isValid()) { // Tree Mode Activated. + + if (newLayout == DiveTripModel::TREE) { QModelIndex parent = idx.parent(); - expand(parent); + if (parent.isValid()) + expand(parent); } selectionModel()->select( idx, QItemSelectionModel::Select | QItemSelectionModel::Rows); + if (!scrolled) { + scrollTo(idx, PositionAtCenter); + scrolled = true; + } } } @@ -186,6 +197,7 @@ void DiveListView::currentChanged(const QModelIndex& current, const QModelIndex& selectedDive = get_divenr(dive); if (selectedDive == selected_dive) return; + scrollTo(current); Q_EMIT currentDiveChanged(selectedDive); }