-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogressbardelegate.cpp
More file actions
103 lines (87 loc) · 3.85 KB
/
progressbardelegate.cpp
File metadata and controls
103 lines (87 loc) · 3.85 KB
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
#include <QApplication>
#include <QModelIndex>
#include <QPainter>
#include <QPen>
#include <QRect>
#include <QSize>
#include <QString>
#include <QStyle>
#include <QStyleOptionProgressBar>
#include <QStyleOptionViewItem>
#include "common.hpp"
#include "httpdownload.hpp"
#include "progressbardelegate.hpp"
namespace {
template <class NumberType>
QString formatNumberMB(const NumberType number,
const vfg::format::SuffixOption suffixOpt = vfg::format::SuffixOption::IncludeSuffix) {
if(number < 1000000) {
return vfg::format::formatNumber(number / 1000.0, suffixOpt);
}
return vfg::format::formatNumber(number, suffixOpt);
}
} // namespace
namespace vfg {
namespace ui {
ProgressBarDelegate::ProgressBarDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
auto rect = [&option](const int left, const int top, const int width = -1, const int height = -1){
return option.rect.adjusted(left, top, width, height);
};
auto dl = index.model()->data(index.model()->index(index.row(), index.column()))
.value<std::shared_ptr<vfg::net::HttpDownload>>();
// #000000 (black)
painter->setPen(QPen(QColor::fromRgb(0, 0, 0), 1, Qt::SolidLine));
painter->drawText(rect(5, 5), Qt::AlignLeft, dl->fileName());
// #4D4D4D (gray)
painter->setPen(QPen(QColor::fromRgb(77, 77, 77), 1, Qt::SolidLine));
if(dl->getStatus() == vfg::net::HttpDownload::Status::Finished) {
painter->drawText(rect(5, 25), Qt::AlignLeft,
QString("%1 - %2").arg(vfg::format::formatNumber(dl->bytesTotal()))
.arg(dl->url().host()));
}
else if(dl->getStatus() == vfg::net::HttpDownload::Status::Aborted) {
painter->drawText(rect(5, 25), Qt::AlignLeft,
QString("Canceled - %1").arg(dl->url().host()));
}
else if(dl->getStatus() == vfg::net::HttpDownload::Status::Running) {
QStyleOptionProgressBar progressBarOption;
progressBarOption.state = QStyle::State_Enabled;
progressBarOption.direction = QApplication::layoutDirection();
progressBarOption.rect = rect(5, 25, -5);
progressBarOption.fontMetrics = QApplication::fontMetrics();
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
QString text;
if(dl->sizeKnown()) {
progressBarOption.progress = dl->percentCompleted();
text = QString("%4: %1 of %2 (%3/sec)").arg(formatNumberMB(dl->bytesDownloaded(), vfg::format::SuffixOption::ExcludeSuffix))
.arg(vfg::format::formatNumber(dl->bytesTotal())).arg(vfg::format::formatNumber(dl->downloadSpeed()))
.arg(dl->url().host());
}
else {
// If the download size is not known, display infinite progress bar
progressBarOption.maximum = 0;
text = QString("%3: %1 (%2/sec)").arg(vfg::format::formatNumber(dl->bytesDownloaded()))
.arg(vfg::format::formatNumber(dl->downloadSpeed())).arg(dl->url().host());
}
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
painter->drawText(rect(10, 45), Qt::AlignLeft, text);
}
else if(dl->getStatus() == vfg::net::HttpDownload::Status::Pending) {
painter->drawText(rect(5, 25), Qt::AlignLeft,
QString("Pending - %1").arg(dl->url().host()));
}
}
QSize ProgressBarDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
return QSize(QStyledItemDelegate::sizeHint(option, index).width(), height);
}
} // namespace ui
} // namespace vfg