Skip to content

Commit

Permalink
Implement colour theme selection
Browse files Browse the repository at this point in the history
  • Loading branch information
vicr123 committed May 16, 2024
1 parent fd9ce10 commit da48c35
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 13 deletions.
1 change: 1 addition & 0 deletions application/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ qt_add_qml_module(theterminal
QML_FILES settings/RootProfileSettings.qml
QML_FILES settings/SingleProfileSettings.qml
SOURCES settings/fontmodel.h settings/fontmodel.cpp
SOURCES settings/colormodel.h settings/colormodel.cpp
)

set_target_properties(theterminal PROPERTIES
Expand Down
36 changes: 36 additions & 0 deletions application/settings/SingleProfileSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Item {
id: fontModel
}

ColorModel {
id: colorModel
}

Grandstand {
id: grandstand
anchors.top: parent.top
Expand Down Expand Up @@ -121,6 +125,38 @@ Item {
}
}

GroupBox {
Layout.alignment: Qt.AlignHCenter
implicitWidth: 600
title: qsTr("Colours")

GridLayout {
columns: 2

Label {
text: qsTr("Theme")
}

ComboBox {
id: colorsBox
Layout.fillWidth: true

model: colorModel
textRole: "description"
valueRole: "identifier"

onActivated: () => {
profile.colorName = colorsBox.currentValue;
profile.saveProfile();
}

Component.onCompleted: () => {
colorsBox.currentIndex = colorsBox.indexOfValue(profile.colorName)
}
}
}
}

Item {
Layout.fillHeight: true
}
Expand Down
46 changes: 46 additions & 0 deletions application/settings/colormodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "colormodel.h"

#include <screencolormanager.h>

struct ColorModelPrivate {
};

ColorModel::ColorModel(QObject* parent) :
QAbstractListModel(parent), d{new ColorModelPrivate()} {
}

ColorModel::~ColorModel() {
delete d;
}

int ColorModel::rowCount(const QModelIndex& parent) const {
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;

// FIXME: Implement me!
return ScreenColorManager::definitions().length();
}

QVariant ColorModel::data(const QModelIndex& index, int role) const {
if (!index.isValid())
return QVariant();

auto definition = ScreenColorManager::definitions().value(index.row());
switch (role) {
case Description:
return ScreenColorManager::name(definition);
case Identifier:
return definition;
}

return QVariant();
}

QHash<int, QByteArray> ColorModel::roleNames() const {
return {
{Description, "description"},
{Identifier, "identifier" }
};
}
30 changes: 30 additions & 0 deletions application/settings/colormodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef COLORMODEL_H
#define COLORMODEL_H

#include <QAbstractListModel>
#include <QQmlComponent>

struct ColorModelPrivate;
class ColorModel : public QAbstractListModel {
Q_OBJECT
QML_ELEMENT

public:
explicit ColorModel(QObject* parent = nullptr);
~ColorModel();

enum ColorRoles {
Description = Qt::DisplayRole,
Identifier = Qt::UserRole
};

// Basic functionality:
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;

private:
ColorModelPrivate* d;
};

#endif // COLORMODEL_H
28 changes: 20 additions & 8 deletions application/translations/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,41 @@
<context>
<name>SingleProfileSettings</name>
<message>
<location filename="../settings/SingleProfileSettings.qml" line="60"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="60"/>
<location filename="../settings/SingleProfileSettings.qml" line="64"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="64"/>
<source>Shell</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../settings/SingleProfileSettings.qml" line="64"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="64"/>
<location filename="../settings/SingleProfileSettings.qml" line="68"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="68"/>
<source>Set the shell that is used when you start a terminal with this profile</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../settings/SingleProfileSettings.qml" line="83"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="83"/>
<location filename="../settings/SingleProfileSettings.qml" line="87"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="87"/>
<source>Text</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../settings/SingleProfileSettings.qml" line="89"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="89"/>
<location filename="../settings/SingleProfileSettings.qml" line="93"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="93"/>
<source>Font</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../settings/SingleProfileSettings.qml" line="131"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="131"/>
<source>Colours</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../settings/SingleProfileSettings.qml" line="137"/>
<location filename="../../../build-theterminal-Desktop_ARM-Debug/application/com/vicr123/theterminal/settings/SingleProfileSettings.qml" line="137"/>
<source>Theme</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>main</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ QmlTerminalScreenController::QmlTerminalScreenController(QObject* parent) :
});
connect(d->terminalScreen, &TerminalScreen::historyRolled, this, [this] {
for (auto i = 0; i < d->terminalScreen->rows(); i++) {
// if (d->cachedRuns.contains(i + 1)) {
// d->cachedRuns.insert(i, d->cachedRuns.value(i + 1));
// }
queueRowUpdate(i);
}
// d->cachedRuns.remove(d->terminalScreen->rows() - 1);
queueRowUpdate(d->terminalScreen->rows() - 1);
});
connect(d->terminalScreen, &TerminalScreen::invertScreenChanged, this, [this] {
Expand Down Expand Up @@ -347,4 +343,10 @@ void QmlTerminalScreenController::setColorName(QString colorName) {
d->colorName = colorName;
d->screenColorManager.loadColorDefinition(colorName);
emit colorNameChanged();

// Update all rows
for (auto i = 0; i < d->terminalScreen->rows(); i++) {
queueRowUpdate(i);
}
emit scrollbackLinesChanged();
}
2 changes: 1 addition & 1 deletion libtheterminal/common/colorschemes/PowerShell.colorscheme
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ Color=204,204,204
[General]
Blur=false
ColorRandomization=false
Description=Campbell
Description=Campbell (PowerShell)
Opacity=1
Wallpaper=
25 changes: 25 additions & 0 deletions libtheterminal/common/screencolormanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ ScreenColorManager::~ScreenColorManager() {
delete d;
}

QStringList ScreenColorManager::definitions() {
return {
"Campbell",
"GreenOnBlack",
"Linux",
"PowerShell",
"Solarized",
"SolarizedLight",
"Ubuntu"};
}

QString ScreenColorManager::name(QString definition) {
auto file = definition;
if (!definition.startsWith("/")) {
// Interpret as an internal resource
file = QStringLiteral(":/com/vicr123/theterminal/libtheterminal/colorschemes/%1.colorscheme").arg(definition);
}

// Colors are packed into d->colors as 0xAABBGGRR

QSettings settings(file, QSettings::IniFormat);
auto k = settings.allKeys();
return settings.value("Description", definition).toString();
}

QColor ScreenColorManager::decodeColor(quint32 color) {
switch (decodeColorMode(color)) {
case 0: // 24 bit color
Expand Down
3 changes: 3 additions & 0 deletions libtheterminal/common/screencolormanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class LIBTHETERMINAL_COMMON_EXPORT ScreenColorManager : public QObject {
explicit ScreenColorManager(QObject* parent = nullptr);
~ScreenColorManager();

static QStringList definitions();
static QString name(QString definition);

using Color = quint32;
using ColorSection = quint8;

Expand Down

0 comments on commit da48c35

Please sign in to comment.