Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap selection around at the bottom/top of the library's tracks list #11090

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions src/library/librarycontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,11 @@ void LibraryControl::slotMoveVertical(double v) {
return;
}
case FocusWidget::TracksTable: {
// This wraps around at top/bottom. Doesn't match Up/Down key behaviour
// and may not be desired.
//int i = static_cast<int>(v);
//slotSelectTrack(i);
//return;
// `WLibraryTableView`'s cursor movement function has been overridden to
// wrap the selection around at the top/bottom of the tracks list. This
// behavior is thus shared between `[Library],MoveVertical` and Up/Down
// cursor key presses. See `WLibraryTableView::moveCursor()` for an
// explanation on why this is useful.
break;
}
case FocusWidget::Dialog: {
Expand Down
52 changes: 52 additions & 0 deletions src/widget/wlibrarytableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,55 @@ void WLibraryTableView::focusInEvent(QFocusEvent* event) {
}
}
}

QModelIndex WLibraryTableView::moveCursor(CursorAction cursorAction,
Qt::KeyboardModifiers modifiers) {
// The up and down cursor keys should wrap the list around. This behavior
// also applies to the `[Library],MoveVertical` action that is usually bound
// to the library browse encoder on controllers. Otherwise browsing a
// key-sorted library list requires either a serious workout or the user
// needs to reach for the mouse or keyboard when moving between 12/C#m/E and
// 1/G#m/B.
QAbstractItemModel* pModel = model();
if (pModel &&
(cursorAction == QAbstractItemView::MoveUp ||
cursorAction == QAbstractItemView::MoveDown)) {
// This is very similar to `moveSelection()`, except that it doesn't
// actually modify the selection
const QModelIndex current = currentIndex();
if (current.isValid()) {
const int row = currentIndex().row();
const int column = currentIndex().column();
if (cursorAction == QAbstractItemView::MoveDown) {
if (row + 1 < pModel->rowCount()) {
return pModel->index(row + 1, column);
} else {
return pModel->index(0, column);
}
} else {
if (row - 1 >= 0) {
return pModel->index(row - 1, column);
} else {
return pModel->index(pModel->rowCount() - 1, column);
}
}
} else {
// Selecting a hidden column doesn't work, so we'll need to find the
// first non-hidden column here
int column = 0;
while (isColumnHidden(column) && column < pModel->columnCount()) {
column++;
}

// If the cursor does not yet exist (because the view has not yet
// been interacted with) then this selects the first/last row
if (cursorAction == QAbstractItemView::MoveDown) {
return pModel->index(0, column);
} else {
return pModel->index(pModel->rowCount() - 1, column);
}
}
}

return QTableView::moveCursor(cursorAction, modifiers);
}
2 changes: 2 additions & 0 deletions src/widget/wlibrarytableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class WLibraryTableView : public QTableView, public virtual LibraryView {

protected:
void focusInEvent(QFocusEvent* event) override;
QModelIndex moveCursor(CursorAction cursorAction,
Qt::KeyboardModifiers modifiers) override;
virtual QString getModelStateKey() const = 0;

private:
Expand Down