[PATCH] Scroll to (and re-expand) the selection when sorting and moving

Linus Torvalds torvalds at linux-foundation.org
Thu May 30 23:16:03 PDT 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
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 <torvalds at linux-foundation.org>
---

This is not perfect - because we don't remember which *trips* are 
selected, when you re-sort in trip mode you lose the trip selection.

But it's a lot better than what we used to have.

  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 c384d487218e..075f12d4a85b 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<struct dive*> 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<QSortFilterProxyModel*>(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);
  }

-- 
1.8.3



More information about the subsurface mailing list