diff --git a/CMakeLists.txt b/CMakeLists.txt index 60cffd9..13bd674 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,6 +68,7 @@ set(SOURCE_FILES src/model/model.cpp src/model/model.h src/settings/settings.cpp src/settings/settings.h src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h + src/uiconfig/theme-palette.cpp src/uiconfig/theme-palette.h src/ukui-menu-application.cpp src/ukui-menu-application.h src/model/model-manager.cpp src/model/model-manager.h ) diff --git a/src/settings/settings.cpp b/src/settings/settings.cpp index b1befa7..04d60d7 100644 --- a/src/settings/settings.cpp +++ b/src/settings/settings.cpp @@ -21,10 +21,13 @@ #include #include -#define UKUI_MENU_SCHEMA "org.ukui.menu.settings" +#define UKUI_MENU_SCHEMA "org.ukui.menu.settings" +#define CONTROL_CENTER_SETTING "org.ukui.control-center.personalise" +#define CONTROL_CENTER_TRANSPARENCY_KEY "Transparency" -#define UKUI_STYLE_SCHEMA "org.ukui.style" -#define UKUI_STYLE_NAME_KEY "styleName" +#define UKUI_STYLE_SCHEMA "org.ukui.style" +#define UKUI_STYLE_NAME_KEY "styleName" +#define UKUI_STYLE_THEME_COLOR_KEY "themeColor" namespace UkuiMenu { @@ -42,6 +45,9 @@ GlobalSetting::GlobalSetting(QObject *parent) : QObject(parent) void GlobalSetting::initStyleSetting() { m_cache.insert(StyleName, UKUI_STYLE_VALUE_LIGHT); + m_cache.insert(ThemeColor, UKUI_STYLE_THEME_COLOR_KEY); + m_cache.insert(Transparency, 1); + if (QGSettings::isSchemaInstalled(UKUI_STYLE_SCHEMA)) { QGSettings *settings = new QGSettings(UKUI_STYLE_SCHEMA, {}, this); @@ -49,11 +55,33 @@ void GlobalSetting::initStyleSetting() if (keys.contains(UKUI_STYLE_NAME_KEY)) { m_cache.insert(StyleName, settings->get(UKUI_STYLE_NAME_KEY)); } + if (keys.contains(UKUI_STYLE_THEME_COLOR_KEY)) { + m_cache.insert(ThemeColor,settings->get(UKUI_STYLE_THEME_COLOR_KEY)); + } connect(settings, &QGSettings::changed, this, [this, settings] (const QString &key) { if (key == UKUI_STYLE_NAME_KEY) { updateData(StyleName, settings->get(key)); Q_EMIT styleChanged(StyleName); + } else if (key == UKUI_STYLE_THEME_COLOR_KEY) { + updateData(ThemeColor, settings->get(key)); + Q_EMIT styleChanged(ThemeColor); + } + }); + } + + if (QGSettings::isSchemaInstalled(CONTROL_CENTER_SETTING)) { + QGSettings *settings = new QGSettings(CONTROL_CENTER_SETTING,{},this); + + QStringList keys = settings->keys(); + if (keys.contains(CONTROL_CENTER_TRANSPARENCY_KEY)) { + m_cache.insert(Transparency, settings->get(CONTROL_CENTER_TRANSPARENCY_KEY).toReal()); + } + + connect(settings, &QGSettings::changed, this, [this, settings] (const QString &key) { + if (key == CONTROL_CENTER_TRANSPARENCY_KEY) { + updateData(Transparency, settings->get(key).toReal()); + Q_EMIT styleChanged(Transparency); } }); } diff --git a/src/settings/settings.h b/src/settings/settings.h index ed1e631..7549c49 100644 --- a/src/settings/settings.h +++ b/src/settings/settings.h @@ -40,7 +40,9 @@ class GlobalSetting : public QObject public: enum Key { UnKnowKey = 0, - StyleName + StyleName, + ThemeColor, + Transparency }; Q_ENUM(Key) diff --git a/src/uiconfig/theme-palette.cpp b/src/uiconfig/theme-palette.cpp new file mode 100644 index 0000000..e436ec8 --- /dev/null +++ b/src/uiconfig/theme-palette.cpp @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2022, KylinSoft Co., Ltd. + * + * 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 + * (at your option) 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 . + * + * Authors: hxf + * + */ + +#include "theme-palette.h" + +#include +#include +#include + +using namespace UkuiMenu; + +static ThemePalette *globalInstance = nullptr; + +ThemePalette *ThemePalette::getInstance() +{ + if (!globalInstance) { + globalInstance = new ThemePalette(nullptr); + } + return globalInstance; +} + +ThemePalette::ThemePalette(QObject *parent) : QObject(parent) +{ + initStyleSetting(); + connect(qGuiApp, &QGuiApplication::paletteChanged, this, [=] { + Q_EMIT styleColorChanged(); + }); +} + +QColor ThemePalette::paletteColor(Palette::ColorRole colorRole, Palette::ColorGroup colorGroup) const +{ + switch (colorRole) { + default: + case Palette::Window: + return window(colorGroup); + case Palette::WindowText: + return windowText(colorGroup); + case Palette::Base: + return base(colorGroup); + case Palette::Text: + return text(colorGroup); + case Palette::AlternateBase: + return alternateBase(colorGroup); + case Palette::Button: + return button(colorGroup); + case Palette::ButtonText: + return buttonText(colorGroup); + case Palette::Light: + return light(colorGroup); + case Palette::MidLight: + return midLight(colorGroup); + case Palette::Dark: + return dark(colorGroup); + case Palette::Mid: + return mid(colorGroup); + case Palette::Shadow: + return shadow(colorGroup); + case Palette::Highlight: + return highlight(colorGroup); + case Palette::HighlightedText: + return highlightedText(colorGroup); + } +} + +QColor +ThemePalette::paletteColorWithCustomTransparency(Palette::ColorRole colorRole, Palette::ColorGroup colorGroup, + qreal alphaF) const +{ + QColor color = paletteColor(colorRole, colorGroup); + color.setAlphaF(alphaF); + return color; +} + +QColor +ThemePalette::paletteColorWithTransparency(Palette::ColorRole colorRole, Palette::ColorGroup colorGroup) const +{ + QColor color = paletteColor(colorRole, colorGroup); + color.setAlphaF(m_transparency); + return color; +} + +QColor ThemePalette::window(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Window); +} + +QColor ThemePalette::windowText(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::WindowText); +} + +QColor ThemePalette::text(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Text); +} + +QColor ThemePalette::base(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Base); +} + +QColor ThemePalette::alternateBase(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::AlternateBase); +} + +QColor ThemePalette::button(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Button); +} + +QColor ThemePalette::buttonText(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::ButtonText); +} + +QColor ThemePalette::light(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Light); +} + +QColor ThemePalette::midLight(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Midlight); +} + +QColor ThemePalette::dark(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Dark); +} + +QColor ThemePalette::mid(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Mid); +} + +QColor ThemePalette::shadow(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Shadow); +} + +QColor ThemePalette::highlight(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Highlight); +} + +QColor ThemePalette::highlightedText(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::HighlightedText); +} + +QColor ThemePalette::separator(Palette::ColorGroup colorGroup) const +{ + return QGuiApplication::palette().color(switchColorGroup(colorGroup), QPalette::Window); +} + +QPalette::ColorGroup ThemePalette::switchColorGroup(Palette::ColorGroup colorGroup) +{ + switch (colorGroup) { + default: + case Palette::Active: + return QPalette::Active; + case Palette::Disabled: + return QPalette::Disabled; + case Palette::Inactive: + return QPalette::Inactive; + } +} + +void ThemePalette::initStyleSetting() +{ + GlobalSetting *setting = GlobalSetting::instance(); + m_transparency = setting->get(GlobalSetting::Transparency).toReal(); + connect(setting, &GlobalSetting::styleChanged, this, &ThemePalette::styleChangedSlot); +} + +void ThemePalette::styleChangedSlot(const GlobalSetting::Key& key) +{ + if (key & GlobalSetting::Transparency) { + m_transparency = GlobalSetting::instance()->get(GlobalSetting::Transparency).toReal(); + } + + Q_EMIT styleColorChanged(); +} diff --git a/src/uiconfig/theme-palette.h b/src/uiconfig/theme-palette.h new file mode 100644 index 0000000..d26c7d0 --- /dev/null +++ b/src/uiconfig/theme-palette.h @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2022, KylinSoft Co., Ltd. + * + * 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 + * (at your option) 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 . + * + * Authors: hxf + * + */ + +#ifndef UKUI_SIDEBAR_THEME_PALETTE_H +#define UKUI_SIDEBAR_THEME_PALETTE_H + +#include +#include +#include "settings.h" + +namespace UkuiMenu { + +class Palette { + Q_GADGET +public: + // Warning 警告: 谨防 qt版本更新后,删除,增加或者调整调色板的枚举值 + enum ColorGroup { + Active, Disabled, Inactive + }; + + Q_ENUM(ColorGroup) + + enum ColorRole { + Window, WindowText, Base, Text, AlternateBase, + Button, ButtonText, Light, MidLight, Dark, Mid, Shadow, + Highlight, HighlightedText + }; + + Q_ENUM(ColorRole) +}; + +class ThemePalette : public QObject { + Q_OBJECT +public: + static ThemePalette *getInstance(); + + /** + * 根据调色板的枚举值,获取主题调色板的颜色 + * @param colorRole + * @return + */ + Q_INVOKABLE QColor + paletteColor(Palette::ColorRole colorRole, Palette::ColorGroup colorGroup = Palette::Active) const; + + /** + * 获取自定义透明度的颜色 + * @param colorRole + * @return + */ + Q_INVOKABLE QColor + paletteColorWithCustomTransparency(Palette::ColorRole colorRole, Palette::ColorGroup colorGroup, + qreal alphaF) const; + + /** + * 获取带有主题透明度的颜色 + * @param colorRole + * @return + */ + Q_INVOKABLE QColor paletteColorWithTransparency(Palette::ColorRole colorRole, + Palette::ColorGroup colorGroup = Palette::Active) const; + + Q_INVOKABLE QColor window(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor windowText(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor base(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor text(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor alternateBase(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor button(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor buttonText(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor light(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor midLight(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor dark(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor mid(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor shadow(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor highlight(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor highlightedText(Palette::ColorGroup colorGroup = Palette::Active) const; + Q_INVOKABLE QColor separator(Palette::ColorGroup colorGroup = Palette::Active) const; + +Q_SIGNALS: + void styleColorChanged(); + +private Q_SLOTS: + void styleChangedSlot(const GlobalSetting::Key& key); + +private: + explicit ThemePalette(QObject *parent = nullptr); + static QPalette::ColorGroup switchColorGroup(Palette::ColorGroup colorGroup); + void initStyleSetting(); + +private: + qreal m_transparency = 1.0; +}; + +} + +#endif //UKUI_SIDEBAR_THEME_PALETTE_H