From 6fd9b3ff47af9cfb4abe6358626f11630d95f9e1 Mon Sep 17 00:00:00 2001 From: reionwong Date: Thu, 2 Sep 2021 10:04:37 +0800 Subject: [PATCH] Add font settings --- CMakeLists.txt | 1 + src/fonts.cpp | 43 +++++++++++++++++++ src/fonts.h | 50 ++++++++++++++++++++++ src/main.cpp | 2 + src/qml/GlobalSettings.qml | 1 + src/qml/SettingsDialog.qml | 86 ++++++++++++++++++++++++++++++++++++++ src/qml/Terminal.qml | 3 +- translations/en_US.ts | 12 +++++- translations/zh_CN.ts | 12 +++++- 9 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 src/fonts.cpp create mode 100644 src/fonts.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cf7c74..db0d401 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ set(SRCS src/processhelper.h src/processhelper.cpp src/utils.cpp + src/fonts.cpp ) set(RESOURCES diff --git a/src/fonts.cpp b/src/fonts.cpp new file mode 100644 index 0000000..7b846ef --- /dev/null +++ b/src/fonts.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: Reion Wong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "fonts.h" + +Fonts::Fonts(QObject *parent) : QObject(parent) +{ + init(); +} + +QStringList Fonts::families() const +{ + return m_families; +} + +void Fonts::init() +{ + m_families.clear(); + + for (const QString &family : m_fontDatabase.families()) { + if (m_fontDatabase.isFixedPitch(family)) { + m_families << family; + } + } + + emit familiesChanged(); +} diff --git a/src/fonts.h b/src/fonts.h new file mode 100644 index 0000000..b0c9282 --- /dev/null +++ b/src/fonts.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: Reion Wong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef FONTS_H +#define FONTS_H + +#include +#include +#include +#include + +class Fonts : public QObject +{ + Q_OBJECT + Q_PROPERTY(QStringList families READ families NOTIFY familiesChanged) + +public: + explicit Fonts(QObject *parent = nullptr); + + QStringList families() const; + +Q_SIGNALS: + void familiesChanged(); + +protected: + void init(); + +private: + QStringList m_families; + QFontDatabase m_fontDatabase; + +}; + +#endif // FONTS_H diff --git a/src/main.cpp b/src/main.cpp index da7a2dc..bef991b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,7 @@ #include "processhelper.h" #include "utils.h" +#include "fonts.h" int main(int argc, char *argv[]) { @@ -52,6 +53,7 @@ int main(int argc, char *argv[]) engine.rootContext()->setContextProperty("Process", new ProcessHelper); engine.rootContext()->setContextProperty("Utils", new Utils); + engine.rootContext()->setContextProperty("Fonts", new Fonts); engine.addImportPath(QStringLiteral("qrc:/")); engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); diff --git a/src/qml/GlobalSettings.qml b/src/qml/GlobalSettings.qml index 3c47950..602e345 100644 --- a/src/qml/GlobalSettings.qml +++ b/src/qml/GlobalSettings.qml @@ -24,6 +24,7 @@ Settings { property int width: 750 property int height: 500 property int fontPointSize: 10 + property string fontName: "Noto Mono" property bool blinkingCursor: true property real opacity: 1.0 diff --git a/src/qml/SettingsDialog.qml b/src/qml/SettingsDialog.qml index 7447088..bb9c92e 100644 --- a/src/qml/SettingsDialog.qml +++ b/src/qml/SettingsDialog.qml @@ -24,6 +24,92 @@ FishUI.Window { anchors.rightMargin: FishUI.Units.largeSpacing spacing: FishUI.Units.largeSpacing + Item { + Layout.fillWidth: true + Layout.preferredHeight: 45 + + Rectangle { + anchors.fill: parent + color: FishUI.Theme.secondBackgroundColor + radius: FishUI.Theme.smallRadius + } + + RowLayout { + anchors.fill: parent + anchors.leftMargin: FishUI.Units.largeSpacing + anchors.rightMargin: FishUI.Units.largeSpacing + + Label { + text: qsTr("Font") + } + + Item { + width: FishUI.Units.largeSpacing + } + + ComboBox { + id: fontsCombobox + model: Fonts.families + Layout.fillHeight: true + Layout.fillWidth: true + + onCurrentTextChanged: { + settings.fontName = currentText + } + + Component.onCompleted: { + for (var i = 0; i <= fontsCombobox.model.length; ++i) { + if (fontsCombobox.model[i] === settings.fontName) { + fontsCombobox.currentIndex = i + break + } + } + } + } + } + } + + // Font size + Item { + Layout.fillWidth: true + Layout.preferredHeight: 45 + + Rectangle { + anchors.fill: parent + color: FishUI.Theme.secondBackgroundColor + radius: FishUI.Theme.smallRadius + } + + RowLayout { + anchors.fill: parent + anchors.leftMargin: FishUI.Units.largeSpacing + anchors.rightMargin: FishUI.Units.largeSpacing + + Label { + text: qsTr("Font Size") + } + + Item { + width: FishUI.Units.largeSpacing + } + + Slider { + id: fontSizeSlider + Layout.fillHeight: true + Layout.fillWidth: true + from: 5 + to: 30 + stepSize: 1 + + Component.onCompleted: { + fontSizeSlider.value = settings.fontPointSize + } + + onValueChanged: settings.fontPointSize = fontSizeSlider.value + } + } + } + Item { Layout.fillWidth: true Layout.preferredHeight: 45 diff --git a/src/qml/Terminal.qml b/src/qml/Terminal.qml index 7a7960c..be01560 100644 --- a/src/qml/Terminal.qml +++ b/src/qml/Terminal.qml @@ -107,7 +107,7 @@ Page { id: _terminal anchors.fill: parent colorScheme: FishUI.Theme.darkMode ? "Cutefish-Dark" : "Cutefish-Light" - font.family: "Noto Sans Mono" + font.family: settings.fontName font.pointSize: settings.fontPointSize blinkingCursor: settings.blinkingCursor fullCursorHeight: true @@ -223,6 +223,7 @@ Page { anchors.right: parent.right anchors.top: parent.top anchors.bottom: parent.bottom + anchors.bottomMargin: FishUI.Units.smallSpacing * 1.5 hoverEnabled: true active: hovered || pressed orientation: Qt.Vertical diff --git a/translations/en_US.ts b/translations/en_US.ts index ef8023b..97ec77e 100644 --- a/translations/en_US.ts +++ b/translations/en_US.ts @@ -23,11 +23,21 @@ SettingsDialog + Font + + + + + Font Size + + + + Transparency - + Window Blur diff --git a/translations/zh_CN.ts b/translations/zh_CN.ts index 778b848..781577f 100644 --- a/translations/zh_CN.ts +++ b/translations/zh_CN.ts @@ -23,11 +23,21 @@ SettingsDialog + Font + 字体 + + + + Font Size + 字体大小 + + + Transparency 透明度 - + Window Blur 窗口模糊