Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/widget/wlibrarytableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,45 @@ bool WLibraryTableView::restoreTrackModelState(
verticalScrollBar()->setValue(state->verticalScrollPosition);
horizontalScrollBar()->setValue(state->horizontalScrollPosition);

auto* pSelection = selectionModel();
pSelection->clearSelection();
// Build a selection range rather than selecting each track individually,
// which can lag the GUI by spamming selectionChanged() handlers.
QItemSelectionModel* pSelectionModel = selectionModel();
pSelectionModel->clearSelection();
QItemSelection newSelection;
QModelIndexList selectedRows = state->selectedRows;
QModelIndex topLeft;
QModelIndex bottomRight;
if (!selectedRows.isEmpty()) {
for (auto index : std::as_const(selectedRows)) {
pSelection->select(index,
QItemSelectionModel::Select | QItemSelectionModel::Rows);
// In saveTrackModelState() we fill state->selectedRows with the sorted
// rows, hence no need to sort here.
for (const QModelIndex& index : std::as_const(selectedRows)) {
if (!topLeft.isValid()) {
// start new range. only done once for first row
topLeft = index;
bottomRight = index;
continue;
}

if (index.row() == bottomRight.row() + 1) {
// continuous range
bottomRight = index;
continue;
} else {
// prev index was end of range, add current range to selection
// and start a new one
newSelection.select(topLeft, bottomRight);
topLeft = index;
bottomRight = index;
}
}

// If we reached end, submit the last selection
if (bottomRight == selectedRows.last()) {
newSelection.select(topLeft, bottomRight);
}
}
pSelectionModel->select(newSelection,
QItemSelectionModel::Select | QItemSelectionModel::Rows);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: all this only works because the list returned by QItemSelectionMdel::selectedRows() is sorted by row number

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was puzzled if this is the case. Can you add this as a source code comment?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, will add a comment. the docs aren't explicit about this so I looked it up in the Qt sources
selectedRows() and QItemSelectionRange

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


QModelIndex currIndex = state->currentIndex;
restoreCurrentIndex(currIndex);
Expand Down