+#include "gui/dialogs/SimpleStringDialog.hxx"
+#include "core/BasicBlock.hxx"
+#include "core/Function.hxx"
+#include "core/Comment.hxx"
+#include "disassembler/Instruction.hxx"
+#include "core/InformationManager.hxx"
+#include "core/events/RenameFunctionEvent.hxx"
+#include "core/events/ChangeCommentEvent.hxx"
+#include <algorithm>
+
+class CustomQGraphicsTextItem : public QObject, public QGraphicsTextItem {
+public:
+ CustomQGraphicsTextItem(const QString& text, BasicBlockWidget* parent)
+ : QGraphicsTextItem(text, parent), parent(parent) {}
+ void contextMenuEvent(QGraphicsSceneContextMenuEvent*);
+
+ void adjustSize();
+private:
+ void addComment(int row, bool global);
+
+ BasicBlockWidget* parent;
+};
+
+void CustomQGraphicsTextItem::addComment(int row, bool global) {
+ SimpleStringDialog dialog(global ? "Global comment" : "Local comment");
+ int result = dialog.exec();
+ uint64_t address = parent->instructions[row].getAddress();
+ if (QDialog::Accepted == result) {
+ Comment* comment;
+ if (global) {
+ comment = parent->block->getManager()->newGlobalComment(address);
+ } else {
+ /* TODO: 0x23 as we currently don't have the function here
+ * and setting it to null will make the comment appear
+ * global. Also means local comments are largely still
+ * broken.
+ */
+ comment = parent->block->getManager()->newLocalComment(address, (Function*)0x23);
+ }
+ comment->setText(dialog.result().toStdString());
+ parent->block->getManager()->finishComment(comment);
+ } else {
+ LOG4CXX_DEBUG(parent->logger, "addComment aborted");
+ }
+}
+
+void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
+ QTextCursor c = textCursor();
+ c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
+ c.select(QTextCursor::WordUnderCursor);
+
+ QMenu menu;
+ bool ok;
+ uint64_t address = c.selectedText().toLongLong(&ok, 16);
+ QTextTable* table = c.currentTable();
+ if (ok) {
+ QAction* act = menu.addAction(c.selectedText() + " is a Function");
+ QObject::connect(act, &QAction::triggered,
+ [=]() {
+ parent->mainwindow->requestNewFunctionByAddress(address);
+ if (NULL == table) return;
+ int row = table->cellAt(c).row();
+ uint64_t insAddress = parent->instructions[row].getAddress();
+ Comment* comment = parent->block->getManager()->newLocalComment(insAddress, (Function*)0x23);
+ comment->setText("#F<" + c.selectedText().toStdString() + ">");
+ parent->block->getManager()->finishComment(comment);
+ });
+ }
+
+ if (NULL != table) {
+ int row = table->cellAt(c).row();
+ QAction* globalComment = menu.addAction("Add global Comment");
+ QAction* localComment = menu.addAction("Add local Comment");
+
+ QObject::connect(globalComment, &QAction::triggered,
+ [=]() { addComment(row, true); });
+ QObject::connect(localComment, &QAction::triggered,
+ [=]() { addComment(row, false); });
+ }
+
+ menu.exec(event->screenPos());
+}
+
+/* QGraphicsTextItem has an adjustSize() function that is supposed to
+ * resize the widget to it's "ideal" size. However it totally ignores
+ * all directives to not wrap lines and "ideal" is actually just a
+ * bunch of heuristics.
+ *
+ * We are starting with a hopefully absurdly large startingwidth and
+ * reduce it untill a line is broken (detected by a change in
+ * height). As long as the width (1000 here) is sufficiently large,
+ * this should give us a widget without any line-wrapping.
+ *
+ * One needs to call this on a Pointer of tye CustomQGraphicsTextItem
+ * as the adjustSize() function is not polymorphic (vurtual).
+ */
+void CustomQGraphicsTextItem::adjustSize() {
+ int width = 1000;
+ setTextWidth(width);
+ int height = boundingRect().height();
+ while (width > 250 && height == boundingRect().height()) {
+ setTextWidth(width -= 10);
+ }
+ width += 10;
+ if (width < 250) width = 250;
+ setTextWidth(width);
+}