-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultModel.cpp
More file actions
361 lines (294 loc) · 6.47 KB
/
ResultModel.cpp
File metadata and controls
361 lines (294 loc) · 6.47 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
#include "ResultModel.hpp"
#include "ResultModel.hpp"
#include <QApplication>
#include <QDebug>
#include <QQueue>
inline Node* indexToNode(const QModelIndex& index)
{
return static_cast<Node*>(index.internalPointer());
}
class Node
{
public:
Node(Node* parent, const QMap<Qt::ItemDataRole, QVariant>& data) :
_parent(parent),
_data(data)
{
}
~Node()
{
qDeleteAll(_children);
qDebug() << _data[Qt::DisplayRole];
}
Node* parent() const
{
return _parent;
}
Node* childAt(int index) const
{
return _children.at(index);
}
Node* takeChild(int index)
{
return _children.takeAt(index);
}
QVector<Node*> takeChildren(const std::function<bool(const Node*)>& lambda)
{
QVector<Node*> result;
int i = childCount();
while (i--)
{
if (lambda(childAt(i)))
{
result.append(takeChild(i));
}
}
return result;
}
Node* appendChild(const QMap<Qt::ItemDataRole, QVariant>& data)
{
Node* child = new Node(this, data);
_children.append(child);
return child;
}
QVector<Node*> findChildren(const std::function<bool(const Node*)>& lambda) const
{
QVector<Node*> results;
QQueue<Node*> queue;
queue.enqueue(const_cast<Node*>(this));
while (!queue.empty())
{
Node* node = queue.dequeue();
for (Node* child : node->_children)
{
queue.enqueue(child);
}
if (lambda(node))
{
results.push_back(node);
}
}
return results;
}
Node* findChild(const std::function<bool(const Node*)>& lambda) const
{
auto children = findChildren(lambda);
return children.isEmpty() ? nullptr : children.first();
}
int parentRow() const
{
return _parent ? _parent->_children.indexOf(const_cast<Node*>(this)) : 0;
}
int childCount() const
{
return _children.size();
}
bool hasChildren() const
{
return !_children.isEmpty();
}
QVariant& data(Qt::ItemDataRole role)
{
return _data[role];
}
QVariant value(Qt::ItemDataRole role, const QVariant& defaultValue = QVariant()) const
{
return _data.value(role, defaultValue);
}
private:
Node* _parent;
QVector<Node*> _children;
QMap<Qt::ItemDataRole, QVariant> _data;
};
ResultModel::ResultModel(QObject *parent) :
QAbstractItemModel(parent),
_root(new Node(nullptr, { { Qt::DisplayRole, "root" } }))
{
}
ResultModel::~ResultModel()
{
delete _root;
}
QModelIndex ResultModel::index(int row, int column, const QModelIndex& parentIndex) const
{
if (!hasIndex(row, column, parentIndex))
{
return QModelIndex();
}
Node* parent= parentIndex.isValid() ? indexToNode(parentIndex) : _root;
Node* child = parent ->childAt(row);
return child ? createIndex(row, column, child) : QModelIndex();
}
QModelIndex ResultModel::parent(const QModelIndex& childIndex) const
{
if (!childIndex.isValid())
{
return QModelIndex();
}
Node* parentNode = indexToNode(childIndex)->parent();
return parentNode != _root ?
createIndex(parentNode->parentRow(), 0, parentNode) :
QModelIndex();
}
int ResultModel::rowCount(const QModelIndex& parentIndex) const
{
return parentIndex.isValid() ?
indexToNode(parentIndex)->childCount() :
_root->childCount();
}
int ResultModel::columnCount(const QModelIndex&) const
{
return 2;
}
QVariant ResultModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
Node* item = indexToNode(index);
bool topLevel = item->parent() == _root;
bool hashCell = index.column() == 0 && topLevel;
bool pathCell = index.column() == 1 && !topLevel;
if (role == Qt::DisplayRole)
{
if (hashCell || pathCell)
{
return item->data(Qt::DisplayRole);
}
}
if (role == Qt::CheckStateRole)
{
if (pathCell)
{
return item->data(Qt::CheckStateRole);
}
}
return QVariant();
}
bool ResultModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
Node* item = indexToNode(index);
bool topLevel = item->parent() == _root;
bool pathCell = index.column() == 1 && !topLevel;
if (role == Qt::CheckStateRole && pathCell)
{
if (value == Qt::CheckState::Checked)
{
++_selectedCount;
}
else if (value == Qt::CheckState::Unchecked)
{
--_selectedCount;
}
else
{
qDebug() << "Invalid check state value:" << value;
return false;
}
item->data(Qt::CheckStateRole) = value;
emit dataChanged(index, index);
return true;
}
return false;
}
QVariant ResultModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
{
return QVariant();
}
switch (section)
{
case 0:
return "Hash";
case 1:
return "Path";
}
return QVariant();
}
Qt::ItemFlags ResultModel::flags(const QModelIndex& index) const
{
if (index.column() != 1)
{
return Qt::ItemIsEnabled;
}
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
void ResultModel::clear()
{
beginResetModel();
delete _root;
_root = new Node(nullptr, { { Qt::DisplayRole, "root" } });
_totalCount = 0;
_selectedCount = 0;
endResetModel();
}
void ResultModel::addPath(const QString& hash, const QString& filePath)
{
Node* hashNode = _root->findChild([&hash](const Node* node)
{
return node->value(Qt::DisplayRole).toString() == hash;
});
if (!hashNode)
{
hashNode = _root->appendChild({ { Qt::DisplayRole, hash } });
}
beginInsertRows(QModelIndex(), 0, 1);
hashNode->appendChild({ { Qt::DisplayRole, filePath }, { Qt::CheckStateRole, false } });
++_totalCount;
endInsertRows();
}
QStringList ResultModel::selectedPaths() const
{
QStringList results;
auto isChecked = [&](const Node* node)
{
return node->value(Qt::CheckStateRole) == Qt::CheckState::Checked;
};
for (Node* node : _root->findChildren(isChecked))
{
results.append(node->data(Qt::DisplayRole).toString());
}
return results;
}
int ResultModel::totalCount() const
{
return _totalCount;
}
int ResultModel::selectedCount() const
{
return _selectedCount;
}
void ResultModel::removePath(const QString& filePath)
{
const auto pathEquals = [&](const Node* node)
{
return node->value(Qt::DisplayRole).toString() == filePath;
};
int i = _root->childCount();
beginResetModel();
while (i--)
{
Node* hashNode = _root->childAt(i);
for (Node* pathNode : hashNode->takeChildren(pathEquals))
{
if (pathNode->data(Qt::CheckStateRole) == Qt::CheckState::Checked)
{
--_selectedCount;
}
delete pathNode;
--_totalCount;
}
if (hashNode->childCount() < 2)
{
if (hashNode->childAt(0)->data(Qt::CheckStateRole) == Qt::CheckState::Checked)
{
--_selectedCount;
}
--_totalCount;
delete _root->takeChild(i);
}
}
endResetModel();
}