]> git.siccegge.de Git - frida/frida.git/blob - src/gui/widgets/BasicBlockWidget.cxx
Compile fix for last commit
[frida/frida.git] / src / gui / widgets / BasicBlockWidget.cxx
1 #include "BasicBlockWidget.hxx"
2 #include "gui/Mainwindow.hxx"
3 #include "gui/dialogs/SimpleStringDialog.hxx"
4
5 class CustomQGraphicsTextItem : public QObject, public QGraphicsTextItem {
6 public:
7 CustomQGraphicsTextItem(const QString& text, BasicBlockWidget* parent, Mainwindow* mainwindow)
8 : QGraphicsTextItem(text, parent), parent(parent), mainwindow(mainwindow) {}
9 void contextMenuEvent(QGraphicsSceneContextMenuEvent*);
10 private:
11 BasicBlockWidget* parent;
12 Mainwindow* mainwindow;
13 };
14
15 namespace {
16 void addComment(QTextTableCell cell, const QString& title) {
17 SimpleStringDialog dialog(title);
18 int result = dialog.exec();
19 if (QDialog::Accepted == result) {
20 // LOG4CXX_DEBUG(logger, "adding comment " << dialog.result().toStdString()
21 // << " at row " << cell.row());
22 cell.firstCursorPosition().insertHtml(QString(";; ") + dialog.result());
23 } else {
24 // LOG4CXX_DEBUG(logger, "addComment aborted");
25 }
26 }
27 }
28
29 void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
30 QTextCursor c = textCursor();
31 c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
32 c.select(QTextCursor::WordUnderCursor);
33
34 QMenu menu;
35 bool ok;
36 uint64_t address = c.selectedText().toLongLong(&ok, 16);
37 if (ok) {
38 QAction* act = menu.addAction(c.selectedText() + " is a Function");
39 QObject::connect(act, &QAction::triggered,
40 [=]() {mainwindow->requestNewFunctionByAddress(address);});
41 }
42
43 QTextTable* table = c.currentTable();
44 if (NULL != table) {
45 int row = table->cellAt(c).row();
46 QTextTableCell cell = table->cellAt(row, 2);
47 QAction* globalComment = menu.addAction("Add global Comment");
48 QAction* localComment = menu.addAction("Add local Comment");
49
50 QObject::connect(globalComment, &QAction::triggered,
51 [=]() { addComment(cell, "Global comment"); });
52 QObject::connect(localComment, &QAction::triggered,
53 [=]() { addComment(cell, "Local comment"); });
54 }
55
56 menu.exec(event->screenPos());
57 }
58
59 BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block,
60 Mainwindow * mainwindow)
61 : width(270), height(45), name(name)
62 , _table(NULL)
63 , block(block), mainwindow(mainwindow) {
64 next[0] = NULL; next[1] = NULL;
65 _widget.reset(new CustomQGraphicsTextItem("", this, mainwindow));
66 _widget->setPos(5, 20);
67 _widget->setTextInteractionFlags(Qt::TextSelectableByMouse|
68 Qt::LinksAccessibleByMouse);
69
70 if (width < 250) width = 250;
71 }
72
73 void BasicBlockWidget::addItem(uint8_t* bytes, size_t num_bytes,
74 QString line, const QString& href) {
75 QString bytestring;
76 int row;
77
78 if (_table) {
79 row = _table->rows();
80 _table->appendRows(1);
81 } else {
82 row = 0;
83 QTextTableFormat format;
84 format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
85 format.setBorder(0);
86 _table = _widget->textCursor().insertTable(1, 3, format);
87 }
88
89 for (size_t i(0); i < num_bytes; ++i) {
90 const char * hexdigits = "0123456789ABCDEF";
91 bytestring += hexdigits[(bytes[i] >> 4) & 0xF];
92 bytestring += hexdigits[bytes[i] & 0xF];
93 bytestring += ' ';
94 }
95
96 _table->cellAt(row, 0).firstCursorPosition().insertText(bytestring);
97
98 line = line.replace('\t', ' ').toHtmlEscaped();
99 if (href != "") {
100 line = "<a href=\"" + href + "\">" + line + "</a>";
101 }
102
103 _table->cellAt(row, 1).firstCursorPosition().insertHtml(line);
104
105 QObject::connect(_widget.get(), &QGraphicsTextItem::linkActivated,
106 [=](QString str) {
107 if (str.startsWith("function:")) {
108 QString address = str.remove("function:");
109 mainwindow->switchMainPlaneToAddress(address.toInt(NULL, 16));
110 }
111 });
112 }
113
114 void BasicBlockWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
115 QWidget *widget) {
116 width = 10 + _widget->boundingRect().width();
117 height = 25 + _widget->boundingRect().height();
118 if (width < 250) width = 250;
119
120 painter->fillRect(0, 0, width, height, QColor(0xcc, 0xcc, 0xff, 0xff));
121 painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
122 painter->drawRect(0, 0, width, height);
123 painter->drawText(5, 15, name);
124 }
125
126 QRectF BasicBlockWidget::boundingRect() const {
127 qreal penWidth = 1;
128 QRectF result(- penWidth / 2, - penWidth / 2,
129 width + penWidth, height + penWidth);
130 return result;
131 }
132
133 std::array<QPointF, 3> BasicBlockWidget::getExits() const {
134 return { { mapToScene(QPointF( width/3, height)),
135 mapToScene(QPointF( width/2, height)),
136 mapToScene(QPointF(2*width/3, height)) } };
137 }
138