diff options
Diffstat (limited to 'src/downloadlistview.cpp')
| -rw-r--r-- | src/downloadlistview.cpp | 271 |
1 files changed, 163 insertions, 108 deletions
diff --git a/src/downloadlistview.cpp b/src/downloadlistview.cpp index 0264f3f7..a895d2e2 100644 --- a/src/downloadlistview.cpp +++ b/src/downloadlistview.cpp @@ -17,42 +17,42 @@ You should have received a copy of the GNU General Public License along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. */ -#include "downloadlist.h" #include "downloadlistview.h" -#include <report.h> -#include <log.h> -#include <QPainter> -#include <QMouseEvent> +#include "downloadlist.h" +#include <QApplication> +#include <QCheckBox> +#include <QHeaderView> #include <QMenu> #include <QMessageBox> +#include <QMouseEvent> +#include <QPainter> #include <QSortFilterProxyModel> -#include <QApplication> -#include <QHeaderView> -#include <QCheckBox> #include <QWidgetAction> +#include <log.h> +#include <report.h> using namespace MOBase; -DownloadProgressDelegate::DownloadProgressDelegate( - DownloadManager* manager, DownloadListView* list) +DownloadProgressDelegate::DownloadProgressDelegate(DownloadManager* manager, + DownloadListView* list) : QStyledItemDelegate(list), m_Manager(manager), m_List(list) -{ -} +{} -void DownloadProgressDelegate::paint( - QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const +void DownloadProgressDelegate::paint(QPainter* painter, + const QStyleOptionViewItem& option, + const QModelIndex& index) const { QModelIndex sourceIndex; - if (auto* proxy=dynamic_cast<QSortFilterProxyModel*>(m_List->model())) { + if (auto* proxy = dynamic_cast<QSortFilterProxyModel*>(m_List->model())) { sourceIndex = proxy->mapToSource(index); } else { sourceIndex = index; } bool pendingDownload = (sourceIndex.row() >= m_Manager->numTotalDownloads()); - if (sourceIndex.column() == DownloadList::COL_STATUS && !pendingDownload - && m_Manager->getState(sourceIndex.row()) == DownloadManager::STATE_DOWNLOADING) { + if (sourceIndex.column() == DownloadList::COL_STATUS && !pendingDownload && + m_Manager->getState(sourceIndex.row()) == DownloadManager::STATE_DOWNLOADING) { QProgressBar progressBar; progressBar.setProperty("downloadView", option.widget->property("downloadView")); progressBar.setProperty("downloadProgress", true); @@ -90,7 +90,8 @@ void DownloadListHeader::customResizeSections() for (int idx = rightVisible; idx >= 0; idx--) { if (!isSectionHidden(idx)) { if (length() != width()) - resizeSection(idx, std::max(sectionSize(idx) + width() - length(), minimumSectionSize())); + resizeSection(idx, std::max(sectionSize(idx) + width() - length(), + minimumSectionSize())); else break; } @@ -99,7 +100,8 @@ void DownloadListHeader::customResizeSections() for (int idx = 0; idx <= rightVisible; idx++) { if (!isSectionHidden(idx)) { if (length() != width()) - resizeSection(idx, std::max(sectionSize(idx) + width() - length(), minimumSectionSize())); + resizeSection(idx, std::max(sectionSize(idx) + width() - length(), + minimumSectionSize())); else break; } @@ -107,14 +109,13 @@ void DownloadListHeader::customResizeSections() } } -void DownloadListHeader::mouseReleaseEvent(QMouseEvent *event) +void DownloadListHeader::mouseReleaseEvent(QMouseEvent* event) { QHeaderView::mouseReleaseEvent(event); customResizeSections(); } -DownloadListView::DownloadListView(QWidget *parent) - : QTreeView(parent) +DownloadListView::DownloadListView(QWidget* parent) : QTreeView(parent) { setHeader(new DownloadListHeader(Qt::Horizontal, this)); @@ -130,16 +131,17 @@ DownloadListView::DownloadListView(QWidget *parent) setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); sortByColumn(1, Qt::DescendingOrder); - connect(header(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onHeaderCustomContextMenu(QPoint))); - connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onDoubleClick(QModelIndex))); - connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onCustomContextMenu(QPoint))); + connect(header(), SIGNAL(customContextMenuRequested(QPoint)), this, + SLOT(onHeaderCustomContextMenu(QPoint))); + connect(this, SIGNAL(doubleClicked(QModelIndex)), this, + SLOT(onDoubleClick(QModelIndex))); + connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, + SLOT(onCustomContextMenu(QPoint))); } -DownloadListView::~DownloadListView() -{ -} +DownloadListView::~DownloadListView() {} -void DownloadListView::setManager(DownloadManager *manager) +void DownloadListView::setManager(DownloadManager* manager) { m_Manager = manager; @@ -154,33 +156,34 @@ void DownloadListView::setManager(DownloadManager *manager) header()->hideSection(DownloadList::COL_SOURCEGAME); } -void DownloadListView::setSourceModel(DownloadList *sourceModel) +void DownloadListView::setSourceModel(DownloadList* sourceModel) { m_SourceModel = sourceModel; } -void DownloadListView::onDoubleClick(const QModelIndex &index) +void DownloadListView::onDoubleClick(const QModelIndex& index) { - QModelIndex sourceIndex = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index); + QModelIndex sourceIndex = + qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index); if (m_Manager->getState(sourceIndex.row()) >= DownloadManager::STATE_READY) emit installDownload(sourceIndex.row()); - else if ((m_Manager->getState(sourceIndex.row()) == DownloadManager::STATE_PAUSED) - || (m_Manager->getState(sourceIndex.row()) == DownloadManager::STATE_PAUSING)) + else if ((m_Manager->getState(sourceIndex.row()) == DownloadManager::STATE_PAUSED) || + (m_Manager->getState(sourceIndex.row()) == DownloadManager::STATE_PAUSING)) emit resumeDownload(sourceIndex.row()); } -void DownloadListView::onHeaderCustomContextMenu(const QPoint &point) +void DownloadListView::onHeaderCustomContextMenu(const QPoint& point) { QMenu menu; // display a list of all headers as checkboxes - QAbstractItemModel *model = header()->model(); + QAbstractItemModel* model = header()->model(); for (int i = 1; i < model->columnCount(); ++i) { - QString columnName = model->headerData(i, Qt::Horizontal).toString(); - QCheckBox *checkBox = new QCheckBox(&menu); + QString columnName = model->headerData(i, Qt::Horizontal).toString(); + QCheckBox* checkBox = new QCheckBox(&menu); checkBox->setText(columnName); checkBox->setChecked(!header()->isSectionHidden(i)); - QWidgetAction *checkableAction = new QWidgetAction(&menu); + QWidgetAction* checkableAction = new QWidgetAction(&menu); checkableAction->setDefaultWidget(checkBox); menu.addAction(checkableAction); } @@ -189,10 +192,11 @@ void DownloadListView::onHeaderCustomContextMenu(const QPoint &point) // view/hide columns depending on check-state int i = 1; - for (const QAction *action : menu.actions()) { - const QWidgetAction *widgetAction = qobject_cast<const QWidgetAction*>(action); + for (const QAction* action : menu.actions()) { + const QWidgetAction* widgetAction = qobject_cast<const QWidgetAction*>(action); if (widgetAction != nullptr) { - const QCheckBox *checkBox = qobject_cast<const QCheckBox*>(widgetAction->defaultWidget()); + const QCheckBox* checkBox = + qobject_cast<const QCheckBox*>(widgetAction->defaultWidget()); if (checkBox != nullptr) { header()->setSectionHidden(i, !checkBox->isChecked()); } @@ -203,73 +207,117 @@ void DownloadListView::onHeaderCustomContextMenu(const QPoint &point) qobject_cast<DownloadListHeader*>(header())->customResizeSections(); } -void DownloadListView::resizeEvent(QResizeEvent *event) +void DownloadListView::resizeEvent(QResizeEvent* event) { QTreeView::resizeEvent(event); qobject_cast<DownloadListHeader*>(header())->customResizeSections(); } -void DownloadListView::onCustomContextMenu(const QPoint &point) +void DownloadListView::onCustomContextMenu(const QPoint& point) { QMenu menu(this); QModelIndex index = indexAt(point); - bool hidden = false; + bool hidden = false; try { if (index.row() >= 0) { - const int row = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index).row(); + const int row = + qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index).row(); DownloadManager::DownloadState state = m_Manager->getState(row); hidden = m_Manager->isHidden(row); if (state >= DownloadManager::STATE_READY) { - menu.addAction(tr("Install"), [=] { issueInstall(row); }); + menu.addAction(tr("Install"), [=] { + issueInstall(row); + }); if (m_Manager->isInfoIncomplete(row)) - menu.addAction(tr("Query Info"), [=] { issueQueryInfoMd5(row); }); + menu.addAction(tr("Query Info"), [=] { + issueQueryInfoMd5(row); + }); else - 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.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..."), [=] { issueDelete(row); }); + menu.addAction(tr("Delete..."), [=] { + issueDelete(row); + }); if (hidden) - menu.addAction(tr("Un-Hide"), [=] { issueRestoreToView(row); }); + menu.addAction(tr("Un-Hide"), [=] { + issueRestoreToView(row); + }); else - menu.addAction(tr("Hide"), [=] { issueRemoveFromView(row); }); + menu.addAction(tr("Hide"), [=] { + issueRemoveFromView(row); + }); } else if (state == DownloadManager::STATE_DOWNLOADING) { - 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.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..."), [=] { issueDeleteCompleted(); }); - menu.addAction(tr("Delete Uninstalled Downloads..."), [=] { issueDeleteUninstalled(); }); - menu.addAction(tr("Delete All Downloads..."), [=] { 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..."), [=] { issueRemoveFromViewCompleted(); }); - menu.addAction(tr("Hide Uninstalled..."), [=] { issueRemoveFromViewUninstalled(); }); - menu.addAction(tr("Hide All..."), [=] { 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..."), [=] { issueRestoreToViewAll(); } ); + menu.addAction(tr("Un-Hide All..."), [=] { + issueRestoreToViewAll(); + }); } menu.exec(viewport()->mapToGlobal(point)); @@ -278,31 +326,28 @@ void DownloadListView::onCustomContextMenu(const QPoint &point) void DownloadListView::keyPressEvent(QKeyEvent* event) { if (selectionModel()->hasSelection()) { - const int row = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(currentIndex()).row(); + 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) { + } else if (event->key() == Qt::Key_Delete) { issueDelete(row); } - } - else if (state == DownloadManager::STATE_DOWNLOADING) { + } else if (state == DownloadManager::STATE_DOWNLOADING) { if (event->key() == Qt::Key_Delete) { issueCancel(row); - } - else if (event->key() == Qt::Key_Space) { + } else if (event->key() == Qt::Key_Space) { issuePause(event->key()); } - } - else if (state == DownloadManager::STATE_PAUSED - || state == DownloadManager::STATE_ERROR - || state == DownloadManager::STATE_PAUSING) { + } 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) { + } else if (event->key() == Qt::Key_Space) { issueResume(row); } } @@ -328,12 +373,12 @@ void DownloadListView::issueQueryInfoMd5(int index) 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(index)) - .icon(QMessageBox::Question) - .button({tr("Move to the Recycle Bin"), QMessageBox::Yes}) - .button({tr("Cancel"), QMessageBox::Cancel}) - .exec(); + .main("Are you sure you want to delete this download?") + .content(m_Manager->getFilePath(index)) + .icon(QMessageBox::Question) + .button({tr("Move to the Recycle Bin"), QMessageBox::Yes}) + .button({tr("Cancel"), QMessageBox::Cancel}) + .exec(); if (r != QMessageBox::Yes) { return; @@ -350,17 +395,17 @@ void DownloadListView::issueRemoveFromView(int index) void DownloadListView::issueRestoreToView(int index) { - emit restoreDownload(index); + emit restoreDownload(index); } void DownloadListView::issueRestoreToViewAll() { - emit restoreDownload(-1); + emit restoreDownload(-1); } void DownloadListView::issueVisitOnNexus(int index) { - emit visitOnNexus(index); + emit visitOnNexus(index); } void DownloadListView::issueOpenFile(int index) @@ -368,7 +413,8 @@ void DownloadListView::issueOpenFile(int index) emit openFile(index); } -void DownloadListView::issueOpenMetaFile(int index) { +void DownloadListView::issueOpenMetaFile(int index) +{ emit openMetaFile(index); } @@ -394,27 +440,33 @@ void DownloadListView::issueResume(int index) 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?"), - QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { + 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?"), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { emit removeDownload(-1, true); } } 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?"), - QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { + 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?"), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { emit removeDownload(-2, true); } } 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?"), - QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { + 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?"), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { emit removeDownload(-3, true); } } @@ -422,7 +474,8 @@ void DownloadListView::issueDeleteUninstalled() void DownloadListView::issueRemoveFromViewAll() { if (QMessageBox::question(nullptr, tr("Hide Files?"), - tr("This will remove all finished downloads from this list (but NOT from disk)."), + tr("This will remove all finished downloads from this list " + "(but NOT from disk)."), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { emit removeDownload(-1, false); } @@ -431,7 +484,8 @@ void DownloadListView::issueRemoveFromViewAll() void DownloadListView::issueRemoveFromViewCompleted() { if (QMessageBox::question(nullptr, tr("Hide Files?"), - tr("This will remove all installed downloads from this list (but NOT from disk)."), + tr("This will remove all installed downloads from this " + "list (but NOT from disk)."), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { emit removeDownload(-2, false); } @@ -440,7 +494,8 @@ void DownloadListView::issueRemoveFromViewCompleted() void DownloadListView::issueRemoveFromViewUninstalled() { if (QMessageBox::question(nullptr, tr("Hide Files?"), - tr("This will remove all uninstalled downloads from this list (but NOT from disk)."), + tr("This will remove all uninstalled downloads from this " + "list (but NOT from disk)."), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { emit removeDownload(-3, false); } |
