1 #include "BasicBlockWidget.hxx"
2 #include "CustomQGraphicsTextItem.hxx"
3 #include "gui/Mainwindow.hxx"
5 class CustomQGraphicsTextItem
: public QObject
, public QGraphicsTextItem
{
7 CustomQGraphicsTextItem(const QString
& text
, BasicBlockWidget
* parent
, Mainwindow
* mainwindow
)
8 : QGraphicsTextItem(text
, parent
), parent(parent
), mainwindow(mainwindow
) {}
9 void contextMenuEvent(QGraphicsSceneContextMenuEvent
*);
11 BasicBlockWidget
* parent
;
12 Mainwindow
* mainwindow
;
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
);
22 uint64_t address
= c
.selectedText().toLongLong(&ok
, 16);
24 QAction
* act
= menu
.addAction(c
.selectedText() + " is a Function");
25 QObject::connect(act
, &QAction::triggered
,
26 [=]() {mainwindow
->requestNewFunctionByAddress(address
);});
28 menu
.exec(event
->screenPos());
31 BasicBlockWidget::BasicBlockWidget(const QString
& name
, BasicBlock
* block
,
32 Mainwindow
* mainwindow
)
33 : width(270), height(45), name(name
)
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
);
42 if (width
< 250) width
= 250;
45 void BasicBlockWidget::addItem(uint8_t* bytes
, size_t num_bytes
,
46 QString line
, const QString
& href
) {
52 _table
->appendRows(1);
55 QTextTableFormat format
;
56 format
.setBorderStyle(QTextFrameFormat::BorderStyle_None
);
58 _table
= _widget
->textCursor().insertTable(1, 3, format
);
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];
68 _table
->cellAt(row
, 0).firstCursorPosition().insertText(bytestring
);
70 line
= line
.replace('\t', ' ').toHtmlEscaped();
72 line
= "<a href=\"" + href
+ "\">" + line
+ "</a>";
75 _table
->cellAt(row
, 1).firstCursorPosition().insertHtml(line
);
77 QObject::connect(_widget
.get(), &QGraphicsTextItem::linkActivated
,
79 if (str
.startsWith("function:")) {
80 QString address
= str
.remove("function:");
81 mainwindow
->switchMainPlaneToAddress(address
.toInt(NULL
, 16));
85 width
= 10 + _widget
->boundingRect().width();
86 height
= 25 + _widget
->boundingRect().height();
88 if (width
< 250) width
= 250;
91 void BasicBlockWidget::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
,
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
);
99 QRectF
BasicBlockWidget::boundingRect() const {
101 QRectF
result(- penWidth
/ 2, - penWidth
/ 2,
102 width
+ penWidth
, height
+ penWidth
);
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
)) } };