Skip to content

Commit

Permalink
add jump to line feature to disassembler
Browse files Browse the repository at this point in the history
this allows to user to quickly jump to a location without scrolling

closes: #541
  • Loading branch information
lievenhey committed Jan 9, 2024
1 parent 38db9ee commit 2dff7c0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
64 changes: 51 additions & 13 deletions src/resultsdisassemblypage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,58 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
connect(ui->assemblyView, &QTreeView::entered, this, updateFromDisassembly);
connect(ui->sourceCodeView, &QTreeView::entered, this, updateFromSource);

ui->sourceCodeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->sourceCodeView, &QTreeView::customContextMenuRequested, this, [this](QPoint point) {
const auto index = ui->sourceCodeView->indexAt(point);
const auto fileLine = index.data(SourceCodeModel::FileLineRole).value<Data::FileLine>();
if (!fileLine.isValid())
return;
auto createContextMenu = [](QTreeView* view, auto* model, int role, auto&& addEntries) {
auto gotoMenuWidget = new QWidget(view);
auto layout = new QHBoxLayout(gotoMenuWidget);
layout->setContentsMargins(0, 0, 0, 0);
auto label = new QLabel(QCoreApplication::translate("Util", "Goto: "), gotoMenuWidget);
layout->addWidget(label);
auto edit = new QLineEdit(gotoMenuWidget);
layout->addWidget(edit);
auto gotoAction = new QWidgetAction(view);
gotoAction->setDefaultWidget(gotoMenuWidget);

auto gotoLine = [view, model, role, edit] {
const auto lineNumber = edit->text().toInt();

// model->index()->data is somehow not possible
const QModelIndex index = model->index(0, role);
const auto offset = index.data().value<int>();

auto scrollToIndex = std::clamp(lineNumber - offset, 0, model->rowCount() - 1);
view->scrollTo(model->index(scrollToIndex, 0));
};

QMenu contextMenu;
auto* openEditorAction = contextMenu.addAction(QCoreApplication::translate("Util", "Open in Editor"));
QObject::connect(openEditorAction, &QAction::triggered, &contextMenu,
[this, fileLine]() { emit navigateToCode(fileLine.file, fileLine.line, -1); });
contextMenu.exec(QCursor::pos());
});
connect(edit, &QLineEdit::returnPressed, gotoAction, &QWidgetAction::trigger);
connect(edit, &QLineEdit::returnPressed, view, gotoLine);
connect(gotoAction, &QWidgetAction::triggered, view, gotoLine);

view->setContextMenuPolicy(Qt::CustomContextMenu);
connect(view, &QTreeView::customContextMenuRequested, view, [view, addEntries, gotoAction](QPoint point) {
const auto index = view->indexAt(point);

QMenu contextMenu;
addEntries(&contextMenu, index);

contextMenu.addAction(gotoAction);
contextMenu.exec(QCursor::pos());
});
};

createContextMenu(ui->sourceCodeView, m_sourceCodeModel, SourceCodeModel::SourceCodeLineNumber,
[this](QMenu* menu, const QModelIndex& index) {
const auto fileLine = index.data(SourceCodeModel::FileLineRole).value<Data::FileLine>();
if (fileLine.isValid()) {
auto* openEditorAction =
menu->addAction(QCoreApplication::translate("Util", "Open in Editor"));
QObject::connect(openEditorAction, &QAction::triggered, menu, [this, fileLine]() {
emit navigateToCode(fileLine.file, fileLine.line, -1);
});
menu->addAction(openEditorAction);
}
});

createContextMenu(ui->assemblyView, m_disassemblyModel, DisassemblyModel::AddrRole, [](auto&&, auto&&) {});

auto addScrollTo = [](QTreeView* sourceView, QTreeView* destView, auto sourceModel, auto destModel) {
connect(sourceView, &QTreeView::clicked, sourceView, [=](const QModelIndex& index) {
Expand Down Expand Up @@ -361,7 +400,6 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
searchWidget->hide();

auto actions = new QActionGroup(view);

auto findAction = KStandardAction::find(
this,
[searchWidget, edit] {
Expand Down
20 changes: 10 additions & 10 deletions src/resultsdisassemblypage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
</property>
<property name="icon">
<iconset theme="go-previous">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
Expand All @@ -105,7 +105,7 @@
</property>
<property name="icon">
<iconset theme="go-next">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
Expand Down Expand Up @@ -195,15 +195,15 @@
<widget class="QPushButton" name="nextResult">
<property name="icon">
<iconset theme="arrow-down">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="prevResult">
<property name="icon">
<iconset theme="arrow-up">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
Expand All @@ -214,7 +214,7 @@
</property>
<property name="icon">
<iconset theme="dialog-close">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
Expand Down Expand Up @@ -260,7 +260,7 @@
<widget class="QPushButton" name="searchButton">
<property name="icon">
<iconset theme="search">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
Expand Down Expand Up @@ -360,15 +360,15 @@
<widget class="QPushButton" name="disasmNextButton">
<property name="icon">
<iconset theme="arrow-down">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="disasmPrevButton">
<property name="icon">
<iconset theme="arrow-up">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
Expand All @@ -379,7 +379,7 @@
</property>
<property name="icon">
<iconset theme="dialog-close">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
Expand Down Expand Up @@ -425,7 +425,7 @@
</property>
<property name="icon">
<iconset theme="search">
<normaloff>.</normaloff>.</iconset>
<normaloff>../../../.designer/backup</normaloff>../../../.designer/backup</iconset>
</property>
</widget>
</item>
Expand Down

0 comments on commit 2dff7c0

Please sign in to comment.