ukui-search/libsearch/global-settings.cpp

110 lines
4.2 KiB
C++
Raw Normal View History

2021-01-29 11:43:07 +08:00
/*
* Copyright (C) 2020, 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: zhangzihao <zhangzihao@kylinos.cn>
* Modified by: zhangpengfei <zhangpengfei@kylinos.cn>
*
*/
2024-01-26 14:31:56 +08:00
#include "global-settings.h"
2024-01-26 14:31:56 +08:00
#include "global-settings-private.h"
#include <QPalette>
#include <QApplication>
#include <QDBusReply>
#include <QDebug>
2024-01-26 14:31:56 +08:00
#define CONTROL_CENTER_PERSONALISE_GSETTINGS_ID "org.ukui.control-center.personalise"
#define THEME_GSETTINGS_ID "org.ukui.style"
2024-01-26 14:31:56 +08:00
using namespace UkuiSearch;
2024-01-26 14:31:56 +08:00
GlobalSettingsPrivate::GlobalSettingsPrivate(QObject *parent) : QObject(parent)
{
//全局页面透明度
//the default number of transparency for mainwindow is 0.7
setValue(TRANSPARENCY_KEY, 0.7);
2021-04-26 15:06:47 +08:00
if(QGSettings::isSchemaInstalled(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID)) {
m_transGsettings = new QGSettings(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID, QByteArray(), this);
connect(m_transGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
2021-04-26 15:06:47 +08:00
if(key == TRANSPARENCY_KEY) {
2024-01-26 14:31:56 +08:00
double transparency = m_transGsettings->get(TRANSPARENCY_KEY).toDouble();
setValue(TRANSPARENCY_KEY, transparency);
qApp->paletteChanged(qApp->palette());
2024-01-26 14:31:56 +08:00
Q_EMIT this->valueChanged(TRANSPARENCY_KEY, transparency);
}
});
if(m_transGsettings->keys().contains(TRANSPARENCY_KEY)) {
setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
}
}
//主题,字体大小
setValue(STYLE_NAME_KEY, "ukui-light");
setValue(FONT_SIZE_KEY, 11);
2021-04-26 15:06:47 +08:00
if(QGSettings::isSchemaInstalled(THEME_GSETTINGS_ID)) {
m_themeGsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this);
connect(m_themeGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
2021-04-26 15:06:47 +08:00
if(key == STYLE_NAME_KEY) {
//当前主题改变时也发出paletteChanged信号通知主界面刷新
setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
qApp->paletteChanged(qApp->palette());
2024-01-26 14:31:56 +08:00
Q_EMIT this->valueChanged(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
} else if(key == FONT_SIZE_KEY) {
setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble());
qApp->paletteChanged(qApp->palette());
2021-05-12 15:58:38 +08:00
} else if (key == ICON_THEME_KEY) {
qApp->paletteChanged(qApp->palette());
}
});
if(m_themeGsettings->keys().contains(STYLE_NAME_KEY)) {
setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
}
if(m_themeGsettings->keys().contains(FONT_SIZE_KEY)) {
setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble());
}
}
}
2024-01-26 14:31:56 +08:00
const QVariant GlobalSettingsPrivate::getValue(const QString &key) {
m_mutex.lock();
QVariant value = m_cache.value(key);
m_mutex.unlock();
return value;
}
//this method is designed for main process settings only!!
2024-01-26 14:31:56 +08:00
void GlobalSettingsPrivate::setValue(const QString &key, const QVariant &value) {
m_mutex.lock();
m_cache.insert(key, value);
m_mutex.unlock();
}
2021-03-30 09:23:57 +08:00
2024-01-26 14:31:56 +08:00
GlobalSettings &GlobalSettings::getInstance() {
static GlobalSettings instance;
return instance;
}
const QVariant GlobalSettings::getValue(const QString& key)
{
return d->getValue(key);
}
GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent), d(new GlobalSettingsPrivate(this))
{
connect(d, &GlobalSettingsPrivate::valueChanged, this, &GlobalSettings::valueChanged);
2021-03-30 09:23:57 +08:00
}