]> git.siccegge.de Git - frida/frida.git/blob - src/gui/widgets/BasicBlockWidget.cxx
Add context menu to create Functions
[frida/frida.git] / src / gui / widgets / BasicBlockWidget.cxx
1 #include "BasicBlockWidget.hxx"
2 #include "CustomQGraphicsTextItem.hxx"
3 #include "gui/Mainwindow.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 void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
16 QTextCursor c = textCursor();
17 c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
18 c.select(QTextCursor::WordUnderCursor);
19
20 QMenu menu;
21 bool ok;
22 uint64_t address = c.selectedText().toLongLong(&ok, 16);
23 if (ok) {
24 QAction* act = menu.addAction(c.selectedText() + " is a Function");
25 QObject::connect(act, &QAction::triggered,
26 [=]() {mainwindow->requestNewFunctionByAddress(address);});
27 }
28 menu.exec(event->screenPos());
29 }
30
31 BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block,
32 Mainwindow * mainwindow)
33 : width(270), height(45), name(name)
34 , _table(NULL)
35 , block(block), mainwindow(mainwindow) {
36 next[0] = NULL; next[1] = NULL;
37 _widget.reset(new CustomQGraphicsTextItem("", this, mainwindow));
38 _widget->setPos(5, 20);
39 _widget->setTextInteractionFlags(Qt::TextSelectableByMouse|
40 Qt::LinksAccessibleByMouse);
41
42 if (width < 250) width = 250;
43 }
44
45 void BasicBlockWidget::addItem(uint8_t* bytes, size_t num_bytes,
46 QString line, const QString& href) {
47 QString bytestring;
48 int row;
49
50 if (_table) {
51 row = _table->rows();
52 _table->appendRows(1);
53 } else {
54 row = 0;
55 QTextTableFormat format;
56 format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
57 format.setBorder(0);
58 _table = _widget->textCursor().insertTable(1, 3, format);
59 }
60
61 for (size_t i(0); i < num_bytes; ++i) {
62 const char * hexdigits = "0123456789ABCDEF";
63 bytestring += hexdigits[(bytes[i] >> 4) & 0xF];
64 bytestring += hexdigits[bytes[i] & 0xF];
65 bytestring += ' ';
66 }
67
68 _table->cellAt(row, 0).firstCursorPosition().insertText(bytestring);
69
70 line = line.replace('\t', ' ').toHtmlEscaped();
71 if (href != "") {
72 line = "<a href=\"" + href + "\">" + line + "</a>";
73 }
74
75 _table->cellAt(row, 1).firstCursorPosition().insertHtml(line);
76
77 QObject::connect(_widget.get(), &QGraphicsTextItem::linkActivated,
78 [=](QString str) {
79 if (str.startsWith("function:")) {
80 QString address = str.remove("function:");
81 mainwindow->switchMainPlaneToAddress(address.toInt(NULL, 16));
82 }
83 });
84
85 width = 10 + _widget->boundingRect().width();
86 height = 25 + _widget->boundingRect().height();
87
88 if (width < 250) width = 250;
89 }
90
91 void BasicBlockWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
92 QWidget *widget) {
93 painter->fillRect(0, 0, width, height, QColor(0xcc, 0xcc, 0xff, 0xff));
94 painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
95 painter->drawRect(0, 0, width, height);
96 painter->drawText(5, 15, name);
97 }
98
99 QRectF BasicBlockWidget::boundingRect() const {
100 qreal penWidth = 1;
101 QRectF result(- penWidth / 2, - penWidth / 2,
102 width + penWidth, height + penWidth);
103 return result;
104 }
105
106 std::array<QPointF, 3> BasicBlockWidget::getExits() const {
107 return { { mapToScene(QPointF( width/3, height)),
108 mapToScene(QPointF( width/2, height)),
109 mapToScene(QPointF(2*width/3, height)) } };
110 }
111