-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultModel.cpp
More file actions
407 lines (331 loc) · 7.57 KB
/
ResultModel.cpp
File metadata and controls
407 lines (331 loc) · 7.57 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
#include "ResultModel.hpp"
#include <QDebug>
#include <QFile>
#include <QVector>
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];
}
const 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*)>& predicate)
{
QVector<Node*> result;
int i = childCount();
while (i--)
{
if (predicate(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*)>& predicate) const
{
QVector<Node*> results;
QVector<Node*> stack;
stack.append(const_cast<Node*>(this));
while (!stack.isEmpty())
{
Node* node = stack.takeLast();
stack.append(node->_children);
if (predicate(node))
{
results.append(node);
}
}
return results;
}
Node* findChild(const std::function<bool(const Node*)>& predicate) const
{
auto children = findChildren(predicate);
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 value(Qt::ItemDataRole role, const QVariant& defaultValue = QVariant()) const
{
return _data.value(role, defaultValue);
}
void setValue(Qt::ItemDataRole role, const QVariant& value)
{
_data[role] = value;
}
QString displayString() const
{
return _data.value(Qt::DisplayRole).toString();
}
bool isChecked() const
{
return _data.value(Qt::CheckStateRole) == Qt::CheckState::Checked;
}
private:
const 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();
}
const Node* parentNode= parentIndex.isValid() ? indexToNode(parentIndex) : _root;
const Node* childNode = parentNode->childAt(row);
return childNode ? createIndex(row, column, childNode) : QModelIndex();
}
QModelIndex ResultModel::parent(const QModelIndex& childIndex) const
{
if (!childIndex.isValid())
{
return QModelIndex();
}
const 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();
}
const 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->value(Qt::DisplayRole);
}
}
if (role == Qt::CheckStateRole)
{
if (pathCell)
{
return item->value(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->setValue(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)
{
const auto hashEquals = [&](const Node* node)
{
return node->displayString() == hash;
};
Node* hashNode = _root->findChild(hashEquals);
if (!hashNode)
{
int newHashRow = _root->childCount();
beginInsertRows(QModelIndex(), newHashRow, newHashRow);
hashNode = _root->appendChild({ { Qt::DisplayRole, hash } });
hashNode->appendChild({ { Qt::DisplayRole, filePath }, { Qt::CheckStateRole, Qt::Unchecked } });
++_totalCount;
endInsertRows();
}
else
{
QModelIndex hashIndex = createIndex(hashNode->parentRow(), 0, hashNode);
int newPathRow = hashNode->childCount();
beginInsertRows(hashIndex, newPathRow, newPathRow);
hashNode->appendChild({ { Qt::DisplayRole, filePath }, { Qt::CheckStateRole, Qt::Unchecked } });
++_totalCount;
endInsertRows();
}
}
QStringList ResultModel::selectedPaths() const
{
QStringList results;
const auto isChecked = [&](const Node* node)
{
return node->isChecked();
};
for (const Node* node : _root->findChildren(isChecked))
{
results.append(node->displayString());
}
return results;
}
int ResultModel::totalCount() const
{
return _totalCount;
}
int ResultModel::selectedCount() const
{
return _selectedCount;
}
void ResultModel::prune(const std::function<bool(const Node*)>& predicate)
{
int i = _root->childCount();
beginResetModel();
while (i--)
{
Node* hashNode = _root->childAt(i);
for (const Node* pathNode : hashNode->takeChildren(predicate))
{
if (pathNode->isChecked())
{
--_selectedCount;
}
--_totalCount;
delete pathNode;
}
// If a hash has one of fewer instances remove it from the model
// The point of the application is to find duplicates
if (hashNode->childCount() < 2)
{
// Check if the hash node has a lone child
if (hashNode->childCount() == 1)
{
const Node* lone = hashNode->childAt(0);
if (lone->isChecked())
{
--_selectedCount;
}
--_totalCount;
}
// Delete the hash node itself
// Which will also delete any remaining children. see Node dtor
delete _root->takeChild(i);
}
}
endResetModel();
}
void ResultModel::removePath(const QString& filePath)
{
const auto pathEquals = [&](const Node* node)
{
return node->displayString() == filePath;
};
prune(pathEquals);
}
void ResultModel::removeInexistentPaths()
{
const auto missing = [](const Node* node)
{
const QString filePath = node->displayString();
return !QFile::exists(filePath);
};
prune(missing);
}