Skip to content

Commit

Permalink
Merge pull request #11090 from robbert-vdh/feature/library-tracks-wra…
Browse files Browse the repository at this point in the history
…paround

Wrap selection around at the bottom/top of the library's tracks list
  • Loading branch information
daschuer authored Dec 8, 2022
2 parents 1edfbde + 11f5f89 commit 4127490
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
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

0 comments on commit 4127490

Please sign in to comment.