1.更新翻译,2.增加全局设置读取功能

This commit is contained in:
hewenfei 2022-12-07 16:05:15 +08:00
parent 2d8e7e85c2
commit b13e9846fc
5 changed files with 42 additions and 66 deletions

View File

@ -26,45 +26,40 @@
namespace UkuiMenu {
StyleGSetting::StyleGSetting(QObject *parent) : QObject(parent)
GlobalSetting *GlobalSetting::instance()
{
init();
static GlobalSetting setting(nullptr);
return &setting;
}
QVariant StyleGSetting::get(StyleGSetting::Key key)
GlobalSetting::GlobalSetting(QObject *parent) : QObject(parent)
{
initStyleSetting();
}
void GlobalSetting::initStyleSetting()
{
m_cache.insert(StyleName, UKUI_STYLE_VALUE_LIGHT);
if (QGSettings::isSchemaInstalled(UKUI_STYLE_SCHEMA)) {
QGSettings *settings = new QGSettings(UKUI_STYLE_SCHEMA, {}, this);
QStringList keys = settings->keys();
if (keys.contains(UKUI_STYLE_NAME_KEY)) {
m_cache.insert(StyleName, settings->get(UKUI_STYLE_NAME_KEY));
}
connect(settings, &QGSettings::changed, this, [this, settings] (const QString &key) {
if (key == UKUI_STYLE_NAME_KEY) {
m_cache.insert(StyleName, settings->get(key));
Q_EMIT styleChanged(StyleName);
}
});
}
}
QVariant GlobalSetting::get(const GlobalSetting::Key& key)
{
return m_cache.value(key);
}
void StyleGSetting::init()
{
m_cache.insert(StyleName, UKUI_STYLE_VALUE_LIGHT);
if (QGSettings::isSchemaInstalled(UKUI_STYLE_SCHEMA)) {
m_GSettings = new QGSettings(UKUI_STYLE_SCHEMA, {}, this);
QStringList keys = m_GSettings->keys();
if (keys.contains(UKUI_STYLE_NAME_KEY)) {
valueChangedSlot(UKUI_STYLE_NAME_KEY);
}
connect(m_GSettings, &QGSettings::changed,
this, &StyleGSetting::valueChangedSlot);
}
}
void StyleGSetting::valueChangedSlot(const QString &key)
{
if (key == UKUI_STYLE_NAME_KEY) {
m_cache.insert(StyleName, m_GSettings->get(key).toString());
Q_EMIT valueChanged(StyleName);
}
}
StyleGSetting *StyleGSetting::instance()
{
static StyleGSetting setting(nullptr);
return &setting;
}
} // UkuiMenu

View File

@ -29,7 +29,7 @@
namespace UkuiMenu {
class StyleGSetting : public QObject
class GlobalSetting : public QObject
{
Q_OBJECT
public:
@ -39,27 +39,22 @@ public:
};
Q_ENUM(Key)
static StyleGSetting *instance();
StyleGSetting() = delete;
StyleGSetting(const StyleGSetting& obj) = delete;
StyleGSetting(const StyleGSetting&& obj) = delete;
static GlobalSetting *instance();
GlobalSetting() = delete;
GlobalSetting(const GlobalSetting& obj) = delete;
GlobalSetting(const GlobalSetting&& obj) = delete;
public Q_SLOTS:
QVariant get(StyleGSetting::Key key);
QVariant get(const GlobalSetting::Key& key);
Q_SIGNALS:
void valueChanged(const StyleGSetting::Key& key);
private Q_SLOTS:
void valueChangedSlot(const QString& key);
void styleChanged(const GlobalSetting::Key& key);
private:
explicit StyleGSetting(QObject *parent = nullptr);
void init();
explicit GlobalSetting(QObject *parent = nullptr);
void initStyleSetting();
private:
QGSettings *m_GSettings{nullptr};
QMap<StyleGSetting::Key, QVariant> m_cache;
QMap<GlobalSetting::Key, QVariant> m_cache;
};
} // UkuiMenu

View File

@ -65,8 +65,8 @@ QColor ColorHelper::colorWithAlpha(ColorRole::Role role, qreal alpha)
void ColorHelper::initStyleMonitor()
{
updateStyle();
connect(StyleGSetting::instance(), &StyleGSetting::valueChanged, this , [this] (const StyleGSetting::Key& key) {
if (key & StyleGSetting::StyleName) {
connect(GlobalSetting::instance(), &GlobalSetting::styleChanged, this , [this] (const GlobalSetting::Key& key) {
if (key & GlobalSetting::StyleName) {
updateStyle();
}
});
@ -76,7 +76,7 @@ void ColorHelper::initStyleMonitor()
void ColorHelper::updateStyle()
{
QString styleName = StyleGSetting::instance()->get(StyleGSetting::StyleName).toString();
QString styleName = GlobalSetting::instance()->get(GlobalSetting::StyleName).toString();
m_isDarkStyle = (styleName != UKUI_STYLE_VALUE_LIGHT);
Q_EMIT colorChanged();

View File

@ -1,13 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="bo_CN">
<context>
<name>QObject</name>
<message>
<source>i am a text.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>main</name>
<message>

View File

@ -1,13 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
<name>QObject</name>
<message>
<source>i am a text.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>main</name>
<message>