Skip to content

Commit

Permalink
feat: add tab width setting
Browse files Browse the repository at this point in the history
This patch allows the user to set a custom width for \t. Since we use a
monospace font we can simply use a multiple of the character width. This
is done by settings the tabstopdistance of the QTextOption property of
QTextLayout.

closes: #583
  • Loading branch information
lievenhey committed May 3, 2024
1 parent dc72a3c commit 23ca904
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/disassemblysettingspage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="2" column="0">
<item row="6" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Source Code Search Paths:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="6" column="1">
<widget class="KEditListWidget" name="sourcePaths">
<property name="buttons">
<set>KEditListWidget::All</set>
Expand All @@ -60,20 +60,30 @@
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Show hexdump:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QCheckBox" name="showHexdump">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Tab width</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSpinBox" name="tabWidth"/>
</item>
</layout>
</widget>
<customwidgets>
Expand Down
31 changes: 29 additions & 2 deletions src/models/highlightedtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "highlightedtext.h"

#include <QFontMetrics>
#include <QGuiApplication>
#include <QPalette>
#include <QTextLayout>
Expand All @@ -15,6 +16,8 @@

#include <KColorScheme>

#include <hotspot-config.h>

#if KFSyntaxHighlighting_FOUND
#include <KSyntaxHighlighting/AbstractHighlighter>
#include <KSyntaxHighlighting/Definition>
Expand Down Expand Up @@ -250,13 +253,24 @@ class HighlightedLine
m_layout = nullptr;
}

void setTabWidthInPixels(int tabWidthInPixels)
{
m_tabWidthInPixels = tabWidthInPixels;
m_layout = nullptr;
}

private:
std::unique_ptr<QTextLayout> buildLayout() const
{
Q_ASSERT(m_index != -1);
auto layout = std::make_unique<QTextLayout>();

layout->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
auto option = layout->textOption();
option.setTabStopDistance(m_tabWidthInPixels);
layout->setTextOption(option);

auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
layout->setFont(font);
layout->setText(m_text);
layout->setFormats(m_highlighter->format(m_index));

Expand All @@ -276,6 +290,7 @@ class HighlightedLine
HighlightingImplementation* m_highlighter = nullptr;
QString m_text;
int m_index = -1;
int m_tabWidthInPixels = -1; // qts default value
mutable std::unique_ptr<QTextLayout> m_layout;
};
static_assert(std::is_nothrow_move_constructible_v<HighlightedLine>);
Expand All @@ -285,7 +300,6 @@ HighlightedText::HighlightedText(KSyntaxHighlighting::Repository* repository, QO
: QObject(parent)
, m_repository(repository)
{
Q_UNUSED(repository);
}

HighlightedText::~HighlightedText() = default;
Expand Down Expand Up @@ -313,6 +327,9 @@ void HighlightedText::setText(const QStringList& text)
return HighlightedLine {m_highlighter.get(), text, index++};
});

// this is free since we currently have no text rendered
updateTabWidth(m_tabWidth);

m_cleanedLines = text;
std::for_each(m_cleanedLines.begin(), m_cleanedLines.end(), Util::removeAnsi);
}
Expand Down Expand Up @@ -358,3 +375,13 @@ void HighlightedText::updateHighlighting()
std::for_each(m_highlightedLines.begin(), m_highlightedLines.end(),
[](HighlightedLine& line) { line.updateHighlighting(); });
}

void HighlightedText::updateTabWidth(int tabWidth)
{
m_tabWidth = tabWidth;
auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
const auto tabWidthInPixels = tabWidth * QFontMetrics(font).horizontalAdvance(QLatin1Char(' '));

std::for_each(m_highlightedLines.begin(), m_highlightedLines.end(),
[tabWidthInPixels](HighlightedLine& line) { line.setTabWidthInPixels(tabWidthInPixels); });
}
4 changes: 2 additions & 2 deletions src/models/highlightedtext.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
class QTextLayout;
class QTextLine;

#include "hotspot-config.h"

namespace KSyntaxHighlighting {
class SyntaxHighlighter;
class Definition;
Expand Down Expand Up @@ -53,6 +51,7 @@ class HighlightedText : public QObject

public slots:
void updateHighlighting();
void updateTabWidth(int tabWidth);

private:
KSyntaxHighlighting::Repository* m_repository;
Expand All @@ -61,4 +60,5 @@ public slots:
QStringList m_lines;
QStringList m_cleanedLines;
bool m_isUsingAnsi = false;
int m_tabWidth = 4;
};
8 changes: 8 additions & 0 deletions src/resultsdisassemblypage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
connect(ui->assemblyView, &QTreeView::entered, this, updateFromDisassembly);
connect(ui->sourceCodeView, &QTreeView::entered, this, updateFromSource);

connect(settings, &Settings::tabWidthChanged, m_sourceCodeModel->highlightedText(),
&HighlightedText::updateTabWidth);
connect(settings, &Settings::tabWidthChanged, m_disassemblyModel->highlightedText(),
&HighlightedText::updateTabWidth);

m_sourceCodeModel->highlightedText()->updateTabWidth(settings->tabWidth());
m_disassemblyModel->highlightedText()->updateTabWidth(settings->tabWidth());

auto createContextMenu = [](QTreeView* view, auto* model, auto&& addEntries) {
auto gotoMenuWidget = new QWidget(view);
auto layout = new QHBoxLayout(gotoMenuWidget);
Expand Down
13 changes: 13 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ void Settings::loadFromFile()
connect(this, &Settings::showHexdumpChanged, [sharedConfig](bool showHexdump) {
sharedConfig->group(QStringLiteral("Disassembly")).writeEntry("showHexdump", showHexdump);
});

setTabWidth(sharedConfig->group(QStringLiteral("Disassembly")).readEntry("tabWidth", 4));
connect(this, &Settings::tabWidthChanged, [sharedConfig](int distance) {
sharedConfig->group(QStringLiteral("Disassembly")).writeEntry("tabWidth", distance);
});
}

void Settings::setSourceCodePaths(const QString& paths)
Expand Down Expand Up @@ -286,3 +291,11 @@ void Settings::setShowHexdump(bool showHexdump)
emit showHexdumpChanged(m_showHexdump);
}
}

void Settings::setTabWidth(int distance)
{
if (m_tabWidth != distance) {
m_tabWidth = distance;
emit tabWidthChanged(m_tabWidth);
}
}
8 changes: 8 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ class Settings : public QObject
return m_showHexdump;
}

int tabWidth() const
{
return m_tabWidth;
}

void loadFromFile();

signals:
Expand All @@ -187,6 +192,7 @@ class Settings : public QObject
void perfPathChanged(const QString& perfPath);
void showBranchesChanged(bool showBranches);
void showHexdumpChanged(bool showHexdump);
void tabWidthChanged(int distance);

public slots:
void setPrettifySymbols(bool prettifySymbols);
Expand All @@ -212,6 +218,7 @@ public slots:
void setPerfPath(const QString& path);
void setShowBranches(bool showBranches);
void setShowHexdump(bool showHexdump);
void setTabWidth(int distance);

private:
using QObject::QObject;
Expand All @@ -237,6 +244,7 @@ public slots:
QString m_perfMapPath;
bool m_showBranches = true;
bool m_showHexdump = false;
int m_tabWidth = 4;

QString m_lastUsedEnvironment;

Expand Down
2 changes: 2 additions & 0 deletions src/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,12 @@ void SettingsDialog::addSourcePathPage()

disassemblyPage->showBranches->setChecked(settings->showBranches());
disassemblyPage->showHexdump->setChecked(settings->showHexdump());
disassemblyPage->tabWidth->setValue(settings->tabWidth());

connect(buttonBox(), &QDialogButtonBox::accepted, this, [this, colon, settings] {
settings->setSourceCodePaths(disassemblyPage->sourcePaths->items().join(colon));
settings->setShowBranches(disassemblyPage->showBranches->isChecked());
settings->setShowHexdump(disassemblyPage->showHexdump->isChecked());
settings->setTabWidth(disassemblyPage->tabWidth->value());
});
}

0 comments on commit 23ca904

Please # to comment.