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
, Mainwindow
* mainwindow
)
9 : QGraphicsTextItem(text
, parent
), parent(parent
), mainwindow(mainwindow
) {}
10 void contextMenuEvent(QGraphicsSceneContextMenuEvent
*);
12 BasicBlockWidget
* parent
;
13 Mainwindow
* mainwindow
;
17 void addComment(QTextTableCell cell
, const QString
& title
) {
18 SimpleStringDialog
dialog(title
);
19 int result
= dialog
.exec();
20 if (QDialog::Accepted
== result
) {
21 // LOG4CXX_DEBUG(logger, "adding comment " << dialog.result().toStdString()
22 // << " at row " << cell.row());
23 cell
.firstCursorPosition().insertHtml(QString(";; ") + dialog
.result());
25 // LOG4CXX_DEBUG(logger, "addComment aborted");
30 void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent
* event
) {
31 QTextCursor c
= textCursor();
32 c
.setPosition(document()->documentLayout()->hitTest(event
->pos(), Qt::FuzzyHit
));
33 c
.select(QTextCursor::WordUnderCursor
);
37 uint64_t address
= c
.selectedText().toLongLong(&ok
, 16);
39 QAction
* act
= menu
.addAction(c
.selectedText() + " is a Function");
40 QObject::connect(act
, &QAction::triggered
,
41 [=]() {mainwindow
->requestNewFunctionByAddress(address
);});
44 QTextTable
* table
= c
.currentTable();
46 int row
= table
->cellAt(c
).row();
47 QTextTableCell cell
= table
->cellAt(row
, 2);
48 QAction
* globalComment
= menu
.addAction("Add global Comment");
49 QAction
* localComment
= menu
.addAction("Add local Comment");
51 QObject::connect(globalComment
, &QAction::triggered
,
52 [=]() { addComment(cell
, "Global comment"); });
53 QObject::connect(localComment
, &QAction::triggered
,
54 [=]() { addComment(cell
, "Local comment"); });
57 menu
.exec(event
->screenPos());
60 BasicBlockWidget::BasicBlockWidget(const QString
& name
, BasicBlock
* block
,
61 Mainwindow
* mainwindow
)
62 : width(270), height(45), name(name
)
64 , block(block
), mainwindow(mainwindow
)
65 , logger(log4cxx::Logger::getLogger(name
.toStdString() + "BasicBlock")) {
66 next
[0] = NULL
; next
[1] = NULL
;
67 _widget
.reset(new CustomQGraphicsTextItem("", this, mainwindow
));
68 _widget
->setPos(5, 20);
69 _widget
->setTextInteractionFlags(Qt::TextSelectableByMouse
|
70 Qt::LinksAccessibleByMouse
);
72 if (width
< 250) width
= 250;
75 void BasicBlockWidget::addItem(uint8_t* bytes
, size_t num_bytes
,
76 QString line
, const QString
& href
) {
82 _table
->appendRows(1);
85 QTextTableFormat format
;
86 format
.setBorderStyle(QTextFrameFormat::BorderStyle_None
);
88 _table
= _widget
->textCursor().insertTable(1, 3, format
);
91 for (size_t i(0); i
< num_bytes
; ++i
) {
92 const char * hexdigits
= "0123456789ABCDEF";
93 bytestring
+= hexdigits
[(bytes
[i
] >> 4) & 0xF];
94 bytestring
+= hexdigits
[bytes
[i
] & 0xF];
98 _table
->cellAt(row
, 0).firstCursorPosition().insertText(bytestring
);
100 line
= line
.replace('\t', ' ').toHtmlEscaped();
102 line
= "<a href=\"" + href
+ "\">" + line
+ "</a>";
105 _table
->cellAt(row
, 1).firstCursorPosition().insertHtml(line
);
107 QObject::connect(_widget
.get(), &QGraphicsTextItem::linkActivated
,
109 if (str
.startsWith("function:")) {
110 QString address
= str
.remove("function:");
111 mainwindow
->switchMainPlaneToAddress(address
.toInt(NULL
, 16));
116 void BasicBlockWidget::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
,
118 width
= 10 + _widget
->boundingRect().width();
119 height
= 25 + _widget
->boundingRect().height();
120 if (width
< 250) width
= 250;
122 painter
->fillRect(0, 0, width
, height
, QColor(0xcc, 0xcc, 0xff, 0xff));
123 painter
->setPen(QColor(0x00, 0x00, 0xff, 0xff));
124 painter
->drawRect(0, 0, width
, height
);
125 painter
->drawText(5, 15, name
);
128 QRectF
BasicBlockWidget::boundingRect() const {
130 QRectF
result(- penWidth
/ 2, - penWidth
/ 2,
131 width
+ penWidth
, height
+ penWidth
);
135 std::array
<QPointF
, 3> BasicBlockWidget::getExits() const {
136 return { { mapToScene(QPointF( width
/3, height
)),
137 mapToScene(QPointF( width
/2, height
)),
138 mapToScene(QPointF(2*width
/3, height
)) } };