-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmainwidget.cpp
More file actions
315 lines (251 loc) · 8.77 KB
/
Copy pathmainwidget.cpp
File metadata and controls
315 lines (251 loc) · 8.77 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
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
#include <QListWidgetItem>
#include <QMenu>
#include <QClipboard>
#include <windows.h>
#include "mainwidget.h"
#include "ui_mainwidget.h"
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
this->setFixedSize(400, 700);
addTaskDlg = new AddTaskDialog;
network = new Network;
initSlots();
}
MainWidget::~MainWidget()
{
delete ui;
}
void MainWidget::initSlots()
{
connect(ui->addTaskButton, SIGNAL(clicked()), this, SLOT(showAddTaskDialog()));
connect(addTaskDlg, SIGNAL(downloadSettings(QString,QStringList)),
this, SLOT(handleTaskRequest(QString,QStringList)));
connect(network, SIGNAL(taskFinished(QString)), this, SLOT(downloadFinished(QString)));
connect(network, SIGNAL(fileInfo(QString,QString,qint64)), this, SLOT(taskInfo(QString,QString,qint64)));
connect(network, SIGNAL(process(QString,qint64,double,double)),
this, SLOT(showDownloadProgress(QString,qint64,double,double)));
connect(this, SIGNAL(stopDownloadTask(QString)), network, SLOT(stopTask(QString)));
connect(this, SIGNAL(startDownloadTask(QString)), network, SLOT(startTask(QString)));
connect(this, SIGNAL(removeDownloadTask(QString)), network, SLOT(deleteTask(QString)));
}
void MainWidget::updateTaskList(QStringList taskList)
{
QStringList repeat;
foreach (QString task, taskList) {
/* 跳过重复的任务 */
if (taskListCurrent.contains(task)) {
repeat.append(task);
continue;
}
network->getFileInfo2(task);
// showTask(task);
// network->addToDownloadList(task);
// if (taskListCurrent.isEmpty()) {
// network->startDownload(task, downloadPath);
// }
// taskListCurrent.append(task);
}
if (!repeat.isEmpty()) {
qDebug() << repeat << "已在任务队列中!";
}
}
void MainWidget::showTask(QString task, qint64 size)
{
QString fileName = network->getFileInfo(task);
ListWidget *widget = new ListWidget();
int index = fileName.lastIndexOf(".");
QString fileType = fileName.mid(index + 1);
if (fileType == "bat") {
widget->setHead(":/image/BAT.ico");
} else if (fileType == "zip") {
widget->setHead(":/image/ZIP.ico");
} else if (fileType == "doc" || fileType == "docx") {
widget->setHead(":/image/DOC.ico");
} else if (fileType == "mp4") {
widget->setHead(":/image/MP4.ico");
} else if (fileType == "pdf") {
widget->setHead(":/image/PDF.ico");
} else if (fileType == "rmvb") {
widget->setHead(":/image/RMVB.ico");
} else if (fileType == "txt") {
widget->setHead(":/image/TXT.ico");
} else {
widget->setHead(":/image/File.ico");
}
widget->setFileName(fileName);
widget->setFileSize(size);
widget->setSpeed(QString("等待下载"));
QListWidgetItem *item = new QListWidgetItem();
item->setSizeHint(QSize(0,50));
ui->taskListWidget->addItem(item);
ui->taskListWidget->setItemWidget(item, widget);
widgetList.append(widget);
}
void MainWidget::showAddTaskDialog()
{
addTaskDlg->show();
}
void MainWidget::handleTaskRequest(QString path, QStringList urls)
{
addTaskDlg->close();
downloadPath = path;
updateTaskList(urls);
}
void MainWidget::taskInfo(QString task, QString fileName, qint64 fileSize)
{
showTask(fileName, fileSize);
network->addToDownloadList(task);
if (taskListCurrent.isEmpty()) {
network->startDownload(task, downloadPath);
}
taskListCurrent.append(task);
}
void MainWidget::startCurrentTask()
{
int index = ui->taskListWidget->currentRow();
if (taskListCurrent.isEmpty()) {
return ;
}
QString task = taskListCurrent.at(index);
emit startDownloadTask(task);
}
void MainWidget::stopCurrentTask()
{
int index = ui->taskListWidget->currentRow();
if (taskListCurrent.isEmpty()) {
return ;
}
QString task = taskListCurrent.at(index);
emit stopDownloadTask(task);
ListWidget *widget = widgetList.at(index);
widget->setSpeed("任务暂停中。");
}
void MainWidget::removeCurrentTask()
{
int index = ui->taskListWidget->currentRow();
removeTask(index);
}
void MainWidget::restartDownload()
{
int index = ui->taskListWidget->currentRow();
if (taskListCurrent.isEmpty()) {
return ;
}
ListWidget *widget = widgetList.at(index);
/* 获取当前控件的文件 */
QString filename = widget->getFileName();
QString file = downloadPath + filename;
QFile::remove(file);
QString task = taskListCurrent.at(index);
emit startDownloadTask(task);
}
void MainWidget::removeTask(int index)
{
QListWidgetItem *item = ui->taskListWidget->takeItem(index);
delete item;
widgetList.removeAt(index);
if (taskListCurrent.isEmpty()) {
return ;
}
QString task = taskListCurrent.at(index);
taskListCurrent.removeAt(index);
emit removeDownloadTask(task);
}
void MainWidget::deleteCurrentFile()
{
int index = ui->taskListWidget->currentRow();
if (taskListCurrent.isEmpty()) {
return ;
}
ListWidget *widget = widgetList.at(index);
/* 获取当前控件的文件 */
QString filename = widget->getFileName();
QString file = downloadPath + filename;
removeTask(index);
QFile::remove(file);
}
void MainWidget::copyToClipboard()
{
QClipboard *clipboard = QApplication::clipboard();
int index = ui->taskListWidget->currentRow();
if (taskListCurrent.isEmpty()) {
return ;
}
QString address = taskListCurrent.at(index);
/* 复制下载地址到系统剪切板 */
clipboard->setText(address);
}
void MainWidget::showDownloadProgress(QString task, qint64 byteTotal, double percent, double speed)
{
QString unit;
if (speed < 1024.0) {
unit = "B/s";
} else if (speed < (1024 * 1024)) {
speed /= 1024.0;
unit = "kB/s";
} else {
speed /= 1024 * 1024;
unit = "MB/s";
}
int index = taskListCurrent.indexOf(task);
ListWidget *widget = widgetList.at(index);
QString info = QString("完成进度: %1%, 当前速度: %2%3")
.arg(percent, 2, 'f', 2)
.arg(speed, 3, 'f', 2).arg(unit);
widget->setFileSize(byteTotal);
widget->setSpeed(info);
}
void MainWidget::downloadFinished(QString task)
{
/* 获取当前任务的编号 */
int index = taskListCurrent.indexOf(task);
ListWidget *widget = widgetList.at(index);
widget->setSpeed(QString("下载完成"));
}
void MainWidget::on_taskListWidget_customContextMenuRequested(const QPoint &pos)
{
/* 判断右击处是否有item存在 */
QListWidgetItem *item = ui->taskListWidget->itemAt(pos);
if (item == NULL) {
return;
}
QMenu *menu = new QMenu;
QAction *actionStartTask = new QAction("开始任务", this);
QAction *actionStopTask = new QAction("暂停任务", this);
QAction *actionDelTask = new QAction("删除任务", this);
QAction *actionRestartDownload = new QAction("重新下载任务", this);
QAction *actionDelFile = new QAction("彻底删除任务", this);
QAction *actionCopy = new QAction("复制下载链接", this);
connect(actionStartTask, SIGNAL(triggered(bool)), this, SLOT(startCurrentTask()));
connect(actionStopTask, SIGNAL(triggered(bool)), this, SLOT(stopCurrentTask()));
connect(actionDelTask, SIGNAL(triggered(bool)), this, SLOT(removeCurrentTask()));
connect(actionRestartDownload, SIGNAL(triggered(bool)), this, SLOT(restartDownload()));
connect(actionDelFile, SIGNAL(triggered(bool)), this, SLOT(deleteCurrentFile()));
connect(actionCopy, SIGNAL(triggered(bool)), this, SLOT(copyToClipboard()));
menu->addAction(actionStartTask);
menu->addAction(actionStopTask);
menu->addAction(actionDelTask);
menu->addAction(actionRestartDownload);
menu->addAction(actionDelFile);
menu->addAction(actionCopy);
/* 在鼠标位置显示菜单 */
menu->exec(QCursor::pos());
/* 断开信号-槽 */
disconnect(actionStartTask, SIGNAL(triggered(bool)), this, SLOT(startCurrentTask()));
disconnect(actionStopTask, SIGNAL(triggered(bool)), this, SLOT(stopCurrentTask()));
disconnect(actionDelTask, SIGNAL(triggered(bool)), this, SLOT(removeCurrentTask()));
disconnect(actionRestartDownload, SIGNAL(triggered(bool)), this, SLOT(restartDownload()));
disconnect(actionDelFile, SIGNAL(triggered(bool)), this, SLOT(deleteCurrentFile()));
disconnect(actionCopy, SIGNAL(triggered(bool)), this, SLOT(copyToClipboard()));
/* 释放资源 */
delete actionStartTask;
delete actionStopTask;
delete actionDelTask;
delete actionRestartDownload;
delete actionDelFile;
delete actionCopy;
delete menu;
}