优化自定义控件背景色展示部分

This commit is contained in:
zhaominyong 2022-03-30 11:22:13 +08:00
parent 52dbd4c861
commit a2f0d5007a
6 changed files with 122 additions and 39 deletions

View File

@ -18,12 +18,14 @@
*
*/
#include "clicklabel.h"
#include <QColor>
#include "../globalsignals.h"
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
ClickLabel::ClickLabel(const QString &text, QWidget *parent) :
ClickLabel::ClickLabel(QWidget *parent) :
QLabel(parent)
{
setText(text);
adjustSize();
}
ClickLabel::~ClickLabel()
@ -31,7 +33,58 @@ ClickLabel::~ClickLabel()
}
void ClickLabel::mousePressEvent(QMouseEvent *event){
if (event->button() == Qt::LeftButton)
if (event->button() == Qt::LeftButton) {
event->accept();
emit clicked();
// QLabel::mousePressEvent(event);
} else {
QLabel::mousePressEvent(event);
}
}
// 光标悬浮时是否变换颜色
void ClickLabel::setChangeHover()
{
// 设置hover背景色
QString hoverColor = pluginBtnHoverColor(true);
this->setStyleSheet(QString("ClickLabel:hover{background-color:%1;border-radius: 6px;}").arg(hoverColor));
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=]() {
// 深浅主题切换时,因为调色板已经更换,高亮等颜色已经改变,所以要重新加载图标。
QString hoverColor = pluginBtnHoverColor(true);
this->setStyleSheet(QString("ClickLabel:hover{background-color:%1;border-radius: 6px;}").arg(hoverColor));
});
}
QString ClickLabel::pluginBtnHoverColor(bool hoverFlag)
{
QColor color1, color2;
if (this->parent()) {
QWidget * parent = qobject_cast<QWidget *>(this->parent());
color1 = parent->palette().color(QPalette::Active, QPalette::Button);
color2 = parent->palette().color(QPalette::Active, QPalette::BrightText);
} else {
color1 = palette().color(QPalette::Active, QPalette::Button);
color2 = palette().color(QPalette::Active, QPalette::BrightText);
}
QColor color;
qreal r,g,b,a;
QString hoverColor;
if ((g_GSettingWrapper.isDarkTheme() && hoverFlag) ||
(!g_GSettingWrapper.isDarkTheme() && !hoverFlag)) {
r = color1.redF() * 0.8 + color2.redF() * 0.2;
g = color1.greenF() * 0.8 + color2.greenF() * 0.2;
b = color1.blueF() * 0.8 + color2.blueF() * 0.2;
a = color1.alphaF() * 0.8 + color2.alphaF() * 0.2;
} else {
r = color1.redF() * 0.95 + color2.redF() * 0.05;
g = color1.greenF() * 0.95 + color2.greenF() * 0.05;
b = color1.blueF() * 0.95 + color2.blueF() * 0.05;
a = color1.alphaF() * 0.95 + color2.alphaF() * 0.05;
}
color = QColor::fromRgbF(r, g, b, a);
hoverColor = QString("rgba(%1, %2, %3, %4)").arg(color.red())
.arg(color.green())
.arg(color.blue())
.arg(color.alpha());
return hoverColor;
}

View File

@ -21,6 +21,7 @@
#define CLICKLABEL_H
#include <QLabel>
#include <QEvent>
#include <QMouseEvent>
class ClickLabel : public QLabel
@ -28,14 +29,22 @@ class ClickLabel : public QLabel
Q_OBJECT
public:
explicit ClickLabel(const QString &text, QWidget *parent = 0);
explicit ClickLabel(QWidget *parent = nullptr);
~ClickLabel();
// 光标悬浮时是否变换颜色
void setChangeHover();
protected:
void mousePressEvent(QMouseEvent * event);
QString pluginBtnHoverColor(bool hoverFlag);
signals:
void clicked();
private:
bool m_changeHoverColor;
};
#endif // CLICKLABEL_H

View File

@ -6,8 +6,10 @@
#include "../../common/mydefine.h"
PixmapLabel::PixmapLabel(QWidget *parent) :
QLabel(parent)
{}
ClickLabel(parent)
{
setAlignment(Qt::AlignCenter);
}
PixmapLabel::~PixmapLabel()
{}
@ -46,9 +48,26 @@ void PixmapLabel::setUkuiIconSchema(const QString &schema, QSize size)
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged);
}
void PixmapLabel::on_themeIconChanged()
void PixmapLabel::setUkuiIconSchema(const QString &schema, const QString &schemaDefault, QSize size)
{
QIcon titleIcon = QIcon::fromTheme(m_iconTheme);
setPixmap(titleIcon.pixmap(titleIcon.actualSize(m_iconSize)));
m_iconTheme = schema;
m_iconThemeDefault = schemaDefault;
m_iconSize = size;
on_themeIconChanged();
disconnect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged);
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged);
}
void PixmapLabel::on_themeIconChanged()
{
if (m_iconThemeDefault.isEmpty()) {
QIcon titleIcon = QIcon::fromTheme(m_iconTheme);
setPixmap(titleIcon.pixmap(titleIcon.actualSize(m_iconSize)));
} else {
QIcon titleIcon = QIcon::fromTheme(m_iconTheme, QIcon(m_iconThemeDefault));
setPixmap(titleIcon.pixmap(titleIcon.actualSize(m_iconSize)));
}
}

View File

@ -1,10 +1,10 @@
#ifndef PIXMAPLABEL_H
#define PIXMAPLABEL_H
#include <QLabel>
#include <QSize>
#include "clicklabel.h"
class PixmapLabel : public QLabel
class PixmapLabel : public ClickLabel
{
Q_OBJECT
@ -17,6 +17,8 @@ public:
// 设置跟随主题图标
void setUkuiIconSchema(const QString &schema, QSize size = QSize(24, 24));
// 设置跟随主题图标
void setUkuiIconSchema(const QString &schema, const QString &schemaDefault, QSize size = QSize(16, 16));
private slots:
void on_styleNameChanged(bool isDark);
@ -30,6 +32,7 @@ private:
// 主题图标
QString m_iconTheme;
QString m_iconThemeDefault;
// 主题图标大小
QSize m_iconSize;
};

View File

@ -310,8 +310,6 @@ void DataBackup::initSecondWidget()
// label:选择备份数据
MyLabel* labelDataBackup = new MyLabel;
labelDataBackup->setDeplayText(tr("Select backup data"));
// labelDataBackup->setFixedWidth(600);
// labelDataBackup->setFixedHeight(27);
labelDataBackup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
labelDataBackup->setFont(font);
hlayoutLine3->addWidget(labelDataBackup);
@ -324,30 +322,29 @@ void DataBackup::initSecondWidget()
hlayoutLine4->addSpacing(30);
// 所选备份路径编辑框
QLineEdit *editSelect = new QLineEdit;
// editSelect->setMinimumWidth(460);
editSelect->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
editSelect->setMaxLength(255);
editSelect->setTextMargins(0, 0, 62, 0);
QHBoxLayout *searchLayout = new QHBoxLayout(editSelect);
searchLayout->setMargin(2);
searchLayout->setMargin(5);
searchLayout->setSpacing(0);
// 删除按钮
PixmapButton *buttonDelete = new PixmapButton;
buttonDelete->setFixedSize(30, 30);
buttonDelete->setThemeIcon("window-close-symbolic", ":/symbos/window-close-symbolic.png");
PixmapLabel *buttonDelete = new PixmapLabel;
buttonDelete->setFixedSize(24, 24);
buttonDelete->setUkuiIconSchema("window-close-symbolic", ":/symbos/window-close-symbolic.png");
buttonDelete->setToolTip(tr("Clear"));
buttonDelete->setFlat(true);
connect(buttonDelete, &QPushButton::clicked, this, [=]() {
buttonDelete->setChangeHover();
connect(buttonDelete, &PixmapLabel::clicked, this, [=]() {
editSelect->setText("");
});
// 添加按钮
PixmapButton *buttonAdd = new PixmapButton;
buttonAdd->setFixedSize(30, 30);
buttonAdd->setThemeIcon("object-select-symbolic", ":/symbos/object-select-symbolic.png");
PixmapLabel *buttonAdd = new PixmapLabel;
buttonAdd->setFixedSize(24, 24);
buttonAdd->setUkuiIconSchema("object-select-symbolic", ":/symbos/object-select-symbolic.png");
buttonAdd->setToolTip(tr("Add"));
buttonAdd->setFlat(true);
buttonAdd->setChangeHover();
searchLayout->addWidget(buttonDelete);
searchLayout->addWidget(buttonAdd);
searchLayout->setSpacing(0);
searchLayout->setAlignment(Qt::AlignRight);
buttonDelete->setVisible(false);
buttonAdd->setVisible(false);
@ -425,7 +422,7 @@ void DataBackup::initSecondWidget()
vlayout->addSpacing(20);
second->setLayout(vlayout);
connect(buttonAdd, &QPushButton::clicked, this, [=]() {
connect(buttonAdd, &PixmapLabel::clicked, this, [=]() {
if (editSelect->text().isEmpty())
MessageBoxUtils::QMESSAGE_BOX_WARNING(this, QObject::tr("Warning"),
QObject::tr("Please select a backup file or directory"),
@ -568,23 +565,25 @@ void DataBackup::initSecondWidget_inc()
editSelect->setMaxLength(255);
editSelect->setTextMargins(0, 0, 62, 0);
QHBoxLayout *searchLayout = new QHBoxLayout(editSelect);
searchLayout->setMargin(2);
searchLayout->setMargin(5);
searchLayout->setSpacing(0);
// 删除按钮
PixmapButton *buttonDelete = new PixmapButton;
buttonDelete->setFixedSize(30, 30);
buttonDelete->setThemeIcon("window-close-symbolic", ":/symbos/window-close-symbolic.png");
PixmapLabel *buttonDelete = new PixmapLabel;
buttonDelete->setFixedSize(24, 24);
buttonDelete->setUkuiIconSchema("window-close-symbolic", ":/symbos/window-close-symbolic.png");
buttonDelete->setToolTip(tr("Clear"));
connect(buttonDelete, &QPushButton::clicked, this, [=]() {
buttonDelete->setChangeHover();
connect(buttonDelete, &PixmapLabel::clicked, this, [=]() {
editSelect->setText("");
});
// 添加按钮
PixmapButton *buttonAdd = new PixmapButton;
buttonAdd->setFixedSize(30, 30);
buttonAdd->setThemeIcon("object-select-symbolic", ":/symbos/object-select-symbolic.png");
PixmapLabel *buttonAdd = new PixmapLabel;
buttonAdd->setFixedSize(24, 24);
buttonAdd->setUkuiIconSchema("object-select-symbolic", ":/symbos/object-select-symbolic.png");
buttonAdd->setToolTip(tr("Add"));
buttonAdd->setChangeHover();
searchLayout->addWidget(buttonDelete);
searchLayout->addWidget(buttonAdd);
searchLayout->setSpacing(0);
searchLayout->setAlignment(Qt::AlignRight);
buttonDelete->setVisible(false);
buttonAdd->setVisible(false);
@ -650,7 +649,7 @@ void DataBackup::initSecondWidget_inc()
vlayout->addSpacing(20);
second->setLayout(vlayout);
connect(buttonAdd, &QPushButton::clicked, this, [=]() {
connect(buttonAdd, &PixmapLabel::clicked, this, [=]() {
if (editSelect->text().isEmpty())
MessageBoxUtils::QMESSAGE_BOX_WARNING(this, QObject::tr("Warning"),
QObject::tr("Please select a backup file or directory"),

View File

@ -154,7 +154,7 @@ void SystemBackup::initFirstWidget()
first->setLayout(bottomVBoxLayout);
connect(backupPointManage, &MyPushButton::clicked, this, [=]() {
ManageBackupPointList backupManager(first, ManageBackupPointList::DATA);
ManageBackupPointList backupManager(first, ManageBackupPointList::SYSTEM);
backupManager.exec();
});
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {