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 Dec 12, 2023
1 parent 1811ed5 commit c7984f6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/resultsdisassemblypage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
ui->sourceCodeView->setMouseTracking(true);
setCostHeader(ui->sourceCodeView);

ui->gotoWidget->hide();

connect(ui->gotoLineEdit, &QLineEdit::returnPressed, this, [this]() {
int lineNumber = ui->gotoLineEdit->text().toInt();
int offset = m_sourceCodeModel->index(0, SourceCodeModel::SourceCodeLineNumber).data().value<int>();
// TODO: range validation, maybe, qt should handle out of range, at least test
ui->sourceCodeView->scrollTo(m_sourceCodeModel->index(lineNumber - offset, 0));
ui->gotoWidget->hide();
});

auto settings = Settings::instance();
m_sourceCodeModel->setSysroot(settings->sysroot());

Expand All @@ -275,8 +285,24 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
connect(ui->assemblyView, &QTreeView::entered, this, updateFromDisassembly);
connect(ui->sourceCodeView, &QTreeView::entered, this, updateFromSource);

auto gotoMenuWidget = new QWidget(this);
auto layout = new QHBoxLayout(gotoMenuWidget);
auto label = new QLabel(QStringLiteral("Goto: "), gotoMenuWidget);
layout->addWidget(label);
auto edit = new QLineEdit(gotoMenuWidget);
layout->addWidget(edit);
auto gotoAction = new QWidgetAction(this);
gotoAction->setDefaultWidget(gotoMenuWidget);

connect(edit, &QLineEdit::returnPressed, this, [this, edit] {
int lineNumber = edit->text().toInt();
int offset = m_sourceCodeModel->index(0, SourceCodeModel::SourceCodeLineNumber).data().value<int>();
// TODO: range validation, maybe, qt should handle out of range, at least test
ui->sourceCodeView->scrollTo(m_sourceCodeModel->index(lineNumber - offset, 0));
});

ui->sourceCodeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->sourceCodeView, &QTreeView::customContextMenuRequested, this, [this](QPoint point) {
connect(ui->sourceCodeView, &QTreeView::customContextMenuRequested, this, [this, gotoAction](QPoint point) {
const auto index = ui->sourceCodeView->indexAt(point);
const auto fileLine = index.data(SourceCodeModel::FileLineRole).value<Data::FileLine>();
if (!fileLine.isValid())
Expand All @@ -286,6 +312,10 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
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); });

// TODO: fix focus, pressing enter will trigger both actions
contextMenu.addAction(gotoAction);

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

Expand Down Expand Up @@ -363,6 +393,13 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,

auto actions = new QActionGroup(view);

auto action = new QAction(view);
action->setShortcut(Qt::Key_F6);
connect(action, &QAction::triggered, this, [this] { ui->gotoWidget->show(); });
action->setActionGroup(actions);
action->setShortcutContext(Qt::ShortcutContext::WidgetWithChildrenShortcut);
view->addAction(action);

auto findAction = KStandardAction::find(
this,
[searchWidget, edit] {
Expand Down
34 changes: 34 additions & 0 deletions src/resultsdisassemblypage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,40 @@
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="gotoWidget" native="true">
<property name="enabled">
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Go to line:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="gotoLineEdit"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="CopyableTreeView" name="sourceCodeView">
<property name="alternatingRowColors">
Expand Down

0 comments on commit c7984f6

Please sign in to comment.