-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadlistmodel.cpp
145 lines (124 loc) · 3.34 KB
/
uploadlistmodel.cpp
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
#include "uploadlistmodel.h"
#include "completelistmodel.h"
UploadListModel* UploadListModel::pUploadListModel = nullptr;
UploadListModel::UploadListModel(QObject *parent)
: QAbstractListModel(parent)
{
pUploadListModel = this;
}
int UploadListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_uploadFileList.size();
}
QVariant UploadListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
QJsonObject item = m_uploadFileList.at(index.row());
switch (role) {
case FILE_NAME:
{
return item.value("fileName").toString();
}
break;
case FILE_SIZE:
{
return item.value("fileSize").toInt();
}
break;
case FILE_ICON:
{
return item.value("fileIcon").toString();
}
break;
case FILE_SIZE_FORMAT:
{
return item.value("fileSizeFormat").toString();
}
break;
case FILE_UP_SIZE:
{
return item.value("fileTransfer").toString();
}
break;
case UPLOAD_SPEED:
{
return item.value("uploadSpeed").toString();
}
break;
case UPLOAD_PROGRESS:
{
return item.value("progressVal").toInt();
}
break;
case LOCAL_PATH:
{
return item.value("localPath").toString();
}
break;
default:
break;
}
return QVariant();
}
QHash<int,QByteArray> UploadListModel::roleNames() const
{
QHash<int,QByteArray> rolesHash;
rolesHash.insert(FILE_NAME, "fileName");
rolesHash.insert(FILE_SIZE, "fileSize");
rolesHash.insert(FILE_ICON, "fileIcon");
rolesHash.insert(FILE_UP_SIZE, "fileUpSize");
rolesHash.insert(FILE_SIZE_FORMAT, "fileSizeFormat");
rolesHash.insert(UPLOAD_SPEED, "uploadSpeed");
rolesHash.insert(UPLOAD_PROGRESS, "progressVal");
rolesHash.insert(LOCAL_PATH, "localPath");
return rolesHash;
}
void UploadListModel::addUploadList(const QJsonObject& fileItem)
{
m_uploadFileList.push_back(fileItem);
beginResetModel();
endResetModel();
m_uploadNum = QString("(%1)").arg(m_uploadFileList.size());
emit uploadNumChanged();
}
void UploadListModel::updateProgress(double uNow, double uTotal,
const QString& speedVal, const QString& upSize)
{
if (m_uploadFileList.empty())
{
return;
}
m_uploadFileList[0].insert("fileSize", uTotal);
m_uploadFileList[0].insert("progressVal", uNow);
m_uploadFileList[0].insert("uploadSpeed", speedVal);
m_uploadFileList[0].insert("fileTransfer", upSize);
beginResetModel();
endResetModel();
}
void UploadListModel::removeItem(int index, bool uploadSuccess, bool cancel)
{
if (m_uploadFileList.size() <= index)
{
return;
}
QJsonObject item = m_uploadFileList.takeAt(index);
uploadSuccess ? item.insert("fileStatus", u8"上传完成") :
item.insert("fileStatus", u8"上传失败");
//添加到完成列表
if (!cancel && CompleteListModel::pCompleteListModel != nullptr)
{
CompleteListModel::pCompleteListModel->addCompleteItem(item);
}
beginResetModel();
endResetModel();
if (m_uploadFileList.empty())
{
m_uploadNum = "";
} else {
m_uploadNum = QString("(%1)").arg(m_uploadFileList.size());
}
emit uploadNumChanged();
}