Repository navigation
Expand file tree
/
Copy pathtransferpanel.cpp
More file actions
420 lines (367 loc) · 12.6 KB
/
Copy pathtransferpanel.cpp
File metadata and controls
420 lines (367 loc) · 12.6 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
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
#include "transferpanel.h"
#include <QBrush>
#include <QColor>
#include <QEasingCurve>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QIcon>
#include <QLabel>
#include <QProgressBar>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QToolButton>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QVBoxLayout>
#include "fileinfo.h"
namespace {
// "1.2 MB/s":沿用 formatFileSize 的单位和精度,和列表里的文件大小一致。
QString formatSpeed(qint64 bytesPerSecond) {
if (bytesPerSecond < 0) {
return QStringLiteral("—");
}
return TransferPanel::tr("%1/s").arg(formatFileSize(bytesPerSecond));
}
const int kCollapsedHeight = 0;
const int kAnimationMs = 200;
} // namespace
TransferPanel::TransferPanel(QWidget *parent) : QWidget(parent) {
setObjectName(QStringLiteral("transferPanel"));
// 自定义 QWidget 默认不画样式表背景,这里显式打开。
setAttribute(Qt::WA_StyledBackground, true);
initUi();
heightAnimation = new QPropertyAnimation(this, "panelHeight", this);
heightAnimation->setDuration(kAnimationMs);
heightAnimation->setEasingCurve(QEasingCurve::OutCubic);
connect(heightAnimation, &QPropertyAnimation::finished, this, [this]() {
if (!expanded) {
// 收回到 0 高度后再隐藏,避免动画结束后布局还留一条缝。
setPanelHeight(kCollapsedHeight);
hide();
}
});
setPanelHeight(kCollapsedHeight);
hide();
}
void TransferPanel::initUi() {
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
// 标题栏:和文件列表分隔开,收起按钮放在最右侧。
QWidget *header = new QWidget(this);
header->setObjectName(QStringLiteral("transferPanelHeader"));
header->setAttribute(Qt::WA_StyledBackground, true);
header->setFixedHeight(38);
QHBoxLayout *headerLayout = new QHBoxLayout(header);
headerLayout->setContentsMargins(12, 0, 8, 0);
headerLayout->setSpacing(8);
QLabel *title = new QLabel(tr("传输队列"), header);
title->setObjectName(QStringLiteral("transferPanelTitle"));
headerLayout->addWidget(title);
summaryLabel = new QLabel(header);
summaryLabel->setObjectName(QStringLiteral("transferPanelSummary"));
headerLayout->addWidget(summaryLabel);
headerLayout->addStretch(1);
collapseButton = new QToolButton(header);
collapseButton->setObjectName(QStringLiteral("transferPanelCollapseButton"));
collapseButton->setIcon(QIcon(QStringLiteral(":/icons/chevron-down.png")));
collapseButton->setIconSize(QSize(16, 16));
collapseButton->setAutoRaise(true);
collapseButton->setCursor(Qt::PointingHandCursor);
collapseButton->setToolTip(tr("收起传输队列"));
connect(collapseButton, &QToolButton::clicked, this,
&TransferPanel::collapseRequested);
headerLayout->addWidget(collapseButton);
layout->addWidget(header);
QWidget *body = new QWidget(this);
QVBoxLayout *bodyLayout = new QVBoxLayout(body);
bodyLayout->setContentsMargins(12, 8, 12, 10);
bodyLayout->setSpacing(8);
// 进度条占满整行,百分比和速率都写在进度条文字里,不再单独占一行。
progressBar = new QProgressBar(body);
progressBar->setTextVisible(true);
progressBar->setFormat(QStringLiteral("%p%"));
progressBar->setFixedHeight(16);
bodyLayout->addWidget(progressBar);
QHBoxLayout *actionLayout = new QHBoxLayout;
actionLayout->setSpacing(8);
// 传输中目录树被禁用(避免阻塞式调用和传输抢同一个会话),
// 但“添加文件”始终可用,可以继续往队列里追加。
addButton = new QPushButton(tr("添加文件"), body);
addButton->setObjectName(QStringLiteral("transferActionButton"));
addButton->setFixedHeight(28);
connect(addButton, &QPushButton::clicked, this,
&TransferPanel::addFilesRequested);
actionLayout->addWidget(addButton);
// 默认只入队不自动传,由这个按钮开闸。
startButton = new QPushButton(tr("开始上传"), body);
startButton->setObjectName(QStringLiteral("transferActionButton"));
startButton->setFixedHeight(28);
startButton->setToolTip(tr("开始传输队列中等待的文件"));
connect(startButton, &QPushButton::clicked, this,
&TransferPanel::startRequested);
actionLayout->addWidget(startButton);
actionLayout->addStretch(1);
cancelButton = new QPushButton(tr("取消"), body);
cancelButton->setObjectName(QStringLiteral("transferActionButton"));
cancelButton->setFixedHeight(28);
cancelButton->setToolTip(tr("取消当前文件,并放弃队列中其余文件"));
connect(cancelButton, &QPushButton::clicked, this,
&TransferPanel::cancelRequested);
actionLayout->addWidget(cancelButton);
clearButton = new QPushButton(tr("清空"), body);
clearButton->setObjectName(QStringLiteral("transferActionButton"));
clearButton->setFixedHeight(28);
clearButton->setToolTip(tr("清除传输记录并收起传输队列"));
connect(clearButton, &QPushButton::clicked, this,
&TransferPanel::clearRequested);
actionLayout->addWidget(clearButton);
bodyLayout->addLayout(actionLayout);
list = new QTreeWidget(body);
list->setObjectName(QStringLiteral("transferTaskList"));
list->setColumnCount(4);
list->setHeaderLabels(QStringList() << tr("文件名") << tr("大小")
<< tr("进度") << tr("状态"));
list->setRootIsDecorated(false);
list->setUniformRowHeights(true);
list->setSelectionMode(QAbstractItemView::NoSelection);
list->setAlternatingRowColors(true);
list->setMinimumHeight(70);
list->header()->setStretchLastSection(false);
list->header()->setSectionResizeMode(0, QHeaderView::Stretch);
list->setColumnWidth(1, 80);
list->setColumnWidth(2, 120);
list->setColumnWidth(3, 100);
bodyLayout->addWidget(list, 1);
layout->addWidget(body, 1);
updateButtons();
updateSummary();
}
int TransferPanel::rowCount() const { return rows.size(); }
int TransferPanel::appendRow(const TransferItem &item) {
rows.append(item);
QTreeWidgetItem *row = new QTreeWidgetItem(list);
const int index = rows.size() - 1;
updateRow(index, item);
row->setToolTip(0, item.name);
list->scrollToItem(row);
updateSummary();
return index;
}
void TransferPanel::updateRow(int row, const TransferItem &item) {
if (row < 0 || row >= rows.size() || row >= list->topLevelItemCount()) {
return;
}
rows[row] = item;
QTreeWidgetItem *rowItem = list->topLevelItem(row);
rowItem->setText(0, item.name);
rowItem->setText(1, item.totalSize > 0 ? formatFileSize(item.totalSize)
: QStringLiteral("-"));
QString progressText = QStringLiteral("-");
QString stateText;
switch (item.state) {
case TransferItem::Running:
if (item.totalSize > 0) {
const int percent =
int(qMin<qint64>(100, item.doneSize * 100 / item.totalSize));
// 总大小已经单独占一列,这里只显示百分比和已传字节,窄面板下不会被截断。
progressText = QStringLiteral("%1% (%2)")
.arg(percent)
.arg(formatFileSize(item.doneSize));
} else {
progressText = formatFileSize(item.doneSize);
}
stateText = item.upload ? tr("上传中") : tr("下载中");
break;
case TransferItem::Done:
progressText = QStringLiteral("100%");
stateText = tr("完成");
break;
case TransferItem::Failed:
stateText = item.message.isEmpty() ? tr("失败")
: tr("失败: %1").arg(item.message);
break;
case TransferItem::Cancelled:
stateText = tr("已取消");
break;
case TransferItem::Pending:
default:
stateText = tr("等待中");
break;
}
rowItem->setText(2, progressText);
rowItem->setText(3, stateText);
// 状态列可能被省略号截断,悬停时显示完整原因。
rowItem->setToolTip(3, stateText);
// 失败标红、完成标绿,一眼能扫到异常。
QBrush brush = list->palette().text();
if (item.state == TransferItem::Failed) {
brush = QBrush(QColor(0xd9, 0x53, 0x4f));
} else if (item.state == TransferItem::Done) {
brush = QBrush(QColor(0x5c, 0xb8, 0x5c));
}
rowItem->setForeground(3, brush);
updateSummary();
}
void TransferPanel::clearRows() {
rows.clear();
list->clear();
updateSummary();
}
void TransferPanel::updateBarText() {
QString text = progressText;
if (!speedText.isEmpty()) {
if (!text.isEmpty()) {
text += QStringLiteral(" · ");
}
text += speedText;
}
progressBar->setFormat(text);
}
void TransferPanel::setOverallProgress(qint64 doneSize, qint64 totalSize) {
if (totalSize <= 0) {
progressBar->setValue(0);
progressText = QStringLiteral("%p%");
updateBarText();
return;
}
const int percent =
int(qMin(100.0, double(doneSize) * 100.0 / double(totalSize)));
progressBar->setValue(percent);
progressText = QStringLiteral("%1% (%2 / %3)")
.arg(percent)
.arg(formatFileSize(doneSize),
formatFileSize(totalSize));
updateBarText();
}
void TransferPanel::setSpeed(qint64 bytesPerSecond, bool known, bool upload) {
if (!known) {
speedText = upload ? tr("↑ 计算中…") : tr("↓ 计算中…");
updateBarText();
return;
}
const QString arrow = upload ? QStringLiteral("↑") : QStringLiteral("↓");
speedText = tr("%1 %2").arg(arrow, formatSpeed(bytesPerSecond));
updateBarText();
}
void TransferPanel::setTransferring(bool transferring) {
this->transferring = transferring;
if (transferring) {
cancelPending = false;
}
updateButtons();
updateSummary();
}
void TransferPanel::setStartEnabled(bool enabled) {
startEnabled = enabled;
updateButtons();
}
void TransferPanel::clearSpeed() {
speedText.clear();
updateBarText();
}
void TransferPanel::setCancelPending(bool pending) {
cancelPending = pending;
updateButtons();
}
void TransferPanel::updateButtons() {
cancelButton->setEnabled(transferring && !cancelPending);
cancelButton->setText(cancelPending ? tr("取消中…") : tr("取消"));
clearButton->setEnabled(!transferring);
addButton->setEnabled(true);
startButton->setEnabled(startEnabled && !transferring);
}
void TransferPanel::updateSummary() {
if (rows.isEmpty()) {
if (summaryLabel->text() != tr("暂无传输任务")) {
summaryLabel->setText(tr("暂无传输任务"));
}
return;
}
int running = 0;
int pending = 0;
int done = 0;
int failed = 0;
int cancelled = 0;
for (const TransferItem &item : rows) {
switch (item.state) {
case TransferItem::Running:
++running;
break;
case TransferItem::Done:
++done;
break;
case TransferItem::Failed:
++failed;
break;
case TransferItem::Cancelled:
++cancelled;
break;
case TransferItem::Pending:
default:
++pending;
break;
}
}
QStringList parts;
parts << tr("%1 个任务").arg(rows.size());
if (running > 0) {
parts << tr("进行中 %1").arg(running);
}
if (pending > 0) {
parts << tr("等待 %1").arg(pending);
}
if (done > 0) {
parts << tr("完成 %1").arg(done);
}
if (failed > 0) {
parts << tr("失败 %1").arg(failed);
}
if (cancelled > 0) {
parts << tr("取消 %1").arg(cancelled);
}
const QString text = parts.join(QStringLiteral(" · "));
if (summaryLabel->text() != text) {
summaryLabel->setText(text);
}
}
void TransferPanel::setExpanded(bool expand, bool animate) {
if (expanded == expand && (expand ? isVisible() : !isVisible())) {
return;
}
expanded = expand;
heightAnimation->stop();
if (expand) {
show();
const int target = targetHeight();
if (!animate) {
setPanelHeight(target);
return;
}
heightAnimation->setStartValue(panelHeight());
heightAnimation->setEndValue(target);
heightAnimation->start();
return;
}
if (!isVisible()) {
setPanelHeight(kCollapsedHeight);
return;
}
if (!animate) {
setPanelHeight(kCollapsedHeight);
hide();
return;
}
heightAnimation->setStartValue(panelHeight());
heightAnimation->setEndValue(kCollapsedHeight);
heightAnimation->start();
}
int TransferPanel::panelHeight() const { return height(); }
void TransferPanel::setPanelHeight(int value) {
setFixedHeight(qMax(0, value));
}
int TransferPanel::targetHeight() const {
const int available = parentWidget() ? parentWidget()->height() : 640;
// 大约占文件管理区域的 40%,同时保证任务列表至少有几十像素可见。
return qBound(200, available * 2 / 5, 340);
}