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
/*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QHeaderView>
#include <QStyleOptionProgressBarV2>
#include <QApplication>
#include "UBDownloadWidget.h"
#include "core/UBApplication.h"
#include "core/memcheck.h"
/**
* \brief Constructor
* @param parent as the parent widget
* @param name as the widget object name
*/
UBDownloadWidget::UBDownloadWidget(QWidget *parent, const char *name):QWidget(parent)
, mpLayout(NULL)
, mpBttnLayout(NULL)
, mpTree(NULL)
, mpCancelBttn(NULL)
, mpItem(NULL)
{
setObjectName(name);
setWindowTitle(tr("Downloading files"));
setAttribute(Qt::WA_StyledBackground, true);
setStyleSheet(UBApplication::globalStyleSheet());
resize(400, 300);
mpLayout = new QVBoxLayout(this);
setLayout(mpLayout);
mpTree = new QTreeWidget(this);
mpTree->setRootIsDecorated(false);
mpTree->setColumnCount(2);
mpTree->header()->setStretchLastSection(false);
mpTree->header()->setResizeMode(eItemColumn_Desc, QHeaderView::Stretch);
mpTree->header()->setResizeMode(eItemColumn_Close, QHeaderView::Custom);
mpTree->resizeColumnToContents(eItemColumn_Close);
mpTree->header()->close();
mpLayout->addWidget(mpTree, 1);
mpBttnLayout = new QHBoxLayout();
mpBttnLayout->addStretch(1);
mpCancelBttn = new QPushButton(tr("Cancel"), this);
mpCancelBttn->setObjectName("DockPaletteWidgetButton");
mpBttnLayout->addWidget(mpCancelBttn, 0);
mpLayout->addLayout(mpBttnLayout);
connect(UBDownloadManager::downloadManager(), SIGNAL(fileAddedToDownload()), this, SLOT(onFileAddedToDownload()));
connect(UBDownloadManager::downloadManager(), SIGNAL(downloadUpdated(int,qint64,qint64)), this, SLOT(onDownloadUpdated(int,qint64,qint64)));
connect(UBDownloadManager::downloadManager(), SIGNAL(downloadFinished(int)), this, SLOT(onDownloadFinished(int)));
connect(mpCancelBttn, SIGNAL(clicked()), this, SLOT(onCancelClicked()));
connect(mpTree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(onItemClicked(QTreeWidgetItem*,int)));
}
/**
* \brief Destructor
*/
UBDownloadWidget::~UBDownloadWidget()
{
if(NULL != mpCancelBttn)
{
delete mpCancelBttn;
mpCancelBttn = NULL;
}
if(NULL != mpTree)
{
delete mpTree;
mpTree = NULL;
}
if(NULL != mpBttnLayout)
{
delete mpBttnLayout;
mpBttnLayout = NULL;
}
if(NULL != mpLayout)
{
delete mpLayout;
mpLayout = NULL;
}
}
/**
* \brief Refresh the tree of downloaded files
*/
void UBDownloadWidget::onFileAddedToDownload()
{
if(NULL != mpTree)
{
mpTree->clear();
addCurrentDownloads();
addPendingDownloads();
}
}
/**
* \brief Add the current downloads
*/
void UBDownloadWidget::addCurrentDownloads()
{
QVector<sDownloadFileDesc> actualDL = UBDownloadManager::downloadManager()->currentDownloads();
qDebug() << "Actual downloads size: " << actualDL.size();
for(int i=0; i<actualDL.size();i++)
{
mpItem = new QTreeWidgetItem(mpTree);
mpItem->setText(eItemColumn_Desc, actualDL.at(i).name);
mpItem->setData(eItemColumn_Desc, Qt::UserRole, QVariant(actualDL.at(i).id));
mpItem->setIcon(eItemColumn_Close, QIcon(":images/close.svg"));
mpTree->addTopLevelItem(mpItem);
mpItem = new QTreeWidgetItem(mpTree);
mpItem->setData(eItemColumn_Desc, Qt::UserRole, actualDL.at(i).currentSize);
mpItem->setData(eItemColumn_Desc, Qt::UserRole + 1, actualDL.at(i).totalSize);
mpItem->setData(eItemColumn_Desc, Qt::UserRole + 2, actualDL.at(i).id);
mpTree->addTopLevelItem(mpItem);
mpTree->setItemDelegateForRow(((i+1)*2)-1, &mProgressBarDelegate);
}
}
/**
* \brief Add the pending downloads
*/
void UBDownloadWidget::addPendingDownloads()
{
QVector<sDownloadFileDesc> pendingDL = UBDownloadManager::downloadManager()->pendingDownloads();
for(int i=0; i<pendingDL.size(); i++)
{
mpItem = new QTreeWidgetItem(mpTree);
mpItem->setText(eItemColumn_Desc, pendingDL.at(i).name);
mpItem->setData(eItemColumn_Desc, Qt::UserRole, QVariant(pendingDL.at(i).id));
mpTree->addTopLevelItem(mpItem);
}
}
/**
* \brief Update the progress bar
* @param id as the downloaded file id
* @param crnt as the current transfered size
* @param total as the total size of the file
*/
void UBDownloadWidget::onDownloadUpdated(int id, qint64 crnt, qint64 total)
{
if(NULL != mpTree)
{
QAbstractItemModel* model = mpTree->model();
if(NULL != model)
{
for(int i=0; i< model->rowCount(); i++)
{
QModelIndex currentIndex = model->index(i, eItemColumn_Desc);
if(id == currentIndex.data(Qt::UserRole + 2))
{
// We found the right item, now we update the progress bar
model->setData(currentIndex, crnt, Qt::UserRole);
model->setData(currentIndex, total, Qt::UserRole + 1);
break;
}
}
}
}
}
/**
* \brief Handles the download finish notification
* @param id as the downloaded file id
*/
void UBDownloadWidget::onDownloadFinished(int id)
{
Q_UNUSED(id);
// Refresh the file's list
onFileAddedToDownload();
}
/**
* \brief Handles the Cancel button action
*/
void UBDownloadWidget::onCancelClicked()
{
UBDownloadManager::downloadManager()->cancelDownloads();
}
/**
* \brief Handles the item click notification
* @param pItem as the item clicked
* @param col as the column containing the item clicked
*/
void UBDownloadWidget::onItemClicked(QTreeWidgetItem *pItem, int col)
{
if( eItemColumn_Close == col
&& "" != pItem->text(eItemColumn_Desc)){
// Stop the download of the clicked item and remove it from the list
UBDownloadManager::downloadManager()->cancelDownload(pItem->data(eItemColumn_Desc, Qt::UserRole).toInt());
}
}
// ---------------------------------------------------------------------------------------------
UBDownloadProgressDelegate::UBDownloadProgressDelegate(QObject *parent):QItemDelegate(parent)
{
}
void UBDownloadProgressDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionProgressBarV2 opt;
opt.rect = option.rect;
opt.minimum = 0;
opt.maximum = index.data(Qt::UserRole + 1).toInt();
opt.progress = index.data(Qt::UserRole).toInt();
if(0 == index.column()){
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0);
}
}