1 #include "BasicBlockWidget.hxx"
2 #include "gui/Mainwindow.hxx"
3 #include "gui/dialogs/SimpleStringDialog.hxx"
4 #include "core/BasicBlock.hxx"
5 #include "core/Function.hxx"
6 #include "core/Comment.hxx"
7 #include "disassembler/Instruction.hxx"
8 #include "core/InformationManager.hxx"
9 #include "core/events/RenameFunctionEvent.hxx"
10 #include "core/events/ChangeCommentEvent.hxx"
13 class CustomQGraphicsTextItem
: public QObject
, public QGraphicsTextItem
{
15 CustomQGraphicsTextItem(const QString
& text
, BasicBlockWidget
* parent
)
16 : QGraphicsTextItem(text
, parent
), parent(parent
) {}
17 void contextMenuEvent(QGraphicsSceneContextMenuEvent
*);
21 void addComment(int row
, bool global
);
23 BasicBlockWidget
* parent
;
26 void CustomQGraphicsTextItem::addComment(int row
, bool global
) {
27 SimpleStringDialog
dialog(global
? "Global comment" : "Local comment");
28 int result
= dialog
.exec();
29 uint64_t address
= parent
->instructions
[row
].getAddress();
30 if (QDialog::Accepted
== result
) {
33 comment
= parent
->block
->getManager()->newGlobalComment(address
);
35 comment
= parent
->block
->getManager()->newLocalComment(address
, (Function
*)0x23);
37 comment
->setText(dialog
.result().toStdString());
38 parent
->block
->getManager()->finishComment(comment
);
40 // cell.firstCursorPosition().insertHtml(QString(";; ") + dialog.result());
42 LOG4CXX_DEBUG(parent
->logger
, "addComment aborted");
46 void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent
* event
) {
47 QTextCursor c
= textCursor();
48 c
.setPosition(document()->documentLayout()->hitTest(event
->pos(), Qt::FuzzyHit
));
49 c
.select(QTextCursor::WordUnderCursor
);
53 uint64_t address
= c
.selectedText().toLongLong(&ok
, 16);
55 QAction
* act
= menu
.addAction(c
.selectedText() + " is a Function");
56 QObject::connect(act
, &QAction::triggered
,
57 [=]() {parent
->mainwindow
->requestNewFunctionByAddress(address
);});
60 QTextTable
* table
= c
.currentTable();
62 int row
= table
->cellAt(c
).row();
63 QAction
* globalComment
= menu
.addAction("Add global Comment");
64 QAction
* localComment
= menu
.addAction("Add local Comment");
66 QObject::connect(globalComment
, &QAction::triggered
,
67 [=]() { addComment(row
, true); });
68 QObject::connect(localComment
, &QAction::triggered
,
69 [=]() { addComment(row
, false); });
72 menu
.exec(event
->screenPos());
75 void CustomQGraphicsTextItem::adjustSize() {
78 int height
= boundingRect().height();
79 while (height
== boundingRect().height()) {
80 setTextWidth(width
-= 10);
83 if (width
< 250) width
= 250;
87 BasicBlockWidget::BasicBlockWidget(const QString
& name
, BasicBlock
* block
,
88 Mainwindow
* mainwindow
)
89 : width(200), height(45), name(name
)
91 , block(block
), mainwindow(mainwindow
)
92 , logger(log4cxx::Logger::getLogger("gui.BasicBlockWidget." + name
.toStdString())) {
93 next
[0] = NULL
; next
[1] = NULL
;
95 block
->getManager()->registerRenameFunctionEvent([=](RenameFunctionEvent
* event
) {updateFunctionName(event
);});
97 _widget
.reset(new CustomQGraphicsTextItem("", this));
98 _widget
->setPos(5, 20);
99 _widget
->setTextInteractionFlags(Qt::TextSelectableByMouse
|
100 Qt::LinksAccessibleByMouse
);
102 if (width
< 250) width
= 250;
104 QObject::connect(_widget
.get(), &QGraphicsTextItem::linkActivated
,
106 if (str
.startsWith("function:")) {
107 QString address
= str
.remove("function:");
108 mainwindow
->switchMainPlaneToAddress(address
.toInt(NULL
, 16));
111 instructions
= block
->getInstructions();
113 block
->getManager()->registerChangeCommentEvent([=](ChangeCommentEvent
* e
) {changeCommentHandler(e
);});
116 void BasicBlockWidget::updateFunctionName(RenameFunctionEvent
*event
) {
117 QString search
= QString("function:") + QString::number(event
->address
, 16);
118 QTextDocument
*document
= _widget
->document();
119 QTextBlock b
= document
->begin();
120 while (b
.isValid()) {
121 for (QTextBlock::iterator i
= b
.begin(); !i
.atEnd(); ++i
) {
122 QTextCharFormat format
= i
.fragment().charFormat();
123 bool isLink
= format
.isAnchor();
126 if (search
== format
.anchorHref()) {
127 LOG4CXX_DEBUG(logger
, i
.fragment().text().toStdString() << " ---> " << format
.anchorHref().toStdString());
129 c
.setPosition(i
.fragment().position());
130 c
.movePosition(QTextCursor::Right
, QTextCursor::MoveAnchor
, i
.fragment().length());
131 c
.movePosition(QTextCursor::Left
, QTextCursor::KeepAnchor
, i
.fragment().length());
132 c
.movePosition(QTextCursor::WordRight
, QTextCursor::KeepAnchor
);
133 c
.insertText(event
->new_name
.c_str());
135 QGraphicsTextItem
* item
= _widget
.get();
136 ((CustomQGraphicsTextItem
*)item
)->adjustSize();
144 void BasicBlockWidget::changeCommentHandler(ChangeCommentEvent
* event
) {
145 auto inst_it
= std::find_if(instructions
.begin(), instructions
.end(),
146 [=](Instruction
& inst
) {
147 return inst
.getAddress() == event
->address
;
149 if (inst_it
!= instructions
.end()) {
150 if (std::find(inst_it
->comments().begin(),
151 inst_it
->comments().begin(),
152 event
->comment
) == inst_it
->comments().end()) {
153 LOG4CXX_DEBUG(logger
, "Change Comment Event -- New Comment!");
154 inst_it
->comments().push_back(event
->comment
);
156 int row
= inst_it
- instructions
.begin();
157 LOG4CXX_DEBUG(logger
, "Inserting comment for instruction at row " << std::hex
<< row
);
158 QTextCursor cursor
= _table
->cellAt(row
, 2).lastCursorPosition();
159 while (cursor
!= _table
->cellAt(row
, 2).firstCursorPosition()) {
160 cursor
.movePosition(QTextCursor::Left
, QTextCursor::KeepAnchor
, 1);
162 cursor
.removeSelectedText();
163 cursor
.insertHtml(formatComments(&*inst_it
));
164 QGraphicsTextItem
* item
= _widget
.get();
165 ((CustomQGraphicsTextItem
*)item
)->adjustSize();
169 void BasicBlockWidget::populateWidget() {
171 QTextTableFormat format
;
172 format
.setBorderStyle(QTextFrameFormat::BorderStyle_None
);
175 for (Instruction
& inst
: instructions
) {
177 row
= _table
->rows();
178 _table
->appendRows(1);
181 _table
= _widget
->textCursor().insertTable(1, 3, format
);
184 for (uint8_t byte
: inst
.getBytes()) {
185 const char * hexdigits
= "0123456789ABCDEF";
186 bytestring
+= hexdigits
[(byte
>> 4) & 0xF];
187 bytestring
+= hexdigits
[byte
& 0xF];
190 _table
->cellAt(row
, 0).firstCursorPosition().insertHtml("<nobr>" + bytestring
+ "</nobr>");
192 QString line
= inst
.getText().c_str();
193 line
= line
.replace('\t', ' ').toHtmlEscaped();
194 if (inst
.getReference() != "") {
195 QString href
= inst
.getReference().c_str();
196 QStringList list
= href
.split(":");
197 if (list
[0] == "function") {
198 uint64_t address
= href
.split(":")[1].toLongLong(NULL
, 16);
199 Function
* fun
= block
->getManager()->getFunction(address
);
202 line
= line
.split(" ")[0] + " " + QString(fun
->getName().c_str()).toHtmlEscaped();
203 LOG4CXX_DEBUG(logger
, "Naming function at " << address
<< " " << fun
->getName());
206 line
= "<a href=\"" + href
+ "\">" + line
+ "</a>";
208 _table
->cellAt(row
, 1).firstCursorPosition().insertHtml("<nobr>" + line
+ "</nobr>");
209 _table
->cellAt(row
, 2).firstCursorPosition().insertHtml(formatComments(&inst
));
211 QGraphicsTextItem
* item
= _widget
.get();
212 ((CustomQGraphicsTextItem
*)item
)->adjustSize();
215 QString
BasicBlockWidget::formatComments(Instruction
* inst
) {
217 for (Comment
* c
: inst
->comments()) {
218 comments
+= "<br />";
219 comments
+= QString(c
->getText().c_str()).toHtmlEscaped();
221 return (comments
== "" ? "" : ";; ") + comments
.trimmed();
224 void BasicBlockWidget::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*,
226 width
= 10 + _widget
->boundingRect().width();
227 height
= 25 + _widget
->boundingRect().height();
228 if (width
< 250) width
= 250;
230 painter
->fillRect(0, 0, width
, height
, QColor(0xcc, 0xcc, 0xff, 0xff));
231 painter
->setPen(QColor(0x00, 0x00, 0xff, 0xff));
232 painter
->drawRect(0, 0, width
, height
);
233 painter
->drawText(5, 15, name
);
236 QRectF
BasicBlockWidget::boundingRect() const {
238 QRectF
result(- penWidth
/ 2, - penWidth
/ 2,
239 width
+ penWidth
, height
+ penWidth
);
243 std::array
<QPointF
, 3> BasicBlockWidget::getExits() const {
244 return { { mapToScene(QPointF( width
/3, height
)),
245 mapToScene(QPointF( width
/2, height
)),
246 mapToScene(QPointF(2*width
/3, height
)) } };