yhkylin-backup-tools/kybackup/gsettingswrapper.cpp

121 lines
5.4 KiB
C++
Executable File

/**
* @brief 由于当前仅用于主题变化时,备份还原工具的一些图标跟随变化,此处暂时先简单封装。后续根据使用情况另行优化。
*/
#include "gsettingswrapper.h"
#include <QIcon>
#include <QLabel>
#include "globalsignals.h"
#include "globalbackupinfo.h"
#include "../common/mydefine.h"
GSettingsWrapper::GSettingsWrapper(token)
{
m_isDarkTheme = false;
if (QGSettings::isSchemaInstalled(ORG_UKUI_STYLE)) {
m_pGsettingThemeData = new QGSettings(ORG_UKUI_STYLE);
QString themeStyleCurr = this->m_pGsettingThemeData->get(STYLE_NAME).toString();
if (STYLE_NAME_VALUE_DARK == themeStyleCurr || STYLE_NAME_VALUE_BLACK == themeStyleCurr) {
// 深色主题
m_isDarkTheme = true;
} else if (STYLE_NAME_VALUE_DEFAULT == themeStyleCurr || STYLE_NAME_VALUE_LIGHT == themeStyleCurr || STYLE_NAME_VALUE_WHITE == themeStyleCurr) {
// 浅色主题
m_isDarkTheme = false;
}
QObject::connect(m_pGsettingThemeData, &QGSettings::changed, [=](const QString &key) {
// 主题变更
if (key == STYLE_NAME) {
QString themeStyle = this->m_pGsettingThemeData->get(STYLE_NAME).toString();
if (STYLE_NAME_VALUE_DARK == themeStyle || STYLE_NAME_VALUE_BLACK == themeStyle) {
m_isDarkTheme = true;
// 深色主题
emit GlobelBackupInfo::inst().getGlobalSignals()->styleNameChanged(true);
} else if (STYLE_NAME_VALUE_DEFAULT == themeStyle || STYLE_NAME_VALUE_LIGHT == themeStyle || STYLE_NAME_VALUE_WHITE == themeStyle) {
m_isDarkTheme = false;
// 浅色主题
emit GlobelBackupInfo::inst().getGlobalSignals()->styleNameChanged(false);
}
}
});
}
}
GSettingsWrapper::~GSettingsWrapper()
{
delete m_pGsettingThemeData;
}
/**
* @brief 绑定连接UKui风格的主题
* @param QWidget *, 跟随风格变化的窗体对象指针
*/
void GSettingsWrapper::connectUkuiIconSchema(QWidget * widgetPtr, QSize size)
{
if (widgetPtr != nullptr && QGSettings::isSchemaInstalled(ORG_UKUI_STYLE)) {
QObject::connect(m_pGsettingThemeData, &QGSettings::changed, [=](const QString &key) {
if (key == ICON_THEME_NAME) {
QIcon titleIcon = QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS);
if (widgetPtr->inherits("QLabel")) {
QLabel *labelPtr = qobject_cast<QLabel *>(widgetPtr);
const QPixmap * pixmapPtr = labelPtr->pixmap();
if (pixmapPtr != nullptr) {
labelPtr->setPixmap(titleIcon.pixmap(titleIcon.actualSize(size)));
} else {
labelPtr->setPixmap(titleIcon.pixmap(titleIcon.actualSize(QSize(24, 24))));
}
} else {
widgetPtr->setWindowIcon(titleIcon);
}
}
});
}
}
/**
* @brief 绑定连接UKui风格的背景颜色
* @param widgetPtr, 跟随风格变化的窗体对象指针
* @param light 浅色
* @param dark 深色
*/
void GSettingsWrapper::connectUkuiBackground(QWidget * widgetPtr, QColor light, QColor dark)
{
if (widgetPtr != nullptr && QGSettings::isSchemaInstalled(ORG_UKUI_STYLE)) {
QObject::connect(m_pGsettingThemeData, &QGSettings::changed, [=](const QString &key) {
// 主题变更
if (key == STYLE_NAME) {
widgetPtr->setAutoFillBackground(true);
QPalette palette = widgetPtr->palette();
// 只有跟随主题时,才会变化
if (GlobelBackupInfo::BlackgroundMode::Auto == GlobelBackupInfo::inst().getBlackgroundMode()) {
QString themeStyle = this->m_pGsettingThemeData->get(STYLE_NAME).toString();
if (STYLE_NAME_VALUE_DARK == themeStyle || STYLE_NAME_VALUE_BLACK == themeStyle) {
// 深色主题
palette.setColor(QPalette::Window, dark);
palette.setColor(QPalette::WindowText, Qt::white);
palette.setColor(QPalette::ButtonText, Qt::white);
} else if (STYLE_NAME_VALUE_DEFAULT == themeStyle || STYLE_NAME_VALUE_LIGHT == themeStyle || STYLE_NAME_VALUE_WHITE == themeStyle) {
// 浅色主题
palette.setColor(QPalette::Window, light);
palette.setColor(QPalette::WindowText, Qt::black);
palette.setColor(QPalette::ButtonText, Qt::black);
}
} else if (GlobelBackupInfo::BlackgroundMode::Dark == GlobelBackupInfo::inst().getBlackgroundMode()) {
// 深色主题
palette.setColor(QPalette::Window, dark);
palette.setColor(QPalette::WindowText, Qt::white);
palette.setColor(QPalette::ButtonText, Qt::white);
} else if (GlobelBackupInfo::BlackgroundMode::Light == GlobelBackupInfo::inst().getBlackgroundMode()) {
// 浅色主题
palette.setColor(QPalette::Window, light);
palette.setColor(QPalette::WindowText, Qt::black);
palette.setColor(QPalette::ButtonText, Qt::black);
}
widgetPtr->setPalette(palette);
}
});
}
}