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

DownloadListView improvements #1362

Merged
merged 3 commits into from
Jan 16, 2021
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
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ add_filter(NAME src/dialogs GROUPS

add_filter(NAME src/downloads GROUPS
downloadlist
downloadlistwidget
downloadlistview
downloadmanager
)

Expand Down
191 changes: 113 additions & 78 deletions src/downloadlistwidget.cpp → src/downloadlistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
*/

#include "downloadlist.h"
#include "downloadlistwidget.h"
#include "downloadlistview.h"
#include <report.h>
#include <log.h>
#include <QPainter>
Expand All @@ -34,7 +34,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
using namespace MOBase;

DownloadProgressDelegate::DownloadProgressDelegate(
DownloadManager* manager, DownloadListWidget* list)
DownloadManager* manager, DownloadListView* list)
: QStyledItemDelegate(list), m_Manager(manager), m_List(list)
{
}
Expand Down Expand Up @@ -113,7 +113,7 @@ void DownloadListHeader::mouseReleaseEvent(QMouseEvent *event)
customResizeSections();
}

DownloadListWidget::DownloadListWidget(QWidget *parent)
DownloadListView::DownloadListView(QWidget *parent)
: QTreeView(parent)
{
setHeader(new DownloadListHeader(Qt::Horizontal, this));
Expand All @@ -135,11 +135,11 @@ DownloadListWidget::DownloadListWidget(QWidget *parent)
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onCustomContextMenu(QPoint)));
}

DownloadListWidget::~DownloadListWidget()
DownloadListView::~DownloadListView()
{
}

void DownloadListWidget::setManager(DownloadManager *manager)
void DownloadListView::setManager(DownloadManager *manager)
{
m_Manager = manager;

Expand All @@ -154,18 +154,18 @@ void DownloadListWidget::setManager(DownloadManager *manager)
header()->hideSection(DownloadList::COL_SOURCEGAME);
}

void DownloadListWidget::setSourceModel(DownloadList *sourceModel)
void DownloadListView::setSourceModel(DownloadList *sourceModel)
{
m_SourceModel = sourceModel;
}

void DownloadListWidget::setMetaDisplay(bool metaDisplay)
void DownloadListView::setMetaDisplay(bool metaDisplay)
{
if (m_SourceModel != nullptr)
m_SourceModel->setMetaDisplay(metaDisplay);
}

void DownloadListWidget::onDoubleClick(const QModelIndex &index)
void DownloadListView::onDoubleClick(const QModelIndex &index)
{
QModelIndex sourceIndex = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index);
if (m_Manager->getState(sourceIndex.row()) >= DownloadManager::STATE_READY)
Expand All @@ -175,7 +175,7 @@ void DownloadListWidget::onDoubleClick(const QModelIndex &index)
emit resumeDownload(sourceIndex.row());
}

void DownloadListWidget::onHeaderCustomContextMenu(const QPoint &point)
void DownloadListView::onHeaderCustomContextMenu(const QPoint &point)
{
QMenu menu;

Expand Down Expand Up @@ -209,98 +209,133 @@ void DownloadListWidget::onHeaderCustomContextMenu(const QPoint &point)
qobject_cast<DownloadListHeader*>(header())->customResizeSections();
}

void DownloadListWidget::resizeEvent(QResizeEvent *event)
void DownloadListView::resizeEvent(QResizeEvent *event)
{
QTreeView::resizeEvent(event);
qobject_cast<DownloadListHeader*>(header())->customResizeSections();
}

void DownloadListWidget::onCustomContextMenu(const QPoint &point)
void DownloadListView::onCustomContextMenu(const QPoint &point)
{
QMenu menu(this);
QModelIndex index = indexAt(point);
bool hidden = false;

try
{
try {
if (index.row() >= 0) {
m_ContextRow = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index).row();
DownloadManager::DownloadState state = m_Manager->getState(m_ContextRow);
const int row = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index).row();
DownloadManager::DownloadState state = m_Manager->getState(row);

hidden = m_Manager->isHidden(m_ContextRow);
hidden = m_Manager->isHidden(row);

if (state >= DownloadManager::STATE_READY) {
menu.addAction(tr("Install"), this, SLOT(issueInstall()));
if (m_Manager->isInfoIncomplete(m_ContextRow))
menu.addAction(tr("Query Info"), this, SLOT(issueQueryInfoMd5()));
menu.addAction(tr("Install"), [=] { issueInstall(row); });
if (m_Manager->isInfoIncomplete(row))
menu.addAction(tr("Query Info"), [=] { issueQueryInfoMd5(row); });
else
menu.addAction(tr("Visit on Nexus"), this, SLOT(issueVisitOnNexus()));
menu.addAction(tr("Open File"), this, SLOT(issueOpenFile()));
menu.addAction(tr("Open Meta File"), this, SLOT(issueOpenMetaFile()));
menu.addAction(tr("Reveal in Explorer"), this, SLOT(issueOpenInDownloadsFolder()));
menu.addAction(tr("Visit on Nexus"), [=] { issueVisitOnNexus(row); });
menu.addAction(tr("Open File"), [=] { issueOpenFile(row); });
menu.addAction(tr("Open Meta File"), [=] { issueOpenMetaFile(row); });
menu.addAction(tr("Reveal in Explorer"), [=] { issueOpenInDownloadsFolder(row); });

menu.addSeparator();

menu.addAction(tr("Delete..."), this, SLOT(issueDelete()));
menu.addAction(tr("Delete..."), [=] { issueDelete(row); });
if (hidden)
menu.addAction(tr("Un-Hide"), this, SLOT(issueRestoreToView()));
menu.addAction(tr("Un-Hide"), [=] { issueRestoreToView(row); });
else
menu.addAction(tr("Hide"), this, SLOT(issueRemoveFromView()));
menu.addAction(tr("Hide"), [=] { issueRemoveFromView(row); });
} else if (state == DownloadManager::STATE_DOWNLOADING) {
menu.addAction(tr("Cancel"), this, SLOT(issueCancel()));
menu.addAction(tr("Pause"), this, SLOT(issuePause()));
menu.addAction(tr("Reveal in Explorer"), this, SLOT(issueOpenInDownloadsFolder()));
} else if ((state == DownloadManager::STATE_PAUSED) || (state == DownloadManager::STATE_ERROR)
|| (state == DownloadManager::STATE_PAUSING)) {
menu.addAction(tr("Delete..."), this, SLOT(issueDelete()));
menu.addAction(tr("Resume"), this, SLOT(issueResume()));
menu.addAction(tr("Reveal in Explorer"), this, SLOT(issueOpenInDownloadsFolder()));
menu.addAction(tr("Cancel"), [=] { issueCancel(row); });
menu.addAction(tr("Pause"), [=] { issuePause(row); });
menu.addAction(tr("Reveal in Explorer"), [=] { issueOpenInDownloadsFolder(row); });
}
else if ((state == DownloadManager::STATE_PAUSED) || (state == DownloadManager::STATE_ERROR)
|| (state == DownloadManager::STATE_PAUSING)) {
menu.addAction(tr("Delete..."), [=] { issueDelete(row); });
menu.addAction(tr("Resume"), [=] { issueResume(row); });
menu.addAction(tr("Reveal in Explorer"), [=] { issueOpenInDownloadsFolder(row); });
}

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
}

menu.addAction(tr("Delete Installed Downloads..."), this, SLOT(issueDeleteCompleted()));
menu.addAction(tr("Delete Uninstalled Downloads..."), this, SLOT(issueDeleteUninstalled()));
menu.addAction(tr("Delete All Downloads..."), this, SLOT(issueDeleteAll()));
menu.addAction(tr("Delete Installed Downloads..."), [=] { issueDeleteCompleted(); });
menu.addAction(tr("Delete Uninstalled Downloads..."), [=] { issueDeleteUninstalled(); });
menu.addAction(tr("Delete All Downloads..."), [=] { issueDeleteAll(); });

menu.addSeparator();
if (!hidden) {
menu.addAction(tr("Hide Installed..."), this, SLOT(issueRemoveFromViewCompleted()));
menu.addAction(tr("Hide Uninstalled..."), this, SLOT(issueRemoveFromViewUninstalled()));
menu.addAction(tr("Hide All..."), this, SLOT(issueRemoveFromViewAll()));
menu.addAction(tr("Hide Installed..."), [=] { issueRemoveFromViewCompleted(); });
menu.addAction(tr("Hide Uninstalled..."), [=] { issueRemoveFromViewUninstalled(); });
menu.addAction(tr("Hide All..."), [=] { issueRemoveFromViewAll(); });
} else {
menu.addAction(tr("Un-Hide All..."), this, SLOT(issueRestoreToViewAll()));
menu.addAction(tr("Un-Hide All..."), [=] { issueRestoreToViewAll(); } );
}

menu.exec(viewport()->mapToGlobal(point));
}

void DownloadListWidget::issueInstall()
void DownloadListView::keyPressEvent(QKeyEvent* event)
{
if (selectionModel()->hasSelection()) {
const int row = qobject_cast<QSortFilterProxyModel*>(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(m_ContextRow);
emit installDownload(index);
}

void DownloadListWidget::issueQueryInfo()
void DownloadListView::issueQueryInfo(int index)
{
emit queryInfo(m_ContextRow);
emit queryInfo(index);
}

void DownloadListWidget::issueQueryInfoMd5()
void DownloadListView::issueQueryInfoMd5(int index)
{
emit queryInfoMd5(m_ContextRow);
emit queryInfoMd5(index);
}

void DownloadListWidget::issueDelete()
void DownloadListView::issueDelete(int index)
{
const auto r = MOBase::TaskDialog(this, tr("Delete download"))
.main("Are you sure you want to delete this download?")
.content(m_Manager->getFilePath(m_ContextRow))
.content(m_Manager->getFilePath(index))
.icon(QMessageBox::Question)
.button({tr("Move to the Recycle Bin"), QMessageBox::Yes})
.button({tr("Cancel"), QMessageBox::Cancel})
Expand All @@ -310,60 +345,60 @@ void DownloadListWidget::issueDelete()
return;
}

emit removeDownload(m_ContextRow, true);
emit removeDownload(index, true);
}

void DownloadListWidget::issueRemoveFromView()
void DownloadListView::issueRemoveFromView(int index)
{
log::debug("removing from view: {}", m_ContextRow);
emit removeDownload(m_ContextRow, false);
log::debug("removing from view: {}", index);
emit removeDownload(index, false);
}

void DownloadListWidget::issueRestoreToView()
void DownloadListView::issueRestoreToView(int index)
{
emit restoreDownload(m_ContextRow);
emit restoreDownload(index);
}

void DownloadListWidget::issueRestoreToViewAll()
void DownloadListView::issueRestoreToViewAll()
{
emit restoreDownload(-1);
}

void DownloadListWidget::issueVisitOnNexus()
void DownloadListView::issueVisitOnNexus(int index)
{
emit visitOnNexus(m_ContextRow);
emit visitOnNexus(index);
}

void DownloadListWidget::issueOpenFile()
void DownloadListView::issueOpenFile(int index)
{
emit openFile(m_ContextRow);
emit openFile(index);
}

void DownloadListWidget::issueOpenMetaFile() {
emit openMetaFile(m_ContextRow);
void DownloadListView::issueOpenMetaFile(int index) {
emit openMetaFile(index);
}

void DownloadListWidget::issueOpenInDownloadsFolder()
void DownloadListView::issueOpenInDownloadsFolder(int index)
{
emit openInDownloadsFolder(m_ContextRow);
emit openInDownloadsFolder(index);
}

void DownloadListWidget::issueCancel()
void DownloadListView::issueCancel(int index)
{
emit cancelDownload(m_ContextRow);
emit cancelDownload(index);
}

void DownloadListWidget::issuePause()
void DownloadListView::issuePause(int index)
{
emit pauseDownload(m_ContextRow);
emit pauseDownload(index);
}

void DownloadListWidget::issueResume()
void DownloadListView::issueResume(int index)
{
emit resumeDownload(m_ContextRow);
emit resumeDownload(index);
}

void DownloadListWidget::issueDeleteAll()
void DownloadListView::issueDeleteAll()
{
if (QMessageBox::warning(nullptr, tr("Delete Files?"),
tr("This will remove all finished downloads from this list and from disk.\n\nAre you absolutely sure you want to proceed?"),
Expand All @@ -372,7 +407,7 @@ void DownloadListWidget::issueDeleteAll()
}
}

void DownloadListWidget::issueDeleteCompleted()
void DownloadListView::issueDeleteCompleted()
{
if (QMessageBox::warning(nullptr, tr("Delete Files?"),
tr("This will remove all installed downloads from this list and from disk.\n\nAre you absolutely sure you want to proceed?"),
Expand All @@ -381,7 +416,7 @@ void DownloadListWidget::issueDeleteCompleted()
}
}

void DownloadListWidget::issueDeleteUninstalled()
void DownloadListView::issueDeleteUninstalled()
{
if (QMessageBox::warning(nullptr, tr("Delete Files?"),
tr("This will remove all uninstalled downloads from this list and from disk.\n\nAre you absolutely sure you want to proceed?"),
Expand All @@ -390,7 +425,7 @@ void DownloadListWidget::issueDeleteUninstalled()
}
}

void DownloadListWidget::issueRemoveFromViewAll()
void DownloadListView::issueRemoveFromViewAll()
{
if (QMessageBox::question(nullptr, tr("Hide Files?"),
tr("This will remove all finished downloads from this list (but NOT from disk)."),
Expand All @@ -399,7 +434,7 @@ void DownloadListWidget::issueRemoveFromViewAll()
}
}

void DownloadListWidget::issueRemoveFromViewCompleted()
void DownloadListView::issueRemoveFromViewCompleted()
{
if (QMessageBox::question(nullptr, tr("Hide Files?"),
tr("This will remove all installed downloads from this list (but NOT from disk)."),
Expand All @@ -408,7 +443,7 @@ void DownloadListWidget::issueRemoveFromViewCompleted()
}
}

void DownloadListWidget::issueRemoveFromViewUninstalled()
void DownloadListView::issueRemoveFromViewUninstalled()
{
if (QMessageBox::question(nullptr, tr("Hide Files?"),
tr("This will remove all uninstalled downloads from this list (but NOT from disk)."),
Expand Down
Loading