-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletelistmodel.cpp
110 lines (89 loc) · 2.28 KB
/
completelistmodel.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
#include "completelistmodel.h"
CompleteListModel* CompleteListModel::pCompleteListModel = nullptr;
CompleteListModel::CompleteListModel(QObject *parent)
: QAbstractListModel(parent)
{
pCompleteListModel = this;
m_completeCount = "0";
}
int CompleteListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_completeList.size();
}
QVariant CompleteListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
QJsonObject item = m_completeList.at(index.row());
switch (role) {
case FILE_NAME:
{
return item.value("fileName").toString();
}
break;
case FILE_SIZE:
{
return item.value("fileSizeFormat").toString();
}
break;
case FILE_ICON:
{
return item.value("fileIcon").toString();
}
break;
case FILE_STATUS:
{
return item.value("fileStatus").toString();
}
break;
case FILE_PATH:
{
return item.value("localPath").toString();
}
break;
default:
break;
}
return QVariant();
}
QHash<int, QByteArray> CompleteListModel::roleNames() const
{
QHash<int, QByteArray> rolesHash;
rolesHash.insert(FILE_NAME, "fileName");
rolesHash.insert(FILE_SIZE, "fileSize");
rolesHash.insert(FILE_ICON, "fileIcon");
rolesHash.insert(FILE_STATUS, "fileStatus");
rolesHash.insert(FILE_PATH, "localPath");
return rolesHash;
}
void CompleteListModel::addCompleteItem(const QJsonObject& item)
{
m_completeList.push_back(item);
beginResetModel();
endResetModel();
m_completeNum = QString("(%1)").arg(m_completeList.size());
emit completeNumChanged();
m_completeCount = QString::number(m_completeList.size());
emit completeCountChanged();
}
void CompleteListModel::removeCompleteItem(int index)
{
if (m_completeList.size() <= index)
{
return;
}
m_completeList.removeAt(index);
beginResetModel();
endResetModel();
if (m_completeList.empty())
{
m_completeNum = "";
} else {
m_completeNum = QString("(%1)").arg(m_completeList.size());
}
emit completeNumChanged();
m_completeCount = QString::number(m_completeList.size());
emit completeCountChanged();
}