跟随主题改造

This commit is contained in:
zhaominyong 2022-02-21 11:04:44 +08:00
parent 1192c2d41a
commit 23022b2406
14 changed files with 247 additions and 35 deletions

View File

@ -2,23 +2,53 @@
#include <QPainter>
#include <QBrush>
#include <QPalette>
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
CircleLable::CircleLable(const QString& text, QWidget* parent /*= nullptr*/, int size /*= 24*/, QColor backgroundColor /*= QColor(0xCC, 0xCC, 0xCC)*/) :
QLabel(parent),
m_text(text),
m_backgroundColor(backgroundColor)
m_backgroundColor(backgroundColor),
m_bAutoTheme(false)
{
this->setFixedSize(QSize(size, size));
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
if (m_bAutoTheme) {
if (isDark) {
this->setBackgroundColor(Qt::white);
} else {
this->setBackgroundColor(Qt::black);
}
}
});
}
CircleLable::~CircleLable()
{}
/**
* @brief label的背景色
* @param backgroundColor 使Qt::blackQt::white等这种颜色表示法
*/
void CircleLable::setBackgroundColor(QColor backgroundColor)
{
// 只有黑白两色跟随主题
if (backgroundColor == QColor(Qt::black)) {
m_bAutoTheme = true;
if (g_GSettingWrapper.isDarkTheme()) {
backgroundColor = QColor(Qt::white);
}
} else if (backgroundColor == QColor(Qt::white)) {
m_bAutoTheme = true;
if (!g_GSettingWrapper.isDarkTheme()) {
backgroundColor = QColor(Qt::black);
}
} else {
m_bAutoTheme = false;
}
m_backgroundColor = backgroundColor;
// 仅仅为了引发重绘
setText("");
repaint();
}
void CircleLable::paintEvent(QPaintEvent *event)

View File

@ -20,6 +20,8 @@ protected:
private:
QString m_text;
QColor m_backgroundColor;
// 是否跟随主题
bool m_bAutoTheme;
};
#endif // CIRCLELABEL_H

View File

@ -3,14 +3,27 @@
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
/**
* @brief Label方法
* @param parent
*/
MyLabel::MyLabel(QWidget* parent) :
QLabel(parent)
QLabel(parent),
m_bAutoTheme(true)
{
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
// 只有黑白两色手动跟随主题,其它颜色不需要程序员手动设置(自动即可)
if (!m_bAutoTheme) {
if (isDark) {
this->setFontColor(Qt::white);
} else {
this->setFontColor(Qt::black);
}
}
});
}
/**
@ -19,23 +32,51 @@ MyLabel::MyLabel(QWidget* parent) :
* @param parent
* @param color
*/
MyLabel::MyLabel(const QString& text, QWidget* parent /*= nullptr*/, QColor color /*= QColor(0xCC, 0xCC, 0xCC)*/) :
MyLabel::MyLabel(const QString& text, QWidget* parent /*= nullptr*/, Qt::Alignment align /*= Qt::AlignCenter*/) :
QLabel(parent),
m_text(text)
m_text(text),
m_bAutoTheme(true)
{
QPalette palette = this->palette();
palette.setColor(QPalette::WindowText, color);
this->setPalette(palette);
this->setAlignment(Qt::AlignCenter);
this->setAlignment(align);
this->setScaledContents(true);
this->setText(text);
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
// 只有黑白两色手动跟随主题,其它颜色不需要程序员手动设置(自动即可)
if (!m_bAutoTheme) {
if (isDark) {
this->setFontColor(Qt::white);
} else {
this->setFontColor(Qt::black);
}
}
});
}
MyLabel::~MyLabel()
{}
/**
* @brief 使Qt::black这种模式的
* @param color
*/
void MyLabel::setFontColor(QColor color)
{
// 只有黑白两色手动跟随主题,其它颜色不需要程序员手动设置(自动即可)
if (color == QColor(Qt::black)) {
m_bAutoTheme = false;
if (g_GSettingWrapper.isDarkTheme()) {
color = QColor(Qt::white);
}
} else if (color == QColor(Qt::white)) {
m_bAutoTheme = false;
if (!g_GSettingWrapper.isDarkTheme()) {
color = QColor(Qt::black);
}
} else {
m_bAutoTheme = true;
}
QPalette palette = this->palette();
palette.setColor(QPalette::WindowText, color);
this->setPalette(palette);

View File

@ -9,7 +9,7 @@ class MyLabel : public QLabel
Q_OBJECT
public:
MyLabel(QWidget* parent = nullptr);
MyLabel(const QString& text, QWidget* parent = nullptr, QColor color = QColor(0xCC, 0xCC, 0xCC));
MyLabel(const QString& text, QWidget* parent = nullptr, Qt::Alignment align = Qt::AlignCenter);
virtual ~MyLabel();
void setDeplayText(const QString& text) { m_text = text; this->setText(text);}
@ -29,6 +29,9 @@ private:
bool m_isOriginal = false;
bool m_needResize = false;
QRect m_rect;
// 背景及字体颜色是否自动跟随主题
bool m_bAutoTheme;
};
#endif // MYLABEL_H

View File

@ -11,7 +11,14 @@ public:
~GlobalSignals() = default;
signals:
// 系统忙碌信号
void busy(bool isBusy);
// 主题背景变化信号
void styleNameChanged(bool isDark);
// 窗口或控件的背景色发生了变化。主要用于通知主题监控模块,用于修正控件颜色,以简化用户自定义背景色跟随主题变化的代码
void backgroundColorChanged();
};
#endif // GLOBALSIGNALS_H

View File

@ -11,10 +11,34 @@
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);
}
}
});
}
m_transparency = 0.8;
}
GSettingsWrapper::~GSettingsWrapper()

View File

@ -53,10 +53,18 @@ public:
*/
void connectUkuiBackground(QWidget * widgetPtr, QColor light, QColor dark);
/**
* @brief
* @return true-false-
*/
bool isDarkTheme() {
return m_isDarkTheme;
}
public:
float m_transparency;
QGSettings *m_pGsettingThemeData;
bool m_isDarkTheme;
};
#define g_GSettingWrapper GSettingsWrapper::inst()

View File

@ -147,17 +147,18 @@ void MainDialog::initTileBar()
backupMain->setObjectName("mainMenu");
m_menuOptionBtn->setMenu(backupMain);
QAction* backupTheme = new QAction(tr("Theme"), m_titleWidget);
backupMain->addAction(backupTheme);
QMenu* selectTheme = new QMenu( m_titleWidget);
selectTheme->setObjectName("selectTheme");
backupTheme->setMenu(selectTheme);
QAction* defaultTheme = new QAction(tr("Auto"), m_titleWidget);
selectTheme->addAction(defaultTheme);
QAction* lightTheme = new QAction(tr("Light"), m_titleWidget);
selectTheme->addAction(lightTheme);
QAction* darkTheme = new QAction(tr("Dark"), m_titleWidget);
selectTheme->addAction(darkTheme);
// 暂时只需跟随主题,浅色主题和深色主题后续可能会独立出来
// QAction* backupTheme = new QAction(tr("Theme"), m_titleWidget);
// backupMain->addAction(backupTheme);
// QMenu* selectTheme = new QMenu( m_titleWidget);
// selectTheme->setObjectName("selectTheme");
// backupTheme->setMenu(selectTheme);
// QAction* defaultTheme = new QAction(tr("Auto"), m_titleWidget);
// selectTheme->addAction(defaultTheme);
// QAction* lightTheme = new QAction(tr("Light"), m_titleWidget);
// selectTheme->addAction(lightTheme);
// QAction* darkTheme = new QAction(tr("Dark"), m_titleWidget);
// selectTheme->addAction(darkTheme);
m_backupHelp = new QAction(tr("Help"), m_titleWidget);
backupMain->addAction(m_backupHelp);

View File

@ -22,6 +22,7 @@
#include "managebackuppointlist.h"
#include "selectrestorepoint.h"
#include "messageboxutils.h"
#include "../gsettingswrapper.h"
DataBackup::DataBackup(QWidget *parent /*= nullptr*/) :
QStackedWidget(parent),
@ -59,9 +60,23 @@ void DataBackup::initFirstWidget()
// 图片
QLabel *imageBackup_firstPage = new QLabel(first);
imageBackup_firstPage->setGeometry(421, 120, 300, 326);
QPixmap pixmap(":/images/data_backup.svg");
imageBackup_firstPage->setPixmap(pixmap);
imageBackup_firstPage->setScaledContents(true);
if (g_GSettingWrapper.isDarkTheme()) {
QPixmap pixmap(":/images/data_backup_dark.svg");
imageBackup_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/data_backup.svg");
imageBackup_firstPage->setPixmap(pixmap);
}
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
if (isDark) {
QPixmap pixmap(":/images/data_backup_dark.svg");
imageBackup_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/data_backup.svg");
imageBackup_firstPage->setPixmap(pixmap);
}
});
// 系统备份大字提示
MyLabel *labelBackup_firstPage = new MyLabel(first);
@ -688,10 +703,13 @@ void DataBackup::initThirdWidget()
label1->setGeometry(11, 72, 164, 30);
MyLabel *label2 = new MyLabel(tr("preparing"), third);
label2->setGeometry(191, 72, 164, 30);
label2->setEnabled(false);
MyLabel *label3 = new MyLabel(tr("backuping"), third);
label3->setGeometry(371, 72, 164, 30);
label3->setEnabled(false);
MyLabel *label4 = new MyLabel(tr("finished"), third);
label4->setGeometry(551, 72, 164, 30);
label4->setEnabled(false);
// ------------ 中部布局begin-------------
QVBoxLayout *vlayout = new QVBoxLayout(third);
@ -1021,8 +1039,10 @@ void DataBackup::initForthWidget()
label2->setGeometry(191, 72, 164, 30);
MyLabel *label3 = new MyLabel(tr("backuping"), forth);
label3->setGeometry(371, 72, 164, 30);
label3->setEnabled(false);
MyLabel *label4 = new MyLabel(tr("finished"), forth);
label4->setGeometry(551, 72, 164, 30);
label4->setEnabled(false);
// 备份名称
MyLabel *labelBackupName = new MyLabel(forth);
@ -1206,6 +1226,7 @@ void DataBackup::initFifthWidget()
label3->setGeometry(371, 72, 164, 30);
MyLabel *label4 = new MyLabel(tr("finished"), fifth);
label4->setGeometry(551, 72, 164, 30);
label4->setEnabled(false);
// ------------ 中部布局begin-------------
QVBoxLayout *vlayout = new QVBoxLayout;

View File

@ -18,6 +18,7 @@
#include "../component/ringsprogressbar.h"
#include "../../common/utils.h"
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
#include "messageboxutils.h"
#include "selectrestorepoint.h"
@ -49,9 +50,23 @@ void DataRestore::initFirstWidget()
// 图片
QLabel *imageRestore_firstPage = new QLabel(first);
imageRestore_firstPage->setGeometry(421, 120, 300, 326);
QPixmap pixmap(":/images/data_restore.svg");
imageRestore_firstPage->setPixmap(pixmap);
imageRestore_firstPage->setScaledContents(true);
if (g_GSettingWrapper.isDarkTheme()) {
QPixmap pixmap(":/images/data_restore_dark.svg");
imageRestore_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/data_restore.svg");
imageRestore_firstPage->setPixmap(pixmap);
}
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
if (isDark) {
QPixmap pixmap(":/images/data_restore_dark.svg");
imageRestore_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/data_restore.svg");
imageRestore_firstPage->setPixmap(pixmap);
}
});
// 数据还原大字提示
MyLabel *labelRestore_firstPage = new MyLabel(first);
@ -239,8 +254,10 @@ void DataRestore::initSecondWidget()
label1->setFontColor(QColor(COLOR_BLUE));
MyLabel *label2 = new MyLabel(tr("restoring"), second);
label2->setIsOriginal(true);
label2->setEnabled(false);
MyLabel *label3 = new MyLabel(tr("finished"), second);
label3->setIsOriginal(true);
label3->setEnabled(false);
QHBoxLayout *layoutLine2 = new QHBoxLayout;
layoutLine2->addSpacing(100);
layoutLine2->addWidget(label1);
@ -562,6 +579,7 @@ void DataRestore::initThirdWidget()
label2->setFontColor(QColor(COLOR_BLUE));
MyLabel *label3 = new MyLabel(tr("finished"), third);
label3->setIsOriginal(true);
label3->setEnabled(false);
QHBoxLayout *layoutLine2 = new QHBoxLayout;
layoutLine2->addSpacing(100);
layoutLine2->addWidget(label1);

View File

@ -19,6 +19,7 @@
#include "../component/myfileselect.h"
#include "../../common/utils.h"
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
#include "selectrestorepoint.h"
#include "messageboxutils.h"
@ -56,9 +57,23 @@ void GhostImage::initFirstWidget()
// 图片
QLabel *imageGhost_firstPage = new QLabel(first);
imageGhost_firstPage->setGeometry(421, 120, 300, 326);
QPixmap pixmap(":/images/ghost_image.svg");
imageGhost_firstPage->setPixmap(pixmap);
imageGhost_firstPage->setScaledContents(true);
if (g_GSettingWrapper.isDarkTheme()) {
QPixmap pixmap(":/images/ghost_image_dark.svg");
imageGhost_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/ghost_image.svg");
imageGhost_firstPage->setPixmap(pixmap);
}
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
if (isDark) {
QPixmap pixmap(":/images/ghost_image_dark.svg");
imageGhost_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/ghost_image.svg");
imageGhost_firstPage->setPixmap(pixmap);
}
});
// Ghost Image大字提示
MyLabel *labelGhost_firstPage = new MyLabel(first);
@ -277,8 +292,10 @@ void GhostImage::initThirdWidget()
label1->setFontColor(QColor(COLOR_BLUE));
MyLabel *label2 = new MyLabel(tr("ghosting"), third);
label2->setIsOriginal(true);
label2->setEnabled(false);
MyLabel *label3 = new MyLabel(tr("finished"), third);
label3->setIsOriginal(true);
label3->setEnabled(false);
QHBoxLayout *layoutLine2 = new QHBoxLayout;
layoutLine2->addSpacing(100);
layoutLine2->addWidget(label1);
@ -655,6 +672,7 @@ void GhostImage::initForthWidget()
label2->setFontColor(QColor(COLOR_BLUE));
MyLabel *label3 = new MyLabel(tr("finished"), forth);
label3->setIsOriginal(true);
label3->setEnabled(false);
QHBoxLayout *layoutLine2 = new QHBoxLayout;
layoutLine2->addSpacing(100);
layoutLine2->addWidget(label1);

View File

@ -17,6 +17,7 @@
#include "../component/ringsprogressbar.h"
#include "../../common/utils.h"
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
#include "managebackuppointlist.h"
#include "messageboxutils.h"
@ -55,9 +56,23 @@ void SystemBackup::initFirstWidget()
// 图片
QLabel *imageBackup_firstPage = new QLabel(first);
imageBackup_firstPage->setGeometry(421, 120, 300, 326);
QPixmap pixmap(":/images/system_backup.svg");
imageBackup_firstPage->setPixmap(pixmap);
imageBackup_firstPage->setScaledContents(true);
if (g_GSettingWrapper.isDarkTheme()) {
QPixmap pixmap(":/images/system_backup_dark.svg");
imageBackup_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/system_backup.svg");
imageBackup_firstPage->setPixmap(pixmap);
}
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
if (isDark) {
QPixmap pixmap(":/images/system_backup_dark.svg");
imageBackup_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/system_backup.svg");
imageBackup_firstPage->setPixmap(pixmap);
}
});
// 系统备份大字提示
MyLabel *labelBackup_firstPage = new MyLabel(first);
@ -281,10 +296,13 @@ void SystemBackup::initThirdWidget()
label1->setGeometry(11, 72, 164, 30);
MyLabel *label2 = new MyLabel(tr("preparing"), third);
label2->setGeometry(191, 72, 164, 30);
label2->setEnabled(false);
MyLabel *label3 = new MyLabel(tr("backuping"), third);
label3->setGeometry(371, 72, 164, 30);
label3->setEnabled(false);
MyLabel *label4 = new MyLabel(tr("finished"), third);
label4->setGeometry(551, 72, 164, 30);
label4->setEnabled(false);
// ------------ 中部布局begin-------------
QVBoxLayout *vlayout = new QVBoxLayout(third);
@ -613,8 +631,10 @@ void SystemBackup::initForthWidget()
label2->setGeometry(191, 72, 164, 30);
MyLabel *label3 = new MyLabel(tr("backuping"), forth);
label3->setGeometry(371, 72, 164, 30);
label3->setEnabled(false);
MyLabel *label4 = new MyLabel(tr("finished"), forth);
label4->setGeometry(551, 72, 164, 30);
label4->setEnabled(false);
// 备份名称
MyLabel *labelBackupName = new MyLabel(forth);
@ -798,6 +818,7 @@ void SystemBackup::initFifthWidget()
label3->setGeometry(371, 72, 164, 30);
MyLabel *label4 = new MyLabel(tr("finished"), fifth);
label4->setGeometry(551, 72, 164, 30);
label4->setEnabled(false);
// ------------ 中部布局begin-------------
QVBoxLayout *vlayout = new QVBoxLayout;

View File

@ -18,6 +18,7 @@
#include "../component/ringsprogressbar.h"
#include "../../common/utils.h"
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
#include "messageboxutils.h"
#include "selectrestorepoint.h"
@ -51,9 +52,23 @@ void SystemRestore::initFirstWidget()
// 图片
QLabel *imageRestore_firstPage = new QLabel(first);
imageRestore_firstPage->setGeometry(421, 120, 300, 326);
QPixmap pixmap(":/images/system_restore.svg");
imageRestore_firstPage->setPixmap(pixmap);
imageRestore_firstPage->setScaledContents(true);
if (g_GSettingWrapper.isDarkTheme()) {
QPixmap pixmap(":/images/system_restore_dark.svg");
imageRestore_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/system_restore.svg");
imageRestore_firstPage->setPixmap(pixmap);
}
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
if (isDark) {
QPixmap pixmap(":/images/system_restore_dark.svg");
imageRestore_firstPage->setPixmap(pixmap);
} else {
QPixmap pixmap(":/images/system_restore.svg");
imageRestore_firstPage->setPixmap(pixmap);
}
});
// 系统还原大字提示
MyLabel *labelRestore_firstPage = new MyLabel(first);
@ -257,8 +272,10 @@ void SystemRestore::initSecondWidget()
label1->setFontColor(QColor(COLOR_BLUE));
MyLabel *label2 = new MyLabel(tr("restoring"), second);
label2->setIsOriginal(true);
label2->setEnabled(false);
MyLabel *label3 = new MyLabel(tr("finished"), second);
label3->setIsOriginal(true);
label3->setEnabled(false);
QHBoxLayout *layoutLine2 = new QHBoxLayout;
layoutLine2->addSpacing(100);
layoutLine2->addWidget(label1);
@ -588,6 +605,7 @@ void SystemRestore::initThirdWidget()
label2->setFontColor(QColor(COLOR_BLUE));
MyLabel *label3 = new MyLabel(tr("finished"), third);
label3->setIsOriginal(true);
label3->setEnabled(false);
QHBoxLayout *layoutLine2 = new QHBoxLayout;
layoutLine2->addSpacing(100);
layoutLine2->addWidget(label1);

View File

@ -125,7 +125,7 @@
<message>
<location filename="module/databackup.cpp" line="147"/>
<source>Update Backup</source>
<translation></translation>
<translation></translation>
</message>
<message>
<location filename="module/databackup.cpp" line="180"/>