From 2b50e2722b549e1ae8b1346feeb78a7725b53d3f Mon Sep 17 00:00:00 2001 From: Petros S Date: Sat, 5 Feb 2022 08:55:10 +0200 Subject: [PATCH] Move more common disassembly-previewing functionality to namaspace (#2849) --- src/common/DisassemblyPreview.cpp | 15 +++++-- src/common/DisassemblyPreview.h | 7 +++ src/widgets/DisassemblerGraphView.cpp | 10 ----- src/widgets/DisassemblerGraphView.h | 1 - src/widgets/DisassemblyWidget.cpp | 64 ++++----------------------- src/widgets/DisassemblyWidget.h | 1 - src/widgets/FunctionsWidget.cpp | 8 +--- 7 files changed, 28 insertions(+), 78 deletions(-) diff --git a/src/common/DisassemblyPreview.cpp b/src/common/DisassemblyPreview.cpp index df6fb58b..e80e6eaa 100644 --- a/src/common/DisassemblyPreview.cpp +++ b/src/common/DisassemblyPreview.cpp @@ -35,9 +35,6 @@ QString DisassemblyPreview::getToolTipStyleSheet() bool DisassemblyPreview::showDisasPreview(QWidget *parent, const QPoint &pointOfEvent, const RVA offsetFrom) { - QProcessEnvironment env; - QPoint point = pointOfEvent; - QList refs = Core()->getXRefs(offsetFrom, false, false); if (refs.length()) { if (refs.length() > 1) { @@ -75,10 +72,20 @@ bool DisassemblyPreview::showDisasPreview(QWidget *parent, const QPoint &pointOf .arg(qMax(8, fnt.pointSize() - 1)) .arg(disasmPreview.join("
")); - QToolTip::showText(point, tooltip, parent, QRect {}, 3500); + QToolTip::showText(pointOfEvent, tooltip, parent, QRect {}, 3500); return true; } } } return false; } + +RVA DisassemblyPreview::readDisassemblyOffset(QTextCursor tc) +{ + auto userData = getUserData(tc.block()); + if (!userData) { + return RVA_INVALID; + } + + return userData->line.offset; +} diff --git a/src/common/DisassemblyPreview.h b/src/common/DisassemblyPreview.h index 656b818e..81e67ee8 100644 --- a/src/common/DisassemblyPreview.h +++ b/src/common/DisassemblyPreview.h @@ -31,8 +31,15 @@ QString getToolTipStyleSheet(); /*! * @brief Show a QToolTip that previews the disassembly that is pointed to + * It works for GraphWidget and DisassemblyWidget * @return True if the tooltip is shown */ bool showDisasPreview(QWidget *parent, const QPoint &pointOfEvent, const RVA offsetFrom); + +/*! + * @brief Reads the offset for the cursor position + * @return The disassembly offset of the hovered asm text + */ +RVA readDisassemblyOffset(QTextCursor tc); } #endif diff --git a/src/widgets/DisassemblerGraphView.cpp b/src/widgets/DisassemblerGraphView.cpp index 7f434ff6..e4bb05b8 100644 --- a/src/widgets/DisassemblerGraphView.cpp +++ b/src/widgets/DisassemblerGraphView.cpp @@ -997,13 +997,3 @@ bool DisassemblerGraphView::Instr::contains(ut64 addr) const { return this->addr <= addr && (addr - this->addr) < size; } - -RVA DisassemblerGraphView::readDisassemblyOffset(QTextCursor tc) -{ - auto userData = getUserData(tc.block()); - if (!userData) { - return RVA_INVALID; - } - - return userData->line.offset; -} diff --git a/src/widgets/DisassemblerGraphView.h b/src/widgets/DisassemblerGraphView.h index 07bfc9ed..c5f10bb1 100644 --- a/src/widgets/DisassemblerGraphView.h +++ b/src/widgets/DisassemblerGraphView.h @@ -178,7 +178,6 @@ private: DisassemblyBlock *blockForAddress(RVA addr); void seekLocal(RVA addr, bool update_viewport = true); void seekInstruction(bool previous_instr); - RVA readDisassemblyOffset(QTextCursor tc); CutterSeekable *seekable = nullptr; QList shortcuts; diff --git a/src/widgets/DisassemblyWidget.cpp b/src/widgets/DisassemblyWidget.cpp index 97c83e64..be8c9bc9 100644 --- a/src/widgets/DisassemblyWidget.cpp +++ b/src/widgets/DisassemblyWidget.cpp @@ -371,7 +371,7 @@ void DisassemblyWidget::highlightCurrentLine() highlightSelection.cursor = cursor; highlightSelection.cursor.movePosition(QTextCursor::Start); while (true) { - RVA lineOffset = readDisassemblyOffset(highlightSelection.cursor); + RVA lineOffset = DisassemblyPreview::readDisassemblyOffset(highlightSelection.cursor); if (lineOffset == seekable->getOffset()) { highlightSelection.format.setBackground(highlightColor); highlightSelection.format.setProperty(QTextFormat::FullWidthSelection, true); @@ -406,7 +406,7 @@ void DisassemblyWidget::highlightPCLine() highlightSelection.cursor.movePosition(QTextCursor::Start); if (PCAddr != RVA_INVALID) { while (true) { - RVA lineOffset = readDisassemblyOffset(highlightSelection.cursor); + RVA lineOffset = DisassemblyPreview::readDisassemblyOffset(highlightSelection.cursor); if (lineOffset == PCAddr) { highlightSelection.format.setBackground(highlightPCColor); highlightSelection.format.setProperty(QTextFormat::FullWidthSelection, true); @@ -439,17 +439,7 @@ void DisassemblyWidget::showDisasContextMenu(const QPoint &pt) RVA DisassemblyWidget::readCurrentDisassemblyOffset() { QTextCursor tc = mDisasTextEdit->textCursor(); - return readDisassemblyOffset(tc); -} - -RVA DisassemblyWidget::readDisassemblyOffset(QTextCursor tc) -{ - auto userData = getUserData(tc.block()); - if (!userData) { - return RVA_INVALID; - } - - return userData->line.offset; + return DisassemblyPreview::readDisassemblyOffset(tc); } void DisassemblyWidget::updateCursorPosition() @@ -476,7 +466,7 @@ void DisassemblyWidget::updateCursorPosition() cursor.movePosition(QTextCursor::Start); while (true) { - RVA lineOffset = readDisassemblyOffset(cursor); + RVA lineOffset = DisassemblyPreview::readDisassemblyOffset(cursor); if (lineOffset == offset) { if (cursorLineOffset > 0) { cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, @@ -537,7 +527,7 @@ void DisassemblyWidget::cursorPositionChanged() cursorCharOffset = c.positionInBlock(); while (c.blockNumber() > 0) { c.movePosition(QTextCursor::PreviousBlock); - if (readDisassemblyOffset(c) != offset) { + if (DisassemblyPreview::readDisassemblyOffset(c) != offset) { break; } cursorLineOffset++; @@ -623,7 +613,7 @@ void DisassemblyWidget::moveCursorRelative(bool up, bool page) void DisassemblyWidget::jumpToOffsetUnderCursor(const QTextCursor &cursor) { - RVA offset = readDisassemblyOffset(cursor); + RVA offset = DisassemblyPreview::readDisassemblyOffset(cursor); seekable->seekToReference(offset); } @@ -645,47 +635,9 @@ bool DisassemblyWidget::eventFilter(QObject *obj, QEvent *event) auto cursorForWord = mDisasTextEdit->cursorForPosition(helpEvent->pos()); cursorForWord.select(QTextCursor::WordUnderCursor); - RVA offsetFrom = readDisassemblyOffset(cursorForWord); - RVA offsetTo = RVA_INVALID; + RVA offsetFrom = DisassemblyPreview::readDisassemblyOffset(cursorForWord); - QList refs = Core()->getXRefs(offsetFrom, false, false); - - if (refs.length()) { - if (refs.length() > 1) { - qWarning() << tr("More than one (%1) references here. Weird behaviour expected.") - .arg(refs.length()); - } - offsetTo = refs.at(0).to; // This is the offset we want to preview - - if (Q_UNLIKELY(offsetFrom != refs.at(0).from)) { - qWarning() << tr("offsetFrom (%1) differs from refs.at(0).from (%(2))") - .arg(offsetFrom) - .arg(refs.at(0).from); - } - - // Only if the offset we point *to* is different from the one the cursor is currently - // on *and* the former is a valid offset, we are allowed to get a preview of offsetTo - if (offsetTo != offsetFrom && offsetTo != RVA_INVALID) { - QStringList disasmPreview = Core()->getDisassemblyPreview(offsetTo, 10); - - // Last check to make sure the returned preview isn't an empty text (QStringList) - if (!disasmPreview.isEmpty()) { - const QFont &fnt = Config()->getFont(); - QFontMetrics fm { fnt }; - - QString tooltip = - QString("
Disassembly Preview:
%3
") - .arg(fnt.family()) - .arg(qMax(6, fnt.pointSize() - 1)) - .arg(disasmPreview.join("
")); - QToolTip::showText(helpEvent->globalPos(), tooltip, this, QRect(), 3500); - } - } - } - - return true; + return DisassemblyPreview::showDisasPreview(this, helpEvent->globalPos(), offsetFrom); } return MemoryDockWidget::eventFilter(obj, event); diff --git a/src/widgets/DisassemblyWidget.h b/src/widgets/DisassemblyWidget.h index 7657401d..1ab16216 100644 --- a/src/widgets/DisassemblyWidget.h +++ b/src/widgets/DisassemblyWidget.h @@ -83,7 +83,6 @@ private: RefreshDeferrer *disasmRefresh; RVA readCurrentDisassemblyOffset(); - RVA readDisassemblyOffset(QTextCursor tc); bool eventFilter(QObject *obj, QEvent *event) override; void keyPressEvent(QKeyEvent *event) override; QString getWindowTitle() const override; diff --git a/src/widgets/FunctionsWidget.cpp b/src/widgets/FunctionsWidget.cpp index c494e5a0..47c93aed 100644 --- a/src/widgets/FunctionsWidget.cpp +++ b/src/widgets/FunctionsWidget.cpp @@ -2,6 +2,7 @@ #include "ui_ListDockWidget.h" #include "core/MainWindow.h" +#include "common/DisassemblyPreview.h" #include "common/Helpers.h" #include "common/FunctionsTask.h" #include "common/TempConfig.h" @@ -634,10 +635,5 @@ void FunctionsWidget::onActionVerticalToggled(bool enable) */ void FunctionsWidget::setTooltipStylesheet() { - setStyleSheet(QString("QToolTip { border-width: 1px; max-width: %1px;" - "opacity: 230; background-color: %2;" - "color: %3; border-color: %3;}") - .arg(kMaxTooltipWidth) - .arg(Config()->getColor("gui.tooltip.background").name()) - .arg(Config()->getColor("gui.tooltip.foreground").name())); + setStyleSheet(DisassemblyPreview::getToolTipStyleSheet()); }