From 4a06534d39b186f58589cd53e29d658246e8f29f Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 28 Jan 2022 20:05:55 +0800 Subject: [PATCH] - enhancement: display localized theme name in the option dialog --- NEWS.md | 1 + .../environmentappearencewidget.cpp | 37 ++++++----- .../environmentappearencewidget.h | 4 ++ RedPandaIDE/thememanager.cpp | 65 ++++++++++++++++--- RedPandaIDE/thememanager.h | 6 ++ RedPandaIDE/themes/dark.json | 1 + RedPandaIDE/themes/default.json | 3 +- RedPandaIDE/utils.cpp | 1 - 8 files changed, 92 insertions(+), 26 deletions(-) diff --git a/NEWS.md b/NEWS.md index b1d9b1ee..2ed0d86f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ Red Panda C++ Version 0.14.1 - enhancement: custom theme - fix: failed to show function tip, when there are parameters having '[' and ']' + - enhancement: display localized theme name in the option dialog Red Panda C++ Version 0.14.0 - enhancement: custom icon set ( in the configuration folder) diff --git a/RedPandaIDE/settingsdialog/environmentappearencewidget.cpp b/RedPandaIDE/settingsdialog/environmentappearencewidget.cpp index 1a509188..2db98cdf 100644 --- a/RedPandaIDE/settingsdialog/environmentappearencewidget.cpp +++ b/RedPandaIDE/settingsdialog/environmentappearencewidget.cpp @@ -28,17 +28,6 @@ EnvironmentAppearenceWidget::EnvironmentAppearenceWidget(const QString& name, co ui(new Ui::EnvironmentAppearenceWidget) { ui->setupUi(this); - ui->cbTheme->addItem("default"); - ui->cbTheme->addItem("dark"); -// ui->cbTheme->addItem("dracula"); -// ui->cbTheme->addItem("light"); -// QStyleFactory factory; -// for (QString name:factory.keys()) { -// ui->cbTheme->addItem(name); -// } - ui->cbLanguage->addItem(tr("English"),"en"); - ui->cbLanguage->addItem(tr("Simplified Chinese"),"zh_CN"); - ui->cbIconSet->addItem("newlook"); } EnvironmentAppearenceWidget::~EnvironmentAppearenceWidget() @@ -48,7 +37,12 @@ EnvironmentAppearenceWidget::~EnvironmentAppearenceWidget() void EnvironmentAppearenceWidget::doLoad() { - ui->cbTheme->setCurrentText(pSettings->environment().theme()); + for (int i=0; icbTheme->count();i++) { + if (ui->cbTheme->itemData(i) == pSettings->environment().theme()) { + ui->cbTheme->setCurrentIndex(i); + break; + } + } ui->cbFont->setCurrentFont(QFont(pSettings->environment().interfaceFont())); ui->spinFontSize->setValue(pSettings->environment().interfaceFontSize()); ui->cbIconSet->setCurrentText(pSettings->environment().iconSet()); @@ -65,15 +59,15 @@ void EnvironmentAppearenceWidget::doLoad() void EnvironmentAppearenceWidget::doSave() { - if (pSettings->environment().theme()!=ui->cbTheme->currentText()) { + if (pSettings->environment().theme()!=ui->cbTheme->currentData().toString()) { ThemeManager themeManager; - PAppTheme appTheme = themeManager.theme(ui->cbTheme->currentText()); + PAppTheme appTheme = themeManager.theme(ui->cbTheme->currentData().toString()); if (appTheme && !appTheme->defaultColorScheme().isEmpty()) { pSettings->editor().setColorScheme(appTheme->defaultColorScheme()); pMainWindow->updateEditorColorSchemes(); } } - pSettings->environment().setTheme(ui->cbTheme->currentText()); + pSettings->environment().setTheme(ui->cbTheme->currentData().toString()); pSettings->environment().setInterfaceFont(ui->cbFont->currentFont().family()); pSettings->environment().setInterfaceFontSize(ui->spinFontSize->value()); pSettings->environment().setLanguage(ui->cbLanguage->currentData().toString()); @@ -85,3 +79,16 @@ void EnvironmentAppearenceWidget::doSave() pSettings->environment().save(); pMainWindow->applySettings(); } + +void EnvironmentAppearenceWidget::init() +{ + ThemeManager themeManager; + QList appThemes = themeManager.getThemes(); + foreach(const PAppTheme& appTheme, appThemes) { + ui->cbTheme->addItem(appTheme->displayName(),appTheme->name()); + } + ui->cbLanguage->addItem(tr("English"),"en"); + ui->cbLanguage->addItem(tr("Simplified Chinese"),"zh_CN"); + ui->cbIconSet->addItem("newlook"); + SettingsWidget::init(); +} diff --git a/RedPandaIDE/settingsdialog/environmentappearencewidget.h b/RedPandaIDE/settingsdialog/environmentappearencewidget.h index 27f67a07..5495ad5b 100644 --- a/RedPandaIDE/settingsdialog/environmentappearencewidget.h +++ b/RedPandaIDE/settingsdialog/environmentappearencewidget.h @@ -39,6 +39,10 @@ private: protected: void doLoad() override; void doSave() override; + + // SettingsWidget interface +public: + void init() override; }; #endif // ENVIRONMENTAPPEARENCEWIDGET_H diff --git a/RedPandaIDE/thememanager.cpp b/RedPandaIDE/thememanager.cpp index 65ea3199..a5732ef6 100644 --- a/RedPandaIDE/thememanager.cpp +++ b/RedPandaIDE/thememanager.cpp @@ -16,6 +16,7 @@ */ #include "thememanager.h" #include +#include #include #include #include @@ -24,6 +25,7 @@ #include #include "utils.h" #include "settings.h" +#include "systemconsts.h" ThemeManager::ThemeManager(QObject *parent) : QObject(parent), mUseCustomTheme(false) @@ -33,14 +35,16 @@ ThemeManager::ThemeManager(QObject *parent) : QObject(parent), PAppTheme ThemeManager::theme(const QString &themeName) { - PAppTheme appTheme = std::make_shared(); - QString themeDir; - if (mUseCustomTheme) - themeDir = pSettings->dirs().config(Settings::Dirs::DataType::Theme); - else - themeDir = pSettings->dirs().data(Settings::Dirs::DataType::Theme); - appTheme->load(QString("%1/%2.json").arg(themeDir, themeName)); - return appTheme; + if (mUseCustomTheme) + prepareCustomeTheme(); + PAppTheme appTheme = std::make_shared(); + QString themeDir; + if (mUseCustomTheme) + themeDir = pSettings->dirs().config(Settings::Dirs::DataType::Theme); + else + themeDir = pSettings->dirs().data(Settings::Dirs::DataType::Theme); + appTheme->load(QString("%1/%2.json").arg(themeDir, themeName)); + return appTheme; } bool ThemeManager::useCustomTheme() const @@ -61,6 +65,34 @@ void ThemeManager::prepareCustomeTheme() copyFolder(pSettings->dirs().data(Settings::Dirs::DataType::Theme),pSettings->dirs().config(Settings::Dirs::DataType::Theme)); } +QList ThemeManager::getThemes() +{ + if (mUseCustomTheme) + prepareCustomeTheme(); + + QList result; + QString themeDir; + if (mUseCustomTheme) + themeDir = pSettings->dirs().config(Settings::Dirs::DataType::Theme); + else + themeDir = pSettings->dirs().data(Settings::Dirs::DataType::Theme); + QDirIterator it(themeDir); + while (it.hasNext()) { + it.next(); + QFileInfo fileInfo = it.fileInfo(); + if (fileInfo.suffix().compare("json", PATH_SENSITIVITY)==0) { + try { + PAppTheme appTheme = std::make_shared(); + appTheme->load(fileInfo.absoluteFilePath()); + result.append(appTheme); + } catch(FileError e) { + //just skip it + } + } + } + return result; +} + AppTheme::AppTheme(QObject *parent):QObject(parent) { @@ -156,7 +188,12 @@ void AppTheme::load(const QString &filename) .arg(error.errorString())); } QJsonObject obj=doc.object(); - mName = obj["name"].toString(); + QFileInfo fileInfo(filename); + mName = fileInfo.baseName(); + mDisplayName = obj["name"].toString(); + QString localeName = obj["name_"+pSettings->environment().language()].toString(); + if (!localeName.isEmpty()) + mDisplayName = localeName; mIsDark = obj["isDark"].toBool(false); mDefaultColorScheme = obj["default scheme"].toString(); QJsonObject colors = obj["palette"].toObject(); @@ -200,6 +237,16 @@ QPalette AppTheme::initialPalette() return palette; } +const QString &AppTheme::name() const +{ + return mName; +} + +const QString &AppTheme::displayName() const +{ + return mDisplayName; +} + const QString &AppTheme::defaultColorScheme() const { return mDefaultColorScheme; diff --git a/RedPandaIDE/thememanager.h b/RedPandaIDE/thememanager.h index f09cbeb7..2c04dc8e 100644 --- a/RedPandaIDE/thememanager.h +++ b/RedPandaIDE/thememanager.h @@ -85,11 +85,16 @@ public: const QString &defaultColorScheme() const; void setDefaultColorScheme(const QString &newDefaultColorScheme); + const QString &displayName() const; + + const QString &name() const; + private: static QPalette initialPalette(); private: QHash mColors; QString mName; + QString mDisplayName; bool mIsDark; QString mDefaultColorScheme; }; @@ -105,6 +110,7 @@ public: bool useCustomTheme() const; void setUseCustomTheme(bool newUseCustomTheme); void prepareCustomeTheme(); + QList getThemes(); private: bool mUseCustomTheme; diff --git a/RedPandaIDE/themes/dark.json b/RedPandaIDE/themes/dark.json index 110ec9e3..50f2dafc 100644 --- a/RedPandaIDE/themes/dark.json +++ b/RedPandaIDE/themes/dark.json @@ -1,5 +1,6 @@ { "name":"dark", + "name_zh_CN": "深色主题", "isDark": true, "default scheme": "VS Code", "palette": { diff --git a/RedPandaIDE/themes/default.json b/RedPandaIDE/themes/default.json index 0f6f5dd4..f6e83631 100644 --- a/RedPandaIDE/themes/default.json +++ b/RedPandaIDE/themes/default.json @@ -1,5 +1,6 @@ { - "name":"default", + "name":"light", + "name_zh_CN":"浅色主题", "isDark":false, "default scheme": "Intellij Classic", "palette": { diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index 3b899332..c2683509 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -1113,7 +1113,6 @@ void copyFolder(const QString &fromDir, const QString &toDir) return; targetDir.mkpath(targetDir.absolutePath()); - qDebug()<<"copy folder"; while (it.hasNext()){ it.next(); const auto fileInfo = it.fileInfo();