-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdelegate.cpp
268 lines (230 loc) · 7.11 KB
/
delegate.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
262
263
264
265
266
267
268
#include "delegate.hpp"
Idatag_delegate_rva::Idatag_delegate_rva(QWidget* parent, Idatag_model* myModel, Idatag_configuration* myConfiguration) :
QStyledItemDelegate()
{
this->parent = parent;
this->myModel = myModel;
this->myConfiguration = myConfiguration;
}
void Idatag_delegate_rva::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
char rva_str[20];
if (!index.isValid()) return;
if (index.data().canConvert<uint64_t>())
{
uint64_t rva = qvariant_cast<uint64_t>(index.data());
if (this->myConfiguration->get_address_width_configuration() == 16)
{
snprintf(rva_str, 19, "0x%016llX", (unsigned long long)rva);
}
else
{
snprintf(rva_str, 19, "0x%08llX", (unsigned long long)rva);
}
painter->save();
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.highlight());
painter->setPen(Qt::white);
}
QStyleOptionViewItem option_b = option;
option_b.rect.setX(option.rect.x() + 5);
painter->drawText(option_b.rect, Qt::AlignVCenter | Qt::AlignLeft, rva_str);
painter->restore();
}
else {
QStyledItemDelegate::paint(painter, option, index);
}
}
Idatag_delegate_name::Idatag_delegate_name(QWidget* parent, Idatag_model* myModel, Idatag_configuration* myConfiguration) :
QStyledItemDelegate()
{
this->parent = parent;
this->myModel = myModel;
this->myConfiguration = myConfiguration;
}
void Idatag_delegate_name::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (!index.isValid()) return;
if (index.data().canConvert<QString>())
{
QString name = qvariant_cast<QString>(index.data());
painter->save();
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.highlight());
painter->setPen(Qt::white);
}
QStyleOptionViewItem option_b = option;
option_b.rect.setX(option.rect.x() + 5);
painter->drawText(option_b.rect, Qt::AlignVCenter | Qt::AlignLeft, name);
painter->restore();
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
Idatag_delegate_tag::Idatag_delegate_tag(QWidget* parent, Idatag_model* myModel, Idatag_configuration* myConfiguration, Idatag_proxy* myProxy) :
QStyledItemDelegate()
{
this->parent = parent;
this->myModel = myModel;
this->myPalette = new Idatag_palette(this->myModel->get_feeders());
this->myConfiguration = myConfiguration;
this->myProxy = myProxy;
}
Idatag_editor::Idatag_editor(QWidget* parent)
: QLineEdit(parent)
{
this->lock = true;
connect(this, &QLineEdit::selectionChanged, this, &Idatag_editor::OnEdit);
}
void Idatag_editor::locker()
{
this->lock = true;
}
void Idatag_editor::OnEdit()
{
if (this->lock) {
int size = this->text().size();
this->setSelection(size, 0);
}
this->lock = false;
}
QWidget* Idatag_delegate_tag::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Idatag_editor* editor = new Idatag_editor(parent);
return editor;
}
void Idatag_delegate_tag::drawTag(Tag tag, QPainter *painter, const QStyleOptionViewItem& option, int width, int height, bool filtered) const
{
painter->setPen(Qt::white);
QString qlabel = QString::fromStdString(tag.get_label());
QPainterPath path = QPainterPath();
path.addRoundedRect(QRectF(option.rect.x(), option.rect.y() + 2, width + 20, height), 10, 10);
painter->fillPath(path, this->myPalette->get_feeder_colour(tag.get_signature()));
painter->drawText(path.controlPointRect(), Qt::AlignHCenter | Qt::AlignVCenter, qlabel);
painter->setPen(Qt::NoPen);
if (filtered)
{
QPen pen(Qt::black, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter->setPen(pen);
}
painter->drawPath(path);
}
void Idatag_delegate_tag::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (!index.isValid()) return;
this->myPalette->refresh_feeders(this->myModel->get_feeders());
Offset* offset = index.data().value<Offset*>();
if (offset != NULL) {
std::vector<Tag> tags= offset->get_tags();
painter->save();
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.highlight());
}
painter->setRenderHint(QPainter::Antialiasing);
painter->translate(10, 0);
int trans = 30;
bool filtered;
for (auto &tag : tags) {
QString qlabel = QString::fromStdString(tag.get_label());
QFontMetrics fontmetrics = painter->fontMetrics();
int width = fontmetrics.boundingRect(qlabel).width() + 7;
int height = fontmetrics.boundingRect(qlabel).height() + 7;
filtered = this->myProxy->is_label_filtered(tag.get_label());
drawTag(tag, painter, option, width, height, filtered);
painter->translate(width + trans, 0);
}
painter->restore();
}
else {
QStyledItemDelegate::paint(painter, option, index);
}
}
void Idatag_delegate_tag::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (!index.isValid()) return;
Offset* offset = index.data().value<Offset*>();
if (offset != NULL) {
Idatag_editor* qeditor = qobject_cast<Idatag_editor*>(editor);
qeditor->locker();
QString qinput = qeditor->text();
std::string input = qinput.toStdString();
if (input.find_first_not_of(" ") != std::string::npos)
{
QStringList labels = qeditor->text().split(" ", QString::SkipEmptyParts);
std::vector<Tag> tags = offset->get_tags();
std::string user = myConfiguration->get_username_configuration();
std::string feeder = user;
bool changed = false;
for (auto& tag : tags)
{
bool still = false;
for (const auto & qlabel : labels)
{
std::string label = qlabel.toStdString();
if (tag.get_label().compare(label) == 0)
{
still = true;
break;
}
}
if (still == false)
{
std::string lbl_toremove = tag.get_label();
offset->remove_tag(lbl_toremove);
changed = true;
}
}
for (const auto & qlabel : labels)
{
std::string label = qlabel.toStdString();
if (!offset->check_already_tagged(label)) {
Tag tag = Tag(label, feeder);
offset->add_tag(tag);
std::string autofeed = "IDATag";
Tag tag_user = Tag(user, autofeed);
offset->add_tag(tag_user);
this->myModel->add_feeder(feeder);
this->myModel->add_feeder(autofeed);
changed = true;
}
}
if (changed) {
std::string autofeed = "IDATag";
Tag tag_user = Tag(user, autofeed);
offset->add_tag(tag_user);
this->myModel->add_feeder(autofeed);
}
}
else
{
offset->remove_all_tags();
}
}
else {
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void Idatag_delegate_tag::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (!index.isValid()) return;
Offset* offset = index.data().value<Offset*>();
if (offset != NULL) {
std::vector<Tag> tags = offset->get_tags();
std::string tag_labels;
for (auto &tag : tags) {
tag_labels += tag.get_label();
tag_labels += " ";
}
QString qtag_labels = QString::fromStdString(tag_labels);
QLineEdit* qeditor = qobject_cast<QLineEdit*>(editor);
if (qeditor) {
qeditor->setText(qtag_labels);
}
}
else {
QStyledItemDelegate::setEditorData(editor, index);
}
}