Skip to content

Commit

Permalink
feat: add tab with setting
Browse files Browse the repository at this point in the history
closes: #583
  • Loading branch information
lievenhey committed May 2, 2024
1 parent dc72a3c commit aa6de85
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 24 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
4 changes: 2 additions & 2 deletions src/models/disassemblymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "search.h"
#include "sourcecodemodel.h"

DisassemblyModel::DisassemblyModel(KSyntaxHighlighting::Repository* repository, QObject* parent)
DisassemblyModel::DisassemblyModel(KSyntaxHighlighting::Repository* repository, int tabWidth, QObject* parent)
: QAbstractTableModel(parent)
, m_highlightedText(repository)
, m_highlightedText(repository, tabWidth)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/disassemblymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DisassemblyModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit DisassemblyModel(KSyntaxHighlighting::Repository* repository, QObject* parent = nullptr);
explicit DisassemblyModel(KSyntaxHighlighting::Repository* repository, int tabWidth = 8, QObject* parent = nullptr);
~DisassemblyModel() override;

void setDisassembly(const DisassemblyOutput& disassemblyOutput, const Data::CallerCalleeResults& results);
Expand Down
33 changes: 28 additions & 5 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 @@ -229,10 +232,11 @@ class HighlightedLine
{
public:
HighlightedLine() = default;
HighlightedLine(HighlightingImplementation* highlighter, const QString& text, int index)
HighlightedLine(HighlightingImplementation* highlighter, const QString& text, int index, int tabWidth)
: m_highlighter(highlighter)
, m_text(Util::removeAnsi(text))
, m_index(index)
, m_tabWidth(tabWidth)
, m_layout(nullptr)
{
}
Expand All @@ -250,13 +254,25 @@ class HighlightedLine
m_layout = nullptr;
}

void setTabWidth(int tabWidth)
{
m_tabWidth = tabWidth;
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 font = QFontDatabase::systemFont(QFontDatabase::FixedFont);

auto option = layout->textOption();
option.setTabStopDistance(m_tabWidth * QFontMetrics(font).horizontalAdvance(QLatin1Char(' ')));
layout->setTextOption(option);

layout->setFont(font);
layout->setText(m_text);
layout->setFormats(m_highlighter->format(m_index));

Expand All @@ -276,16 +292,17 @@ class HighlightedLine
HighlightingImplementation* m_highlighter = nullptr;
QString m_text;
int m_index = -1;
int m_tabWidth = 8;
mutable std::unique_ptr<QTextLayout> m_layout;
};
static_assert(std::is_nothrow_move_constructible_v<HighlightedLine>);
static_assert(std::is_nothrow_destructible_v<HighlightedLine>);

HighlightedText::HighlightedText(KSyntaxHighlighting::Repository* repository, QObject* parent)
HighlightedText::HighlightedText(KSyntaxHighlighting::Repository* repository, int tabWidth, QObject* parent)
: QObject(parent)
, m_repository(repository)
, m_tabWidth(tabWidth)
{
Q_UNUSED(repository);
}

HighlightedText::~HighlightedText() = default;
Expand All @@ -310,7 +327,7 @@ void HighlightedText::setText(const QStringList& text)
m_highlighter->formatText(text);
int index = 0;
std::transform(text.cbegin(), text.cend(), m_highlightedLines.begin(), [this, &index](const QString& text) {
return HighlightedLine {m_highlighter.get(), text, index++};
return HighlightedLine {m_highlighter.get(), text, index++, m_tabWidth};
});

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

void HighlightedText::updateTabWidth(int tabWidth)
{
std::for_each(m_highlightedLines.begin(), m_highlightedLines.end(),
[tabWidth](HighlightedLine& line) { line.setTabWidth(tabWidth); });
}
6 changes: 3 additions & 3 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 All @@ -29,7 +27,7 @@ class HighlightedText : public QObject
{
Q_OBJECT
public:
HighlightedText(KSyntaxHighlighting::Repository* repository, QObject* parent = nullptr);
HighlightedText(KSyntaxHighlighting::Repository* repository, int tabWidth, QObject* parent = nullptr);
~HighlightedText() override;

void setText(const QStringList& text);
Expand All @@ -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 = 8;
};
4 changes: 2 additions & 2 deletions src/models/sourcecodemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

Q_LOGGING_CATEGORY(sourcecodemodel, "hotspot.sourcecodemodel", QtWarningMsg)

SourceCodeModel::SourceCodeModel(KSyntaxHighlighting::Repository* repository, QObject* parent)
SourceCodeModel::SourceCodeModel(KSyntaxHighlighting::Repository* repository, int tabWidth, QObject* parent)
: QAbstractTableModel(parent)
, m_highlightedText(repository)
, m_highlightedText(repository, tabWidth)
{
qRegisterMetaType<QTextLine>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/sourcecodemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SourceCodeModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit SourceCodeModel(KSyntaxHighlighting::Repository* repository, QObject* parent = nullptr);
explicit SourceCodeModel(KSyntaxHighlighting::Repository* repository, int tabWidth = 8, QObject* parent = nullptr);
~SourceCodeModel() override;

void clear();
Expand Down
11 changes: 7 additions & 4 deletions src/resultsdisassemblypage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
, ui(std::make_unique<Ui::ResultsDisassemblyPage>())
#if KFSyntaxHighlighting_FOUND
, m_repository(std::make_unique<KSyntaxHighlighting::Repository>())
, m_disassemblyModel(new DisassemblyModel(m_repository.get(), this))
, m_sourceCodeModel(new SourceCodeModel(m_repository.get(), this))
, m_disassemblyModel(new DisassemblyModel(m_repository.get(), 8, this))
, m_sourceCodeModel(new SourceCodeModel(m_repository.get(), 8, this))
#else
, m_disassemblyModel(new DisassemblyModel(nullptr, this))
, m_sourceCodeModel(new SourceCodeModel(nullptr, this))
, m_disassemblyModel(new DisassemblyModel(nullptr, 8, this))
, m_sourceCodeModel(new SourceCodeModel(nullptr, 8, this))
#endif
, m_disassemblyCostDelegate(new CostDelegate(DisassemblyModel::CostRole, DisassemblyModel::TotalCostRole, this))
, m_sourceCodeCostDelegate(new CostDelegate(SourceCodeModel::CostRole, SourceCodeModel::TotalCostRole, this))
Expand Down Expand Up @@ -274,6 +274,9 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
connect(ui->assemblyView, &QTreeView::entered, this, updateFromDisassembly);
connect(ui->sourceCodeView, &QTreeView::entered, this, updateFromSource);

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

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", 8));
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 = 8;

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->setTabStopDistance(disassemblyPage->tabWidth->value());
});
}
4 changes: 2 additions & 2 deletions tests/modeltests/tst_formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private slots:
QFETCH(QStringList, ansiStrings);
QFETCH(QVector<QVector<QTextLayout::FormatRange>>, formatting);

HighlightedText highlighter(nullptr);
HighlightedText highlighter(nullptr, 0, nullptr);

highlighter.setText(ansiStrings);

Expand Down Expand Up @@ -101,7 +101,7 @@ private slots:

auto repository = std::make_unique<KSyntaxHighlighting::Repository>();

HighlightedText text(repository.get());
HighlightedText text(repository.get(), 0);
text.setText(testfunc);
text.setDefinition(repository->definitionForFileName(QStringLiteral("test.cpp")));

Expand Down

0 comments on commit aa6de85

Please # to comment.