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 { 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); 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 } // UkuiMenu

View File

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

View File

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

View File

@ -1,13 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS> <!DOCTYPE TS>
<TS version="2.1" language="bo_CN"> <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> <context>
<name>main</name> <name>main</name>
<message> <message>

View File

@ -1,13 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS> <!DOCTYPE TS>
<TS version="2.1" language="zh_CN"> <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> <context>
<name>main</name> <name>main</name>
<message> <message>