-
Notifications
You must be signed in to change notification settings - Fork 4
/
resulttabletmodel.cpp
261 lines (225 loc) · 5.3 KB
/
resulttabletmodel.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
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
#include "StdAfx.h"
#include "resulttabletmodel.h"
bool IDLessThan(const KadResult &result1, const KadResult &result2)
{
return result1.Index < result2.Index;
}
bool IDGreaterThan(const KadResult &result1, const KadResult &result2)
{
return result1.Index > result2.Index;
}
bool NameLessThan(const KadResult &result1, const KadResult &result2)
{
return result1.Name < result2.Name;
}
bool NameGreaterThan(const KadResult &result1, const KadResult &result2)
{
return result1.Name > result2.Name;
}
bool SizeLessThan(const KadResult &result1, const KadResult &result2)
{
return result1.Size < result2.Size;
}
bool SizeGreaterThan(const KadResult &result1, const KadResult &result2)
{
return result1.Size > result2.Size;
}
bool TypeLessThan(const KadResult &result1, const KadResult &result2)
{
return result1.Type < result2.Type;
}
bool TypeGreaterThan(const KadResult &result1, const KadResult &result2)
{
return result1.Type > result2.Type;
}
bool ResourceLessThan(const KadResult &result1, const KadResult &result2)
{
return result1.Resource < result2.Resource;
}
bool ResourceGreaterThan(const KadResult &result1, const KadResult &result2)
{
return result1.Resource > result2.Resource;
}
ResultTableModel::ResultTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
ResultTableModel::~ResultTableModel()
{
}
void ResultTableModel::sort(int column, Qt::SortOrder order)
{
if(column == COLUMN_ID)
{
qSort(m_resultList.begin(), m_resultList.end(), order == Qt::AscendingOrder ? IDLessThan : IDGreaterThan);
}
else if(column == COLUMN_NAME)
{
qSort(m_resultList.begin(), m_resultList.end(), order == Qt::AscendingOrder ? NameLessThan : NameGreaterThan);
}
else if(column == COLUMN_SIZE)
{
qSort(m_resultList.begin(), m_resultList.end(), order == Qt::AscendingOrder ? SizeLessThan : SizeGreaterThan);
}
else if(column == COLUMN_TYPE)
{
qSort(m_resultList.begin(), m_resultList.end(), order == Qt::AscendingOrder ? TypeLessThan : TypeGreaterThan);
}
else if(column == COLUMN_RESOURCE)
{
qSort(m_resultList.begin(), m_resultList.end(), order == Qt::AscendingOrder ? ResourceLessThan : ResourceGreaterThan);
}
else if(column == COLUMN_OPERATE)
{
//return "操作";
}
emit layoutChanged();
}
QVariant ResultTableModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int column = index.column();
if(role == Qt::DisplayRole)
{
if(column == COLUMN_ID)
{
return m_resultList[row].Index;
}
else if(column == COLUMN_NAME)
{
return m_resultList[row].Name;
}
else if(column == COLUMN_SIZE)
{
return GetFileSize(m_resultList[row].Size);
}
else if(column == COLUMN_TYPE)
{
return m_resultList[row].Type;
}
else if(column == COLUMN_RESOURCE)
{
return m_resultList[row].Resource;
}
else if(column == COLUMN_OPERATE)
{
//return "操作";
}
}
else if(role == Qt::EditRole)
{
if(column == COLUMN_ID)
{
return m_resultList[row].Index;
}
else if(column == COLUMN_NAME)
{
return m_resultList[row].Name;
}
else if(column == COLUMN_SIZE)
{
return m_resultList[row].Size;
}
else if(column == COLUMN_TYPE)
{
return m_resultList[row].Type;
}
else if(column == COLUMN_RESOURCE)
{
return m_resultList[row].Resource;
}
else if(column == COLUMN_OPERATE)
{
//return "操作";
}
}
return QVariant();
}
QVariant ResultTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::DisplayRole)
{
if(orientation == Qt::Horizontal)
{
if(section == COLUMN_ID)
{
return "编号";
}
else if(section == COLUMN_NAME)
{
return "名称";
}
else if(section == COLUMN_SIZE)
{
return "大小";
}
else if(section == COLUMN_TYPE)
{
return "格式";
}
else if(section == COLUMN_RESOURCE)
{
return "热度";
}
else if(section == COLUMN_OPERATE)
{
return "操作";
}
}
}
return QVariant();
}
int ResultTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_resultList.count();
}
int ResultTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return COLUMN_OPERATE + 1;
}
Qt::ItemFlags ResultTableModel::flags(const QModelIndex &index) const
{
Q_UNUSED(index);
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
bool ResultTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
//beginInsertRows()通知其他组件行数将会改变,endInsertRows()对操作进行确认与通知,返回true表示成功
beginInsertRows(parent, row, row + count - 1);
for (int column = 0; column < count; column++)
{
KadResult result;
m_resultList.insert(row, result);
}
endInsertRows();
return true;
}
bool ResultTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
beginRemoveRows(parent, row, row + count - 1);
for (int column = 0; column < count; column++)
{
m_resultList.removeAt(row);
}
endRemoveRows();
return true;
}
int ResultTableModel::InsertData(const KadResult& result)
{
//在最后一行插入
int row = m_resultList.count();
if(insertRow(row))
{
m_resultList[row] = result;
}
QModelIndex topLeft = index(m_resultList.count(), 0);
QModelIndex bottomRight = index(m_resultList.count(), COLUMN_OPERATE);
emit dataChanged(topLeft, bottomRight);
return row;
}
KadResult& ResultTableModel::GetData(int row)
{
return m_resultList[row];
}