1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.
This file is part of Mod Organizer.
Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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 "downloadlistview.h"
#include "downloadlist.h"
#include <QApplication>
#include <QCheckBox>
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPainter>
#include <QSortFilterProxyModel>
#include <QWidgetAction>
#include <log.h>
#include <report.h>
using namespace MOBase;
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
{
QModelIndex sourceIndex;
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) {
QProgressBar progressBar;
progressBar.setProperty("downloadView", option.widget->property("downloadView"));
progressBar.setProperty("downloadProgress", true);
progressBar.resize(option.rect.width(), option.rect.height());
progressBar.setTextVisible(true);
progressBar.setAlignment(Qt::AlignCenter);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setValue(m_Manager->getProgress(sourceIndex.row()).first);
progressBar.setFormat(m_Manager->getProgress(sourceIndex.row()).second);
progressBar.setStyle(QApplication::style());
// paint the background with default delegate first to preserve table cell styling
QStyledItemDelegate::paint(painter, option, index);
painter->save();
painter->translate(option.rect.topLeft());
progressBar.render(painter);
painter->restore();
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
void DownloadListHeader::customResizeSections()
{
// find the rightmost column that is not hidden
int rightVisible = count() - 1;
while (isSectionHidden(rightVisible) && rightVisible > 0)
rightVisible--;
// if that column is already squashed, squash others to the right side --
// otherwise to the left
if (sectionSize(rightVisible) == minimumSectionSize()) {
for (int idx = rightVisible; idx >= 0; idx--) {
if (!isSectionHidden(idx)) {
if (length() != width())
resizeSection(idx, std::max(sectionSize(idx) + width() - length(),
minimumSectionSize()));
else
break;
}
}
} else {
for (int idx = 0; idx <= rightVisible; idx++) {
if (!isSectionHidden(idx)) {
if (length() != width())
resizeSection(idx, std::max(sectionSize(idx) + width() - length(),
minimumSectionSize()));
else
break;
}
}
}
}
void DownloadListHeader::mouseReleaseEvent(QMouseEvent* event)
{
QHeaderView::mouseReleaseEvent(event);
customResizeSections();
}
DownloadListView::DownloadListView(QWidget* parent) : QTreeView(parent)
{
setHeader(new DownloadListHeader(Qt::Horizontal, this));
header()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
header()->setSectionsMovable(true);
header()->setContextMenuPolicy(Qt::CustomContextMenu);
header()->setCascadingSectionResizes(true);
header()->setStretchLastSection(false);
header()->setSectionResizeMode(QHeaderView::Interactive);
header()->setDefaultSectionSize(100);
setUniformRowHeights(true);
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)));
}
DownloadListView::~DownloadListView() {}
void DownloadListView::setManager(DownloadManager* manager)
{
m_Manager = manager;
// hide these columns by default
//
// note that this is overridden by the ini if MO has been started at least
// once before, which is handled in MainWindow::processUpdates() for older
// versions
header()->hideSection(DownloadList::COL_MODNAME);
header()->hideSection(DownloadList::COL_VERSION);
header()->hideSection(DownloadList::COL_ID);
header()->hideSection(DownloadList::COL_SOURCEGAME);
}
void DownloadListView::setSourceModel(DownloadList* sourceModel)
{
m_SourceModel = sourceModel;
}
void DownloadListView::onDoubleClick(const QModelIndex& 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))
emit resumeDownload(sourceIndex.row());
}
void DownloadListView::onHeaderCustomContextMenu(const QPoint& point)
{
QMenu menu;
// display a list of all headers as checkboxes
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);
checkBox->setText(columnName);
checkBox->setChecked(!header()->isSectionHidden(i));
QWidgetAction* checkableAction = new QWidgetAction(&menu);
checkableAction->setDefaultWidget(checkBox);
menu.addAction(checkableAction);
}
menu.exec(header()->viewport()->mapToGlobal(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);
if (widgetAction != nullptr) {
const QCheckBox* checkBox =
qobject_cast<const QCheckBox*>(widgetAction->defaultWidget());
if (checkBox != nullptr) {
header()->setSectionHidden(i, !checkBox->isChecked());
}
}
++i;
}
qobject_cast<DownloadListHeader*>(header())->customResizeSections();
}
void DownloadListView::resizeEvent(QResizeEvent* event)
{
QTreeView::resizeEvent(event);
qobject_cast<DownloadListHeader*>(header())->customResizeSections();
}
void DownloadListView::onCustomContextMenu(const QPoint& point)
{
QMenu menu(this);
QModelIndex index = indexAt(point);
bool hidden = false;
try {
if (index.row() >= 0) {
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);
});
if (m_Manager->isInfoIncomplete(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.addSeparator();
menu.addAction(tr("Delete..."), [=] {
issueDelete(row);
});
if (hidden)
menu.addAction(tr("Un-Hide"), [=] {
issueRestoreToView(row);
});
else
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.addSeparator();
}
} 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.addSeparator();
if (!hidden) {
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.exec(viewport()->mapToGlobal(point));
}
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(index);
}
void DownloadListView::issueQueryInfo(int index)
{
emit queryInfo(index);
}
void DownloadListView::issueQueryInfoMd5(int index)
{
emit queryInfoMd5(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();
if (r != QMessageBox::Yes) {
return;
}
emit removeDownload(index, true);
}
void DownloadListView::issueRemoveFromView(int index)
{
log::debug("removing from view: {}", index);
emit removeDownload(index, false);
}
void DownloadListView::issueRestoreToView(int index)
{
emit restoreDownload(index);
}
void DownloadListView::issueRestoreToViewAll()
{
emit restoreDownload(-1);
}
void DownloadListView::issueVisitOnNexus(int index)
{
emit visitOnNexus(index);
}
void DownloadListView::issueOpenFile(int index)
{
emit openFile(index);
}
void DownloadListView::issueOpenMetaFile(int index)
{
emit openMetaFile(index);
}
void DownloadListView::issueOpenInDownloadsFolder(int index)
{
emit openInDownloadsFolder(index);
}
void DownloadListView::issueCancel(int index)
{
emit cancelDownload(index);
}
void DownloadListView::issuePause(int index)
{
emit pauseDownload(index);
}
void DownloadListView::issueResume(int index)
{
emit resumeDownload(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) {
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) {
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) {
emit removeDownload(-3, true);
}
}
void DownloadListView::issueRemoveFromViewAll()
{
if (QMessageBox::question(nullptr, tr("Hide Files?"),
tr("This will remove all finished downloads from this list "
"(but NOT from disk)."),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
emit removeDownload(-1, false);
}
}
void DownloadListView::issueRemoveFromViewCompleted()
{
if (QMessageBox::question(nullptr, tr("Hide Files?"),
tr("This will remove all installed downloads from this "
"list (but NOT from disk)."),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
emit removeDownload(-2, false);
}
}
void DownloadListView::issueRemoveFromViewUninstalled()
{
if (QMessageBox::question(nullptr, tr("Hide Files?"),
tr("This will remove all uninstalled downloads from this "
"list (but NOT from disk)."),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
emit removeDownload(-3, false);
}
}
|