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 pathcustomdelegate.cpp
More file actions
155 lines (137 loc) · 5.15 KB
/
Copy pathcustomdelegate.cpp
File metadata and controls
155 lines (137 loc) · 5.15 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
#include "customdelegate.h"
#include "Column.h"
#include <QPainter>
#include <QAbstractItemView>
CustomDelegate::CustomDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
tableView = qobject_cast<QAbstractItemView *>(this->parent());
if(tableView)
{
connect(tableView->model(), &QAbstractItemModel::rowsRemoved, this, &CustomDelegate::updateAnyDuplicates);
}
}
void CustomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(tableView)
{
QModelIndex hover = tableView->indexAt(tableView->viewport()->mapFromGlobal(QCursor::pos()));
if(hover.row() == index.row())
{
QColor custom_color(150,170,200,50);
painter->fillRect(option.rect, custom_color);
}
else
{
QList<int> columns_to_check = {
Column::SHA256,
Column::SHA1,
Column::MD5
};
int used_column = 0;
QModelIndex column_index;
QVariant current_value;
for(int col : columns_to_check)
{
if(tableView->model()->headerData(col, Qt::Horizontal).toBool())
{
column_index = index.sibling(index.row(), col);
current_value = column_index.data();
if(!current_value.toString().contains("-"))
{
used_column = col;
break;
}
}
}
static QMap<QString, QColor> value_to_colormap;
static QVector<QColor> colors = { QColor(100, 255, 100, 100), // green
QColor(100, 100, 255, 100), // blue
QColor(100, 255, 200, 100), // teal
QColor(100, 255, 255, 100), // turquoise
QColor(255, 100, 100, 100), // red
QColor(200, 100, 255, 100), // purple
QColor(255, 100, 200, 100), // pink
QColor(255, 200, 100, 100), // orange
QColor(255, 255, 100, 100), // yellow
QColor(200, 150, 100, 100) // brown
};
if(!current_value.isNull() && !current_value.toString().isEmpty() && !(current_value.toString() == "-"))
{
bool has_duplicate = false;
for(int i = 0; i < tableView->model()->rowCount(); ++i)
{
if(i != index.row())
{
QModelIndex other_row_index = tableView->model()->index(i, used_column);
QVariant other_value = other_row_index.data();
if(current_value == other_value)
{
has_duplicate = true;
emit duplicatesFound();
break;
}
}
}
if(has_duplicate)
{
QString value_str = current_value.toString();
if(!value_to_colormap.contains(value_str))
{
value_to_colormap[value_str] = colors[value_to_colormap.size() % colors.size()];
}
painter->fillRect(option.rect, value_to_colormap[value_str]);
}
}
}
}
QStyledItemDelegate::paint(painter, option, index);
}
void CustomDelegate::updateAnyDuplicates()
{
if(!tableView)
return;
bool any_duplicates = false;
int used_column = getUsedColumn(tableView->model());
int row_count = tableView->model()->rowCount();
for(int row1 = 0; row1 < row_count; ++row1)
{
for(int row2 = row1 + 1; row2 < row_count; ++row2)
{
QModelIndex index1 = tableView->model()->index(row1, used_column);
QModelIndex index2 = tableView->model()->index(row2, used_column);
QVariant value1 = index1.data();
QVariant value2 = index2.data();
if(value1 == value2)
{
any_duplicates = true;
}
}
}
if(!any_duplicates)
emit noDuplicatesFound();
}
int CustomDelegate::getUsedColumn(const QAbstractItemModel *tableModel)
{
QList<int> columns_to_check = {
Column::SHA256,
Column::SHA1,
Column::MD5
};
int used_column = 0;
QModelIndex column_index;
QVariant current_value;
for(int col : columns_to_check)
{
if(tableModel->headerData(col, Qt::Horizontal).toBool())
{
column_index = tableModel->index(0, col);
current_value = column_index.data();
if(!current_value.toString().contains("-"))
{
used_column = col;
break;
}
}
}
return used_column;
}