From f28b501b30d13c581eaea72f1aeebc0dc4039cf3 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Fri, 15 Jan 2021 20:20:12 +0100 Subject: Handle enter and delete on download list. --- src/downloadlistview.cpp | 42 ++++++++++++++++++++++++++++++++++++++---- src/downloadlistview.h | 3 +++ 2 files changed, 41 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/downloadlistview.cpp b/src/downloadlistview.cpp index 7f9516ee..a1f544a8 100644 --- a/src/downloadlistview.cpp +++ b/src/downloadlistview.cpp @@ -221,8 +221,7 @@ void DownloadListView::onCustomContextMenu(const QPoint &point) QModelIndex index = indexAt(point); bool hidden = false; - try - { + try { if (index.row() >= 0) { const int row = qobject_cast(model())->mapToSource(index).row(); DownloadManager::DownloadState state = m_Manager->getState(row); @@ -260,8 +259,8 @@ void DownloadListView::onCustomContextMenu(const QPoint &point) menu.addSeparator(); } - } catch(std::exception&) - { + } + catch(std::exception&) { // this happens when the download index is not found, ignore it and don't // display download-specific actions } @@ -282,6 +281,41 @@ void DownloadListView::onCustomContextMenu(const QPoint &point) menu.exec(viewport()->mapToGlobal(point)); } +void DownloadListView::keyPressEvent(QKeyEvent* event) +{ + if (selectionModel()->hasSelection()) { + const int row = qobject_cast(model())->mapToSource(currentIndex()).row(); + auto state = m_Manager->getState(row); + if (state >= DownloadManager::STATE_READY) { + if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { + issueInstall(row); + } + else if (event->key() == Qt::Key_Delete) { + issueDelete(row); + } + } + else if (state == DownloadManager::STATE_DOWNLOADING) { + if (event->key() == Qt::Key_Delete) { + issueCancel(row); + } + else if (event->key() == Qt::Key_Space) { + issuePause(event->key()); + } + } + else if (state == DownloadManager::STATE_PAUSED + || state == DownloadManager::STATE_ERROR + || state == DownloadManager::STATE_PAUSING) { + if (event->key() == Qt::Key_Delete) { + issueDelete(row); + } + else if (event->key() == Qt::Key_Space) { + issueResume(row); + } + } + } + QTreeView::keyPressEvent(event); +} + void DownloadListView::issueInstall(int index) { emit installDownload(index); diff --git a/src/downloadlistview.h b/src/downloadlistview.h index 0a3e575b..050c869f 100644 --- a/src/downloadlistview.h +++ b/src/downloadlistview.h @@ -90,6 +90,9 @@ signals: void openMetaFile(int index); void openInDownloadsFolder(int index); +protected: + void keyPressEvent(QKeyEvent* event) override; + private slots: void onDoubleClick(const QModelIndex& index); void onCustomContextMenu(const QPoint& point); -- cgit v1.3.1