This repository was archived by the owner on Dec 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomsortfilterproxymodel.cpp
More file actions
105 lines (84 loc) · 2.91 KB
/
Copy pathcustomsortfilterproxymodel.cpp
File metadata and controls
105 lines (84 loc) · 2.91 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
#include "customsortfilterproxymodel.h"
#include "Column.h"
CustomSortFilterProxyModel::CustomSortFilterProxyModel(QTableView *tableView, QObject *parent)
: QSortFilterProxyModel(parent),
m_tableView(tableView)
{
}
void CustomSortFilterProxyModel::setNumericSortingColumns(const QSet<int> &columns)
{
numericSortingColumns = columns;
}
void CustomSortFilterProxyModel::setFilterDuplicates(bool filter)
{
m_filter_duplicates = filter;
invalidateFilter();
}
bool CustomSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index;
bool regexFilterPassed = false;
for(int col = 0; col < sourceModel()->columnCount(); ++col)
{
if(!m_tableView->isColumnHidden(col))
{
index = sourceModel()->index(sourceRow, col, sourceParent);
if(index.isValid() && sourceModel()->data(index).toString().contains(filterRegularExpression()))
{
regexFilterPassed = true;
break;
}
}
}
if(!regexFilterPassed)
return false;
if(m_filter_duplicates)
{
int visible_column = 0;
QList<int> columns_to_check = {
Column::SHA256,
Column::SHA1,
Column::MD5
};
for(int col : columns_to_check)
{
if(!m_tableView->isColumnHidden(col))
{
QModelIndex hash_index = sourceModel()->index(sourceRow, col, sourceParent);
QString hash_values = sourceModel()->data(hash_index).toString();
if(!hash_values.contains("-"))
{
visible_column = col;
break;
}
}
}
if(visible_column)
{
QModelIndex hash_index = sourceModel()->index(sourceRow, visible_column, sourceParent);
QString hash_values = sourceModel()->data(hash_index).toString();
for (int i = 0; i < sourceRow; ++i)
{
QModelIndex other_hash_index = sourceModel()->index(i, visible_column);
QString other_hash_values = sourceModel()->data(other_hash_index).toString();
if (hash_values == other_hash_values && !(hash_values.isEmpty() || hash_values == "-"))
{
return false;
}
}
}
}
return true;
}
bool CustomSortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
if(numericSortingColumns.contains(source_left.column()))
{
bool ok1, ok2;
double left = sourceModel()->data(source_left, Qt::UserRole).toDouble(&ok1);
double right = sourceModel()->data(source_right, Qt::UserRole).toDouble(&ok2);
if(ok1 && ok2)
return left < right;
}
return QSortFilterProxyModel::lessThan(source_left, source_right);
}