添加访问调色板功能

This commit is contained in:
qiqi 2022-12-12 17:00:30 +08:00 committed by hewenfei
parent 0e321fc17e
commit 7aec1a8561
5 changed files with 348 additions and 4 deletions

View File

@ -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
)

View File

@ -22,9 +22,12 @@
#include <QDebug>
#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_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);
}
});
}

View File

@ -40,7 +40,9 @@ class GlobalSetting : public QObject
public:
enum Key {
UnKnowKey = 0,
StyleName
StyleName,
ThemeColor,
Transparency
};
Q_ENUM(Key)

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* Authors: hxf <hewenfei@kylinos.cn>
*
*/
#include "theme-palette.h"
#include <QGuiApplication>
#include <QColor>
#include <QPalette>
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();
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* Authors: hxf <hewenfei@kylinos.cn>
*
*/
#ifndef UKUI_SIDEBAR_THEME_PALETTE_H
#define UKUI_SIDEBAR_THEME_PALETTE_H
#include <QObject>
#include <QPalette>
#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