1 #include "BasicBlockWidget.hxx"
2 #include "gui/Mainwindow.hxx"
3 #include "gui/dialogs/SimpleStringDialog.hxx"
4 #include "core/BasicBlock.hxx"
6 class CustomQGraphicsTextItem
: public QObject
, public QGraphicsTextItem
{
8 CustomQGraphicsTextItem(const QString
& text
, BasicBlockWidget
* parent
)
9 : QGraphicsTextItem(text
, parent
), parent(parent
) {}
10 void contextMenuEvent(QGraphicsSceneContextMenuEvent
*);
12 void addComment(QTextTableCell cell
, const QString
& title
);
14 BasicBlockWidget
* parent
;
17 void CustomQGraphicsTextItem::addComment(QTextTableCell cell
, const QString
& title
) {
18 SimpleStringDialog
dialog(title
);
19 int result
= dialog
.exec();
20 if (QDialog::Accepted
== result
) {
21 LOG4CXX_DEBUG(parent
->logger
, "adding comment " << dialog
.result().toStdString()
22 << " at row " << cell
.row());
23 cell
.firstCursorPosition().insertHtml(QString(";; ") + dialog
.result());
25 LOG4CXX_DEBUG(parent
->logger
, "addComment aborted");
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
);
36 uint64_t address
= c
.selectedText().toLongLong(&ok
, 16);
38 QAction
* act
= menu
.addAction(c
.selectedText() + " is a Function");
39 QObject::connect(act
, &QAction::triggered
,
40 [=]() {parent
->mainwindow
->requestNewFunctionByAddress(address
);});
43 QTextTable
* table
= c
.currentTable();
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");
50 QObject::connect(globalComment
, &QAction::triggered
,
51 [=]() { addComment(cell
, "Global comment"); });
52 QObject::connect(localComment
, &QAction::triggered
,
53 [=]() { addComment(cell
, "Local comment"); });
56 menu
.exec(event
->screenPos());
59 BasicBlockWidget::BasicBlockWidget(const QString
& name
, BasicBlock
* block
,
60 Mainwindow
* mainwindow
)
61 : width(270), height(45), name(name
)
63 , block(block
), mainwindow(mainwindow
)
64 , logger(log4cxx::Logger::getLogger(name
.toStdString() + "BasicBlock")) {
65 next
[0] = NULL
; next
[1] = NULL
;
66 _widget
.reset(new CustomQGraphicsTextItem("", this));
67 _widget
->setPos(5, 20);
68 _widget
->setTextInteractionFlags(Qt::TextSelectableByMouse
|
69 Qt::LinksAccessibleByMouse
);
71 if (width
< 250) width
= 250;
74 void BasicBlockWidget::addItem(uint8_t* bytes
, size_t num_bytes
,
75 QString line
, const QString
& href
) {
81 _table
->appendRows(1);
84 QTextTableFormat format
;
85 format
.setBorderStyle(QTextFrameFormat::BorderStyle_None
);
87 _table
= _widget
->textCursor().insertTable(1, 3, format
);
90 for (size_t i(0); i
< num_bytes
; ++i
) {
91 const char * hexdigits
= "0123456789ABCDEF";
92 bytestring
+= hexdigits
[(bytes
[i
] >> 4) & 0xF];
93 bytestring
+= hexdigits
[bytes
[i
] & 0xF];
97 _table
->cellAt(row
, 0).firstCursorPosition().insertText(bytestring
);
99 line
= line
.replace('\t', ' ').toHtmlEscaped();
101 line
= "<a href=\"" + href
+ "\">" + line
+ "</a>";
104 _table
->cellAt(row
, 1).firstCursorPosition().insertHtml(line
);
106 QObject::connect(_widget
.get(), &QGraphicsTextItem::linkActivated
,
108 if (str
.startsWith("function:")) {
109 QString address
= str
.remove("function:");
110 mainwindow
->switchMainPlaneToAddress(address
.toInt(NULL
, 16));
115 void BasicBlockWidget::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
,
117 width
= 10 + _widget
->boundingRect().width();
118 height
= 25 + _widget
->boundingRect().height();
119 if (width
< 250) width
= 250;
121 painter
->fillRect(0, 0, width
, height
, QColor(0xcc, 0xcc, 0xff, 0xff));
122 painter
->setPen(QColor(0x00, 0x00, 0xff, 0xff));
123 painter
->drawRect(0, 0, width
, height
);
124 painter
->drawText(5, 15, name
);
127 QRectF
BasicBlockWidget::boundingRect() const {
129 QRectF
result(- penWidth
/ 2, - penWidth
/ 2,
130 width
+ penWidth
, height
+ penWidth
);
134 std::array
<QPointF
, 3> BasicBlockWidget::getExits() const {
135 return { { mapToScene(QPointF( width
/3, height
)),
136 mapToScene(QPointF( width
/2, height
)),
137 mapToScene(QPointF(2*width
/3, height
)) } };