Skip to content

Commit

Permalink
Merge pull request #4330 from ninomp/del-key-to-remove-tracks
Browse files Browse the repository at this point in the history
Allow to remove tracks by pressing Delete key
  • Loading branch information
uklotzde authored Oct 20, 2021
2 parents b74f015 + c511446 commit 11fcfcd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/util/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ constexpr int kMaxNumberOfDecks = 4;
// for displaying the shortcut in the track context menu
const Qt::Modifier kPropertiesShortcutModifier = Qt::CTRL;
const Qt::Key kPropertiesShortcutKey = Qt::Key_Return;

// Keyboard shortcut for hiding track and removing from Crate/Playlist/AutoDJ queue.
// This is also used to display the shortcut in the track context menu.
#ifdef Q_OS_MAC
// Note: On macOS, CTRL corresponds to the Command key.
const Qt::Modifier kHideRemoveShortcutModifier = Qt::CTRL;
const Qt::Key kHideRemoveShortcutKey = Qt::Key_Backspace;
#else
const Qt::Modifier kHideRemoveShortcutModifier = static_cast<Qt::Modifier>(0);
const Qt::Key kHideRemoveShortcutKey = Qt::Key_Delete;
#endif
11 changes: 11 additions & 0 deletions src/widget/wtrackmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ void WTrackMenu::createMenus() {
}

void WTrackMenu::createActions() {
const auto hideRemoveKeySequence =
QKeySequence(kHideRemoveShortcutModifier + kHideRemoveShortcutKey);

if (featureIsEnabled(Feature::AutoDJ)) {
m_pAutoDJBottomAct = new QAction(tr("Add to Auto DJ Queue (bottom)"), this);
connect(m_pAutoDJBottomAct, &QAction::triggered, this, &WTrackMenu::slotAddToAutoDJBottom);
Expand All @@ -189,18 +192,26 @@ void WTrackMenu::createActions() {
}

if (featureIsEnabled(Feature::Remove)) {
// Keyboard shortcuts are set here just to have them displayed in the menu.
// Actual keypress is handled in WTrackTableView::keyPressEvent().
m_pRemoveAct = new QAction(tr("Remove"), this);
m_pRemoveAct->setShortcut(hideRemoveKeySequence);
connect(m_pRemoveAct, &QAction::triggered, this, &WTrackMenu::slotRemove);

m_pRemovePlaylistAct = new QAction(tr("Remove from Playlist"), this);
m_pRemovePlaylistAct->setShortcut(hideRemoveKeySequence);
connect(m_pRemovePlaylistAct, &QAction::triggered, this, &WTrackMenu::slotRemove);

m_pRemoveCrateAct = new QAction(tr("Remove from Crate"), this);
m_pRemoveCrateAct->setShortcut(hideRemoveKeySequence);
connect(m_pRemoveCrateAct, &QAction::triggered, this, &WTrackMenu::slotRemove);
}

if (featureIsEnabled(Feature::HideUnhidePurge)) {
m_pHideAct = new QAction(tr("Hide from Library"), this);
// This is just for having the shortcut displayed next to the action in the menu.
// The actual keypress is handled in WTrackTableView::keyPressEvent().
m_pHideAct->setShortcut(hideRemoveKeySequence);
connect(m_pHideAct, &QAction::triggered, this, &WTrackMenu::slotHide);

m_pUnhideAct = new QAction(tr("Unhide from Library"), this);
Expand Down
43 changes: 43 additions & 0 deletions src/widget/wtracktableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,53 @@ void WTrackTableView::keyPressEvent(QKeyEvent* event) {
m_pTrackMenu->loadTrackModelIndices(indices);
m_pTrackMenu->slotShowDlgTrackInfo();
}
} else if (event->key() == kHideRemoveShortcutKey &&
event->modifiers() == kHideRemoveShortcutModifier) {
hideOrRemoveSelectedTracks();
}
QTableView::keyPressEvent(event);
}

void WTrackTableView::hideOrRemoveSelectedTracks() {
QModelIndexList indices = selectionModel()->selectedRows();
if (indices.isEmpty()) {
return;
}

TrackModel* pTrackModel = getTrackModel();
if (!pTrackModel) {
return;
}

QMessageBox::StandardButton response;
if (pTrackModel->hasCapabilities(TrackModel::Capability::Hide)) {
// Hide tracks if this is the main library table
response = QMessageBox::question(this,
tr("Confirm track hide"),
tr("Are you sure you want to hide the selected tracks?"));
if (response == QMessageBox::Yes) {
pTrackModel->hideTracks(indices);
}
} else {
// Else remove the tracks from AutoDJ/crate/playlist
QString message;
if (pTrackModel->hasCapabilities(TrackModel::Capability::Remove)) {
message = tr("Are you sure you want to remove the selected tracks from AutoDJ queue?");
} else if (pTrackModel->hasCapabilities(TrackModel::Capability::RemoveCrate)) {
message = tr("Are you sure you want to remove the selected tracks from this crate?");
} else if (pTrackModel->hasCapabilities(TrackModel::Capability::RemovePlaylist)) {
message = tr("Are you sure you want to remove the selected tracks from this playlist?");
} else {
return;
}

response = QMessageBox::question(this, tr("Confirm track removal"), message);
if (response == QMessageBox::Yes) {
pTrackModel->removeTracks(indices);
}
}
}

void WTrackTableView::loadSelectedTrack() {
auto indices = selectionModel()->selectedRows();
if (indices.size() > 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/widget/wtracktableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class WTrackTableView : public WLibraryTableView {

void initTrackMenu();

void hideOrRemoveSelectedTracks();

const UserSettingsPointer m_pConfig;
Library* const m_pLibrary;

Expand Down

0 comments on commit 11fcfcd

Please sign in to comment.