yhkylin-backup-tools/kybackup/component/myiconbutton.cpp

160 lines
6.4 KiB
C++
Raw Normal View History

2021-09-16 16:05:46 +08:00
#include "myiconbutton.h"
#include <QFontMetrics>
#include <QHBoxLayout>
#include <QStyleOptionButton>
2021-09-23 11:21:58 +08:00
#include <QApplication>
#include <QPainter>
#include "imageutil.h"
2022-02-23 12:10:39 +08:00
#include "../gsettingswrapper.h"
2022-03-08 11:17:02 +08:00
#include "../globalbackupinfo.h"
2021-09-16 16:05:46 +08:00
MyIconButton::MyIconButton(QWidget *parent) :
QPushButton(parent)
{
2022-02-22 15:25:28 +08:00
this->setCheckable(false);
this->setProperty("useButtonPalette", true);
m_hoverColor = pluginBtnHoverColor(true);
m_clickColor = pluginBtnHoverColor(false);
if (!this->isChecked())
this->setStyleSheet(QString("QPushButton:hover{background-color:%1;border-radius: 6px;}"
"QPushButton:pressed{background-color:%2;border-radius: 6px;}").arg(m_hoverColor).arg(m_clickColor));
m_pixSize = 16;
2022-02-15 16:51:03 +08:00
2021-09-16 16:05:46 +08:00
m_iconButton = new QPushButton(this);
2022-02-23 12:10:39 +08:00
m_iconButton->setCheckable(true);
2021-09-16 16:05:46 +08:00
m_iconButton->setFixedSize(QSize(24, 24));
m_iconButton->setFocusPolicy(Qt::NoFocus);
m_iconButton->setFlat(true);
QString iconBtnQss = QString("QPushButton:checked{border: none;}"
"QPushButton:!checked{border: none;}");
m_iconButton->setStyleSheet(iconBtnQss);
2021-09-16 16:05:46 +08:00
2021-10-13 16:31:58 +08:00
m_textLabel = new MyLabel(this);
2021-09-16 16:05:46 +08:00
QSizePolicy textLabelPolicy = m_textLabel->sizePolicy();
textLabelPolicy.setHorizontalPolicy(QSizePolicy::Fixed);
textLabelPolicy.setVerticalPolicy(QSizePolicy::Fixed);
m_textLabel->setSizePolicy(textLabelPolicy);
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->setContentsMargins(8, 0, 0, 0);
hLayout->addWidget(m_iconButton, Qt::AlignCenter);
hLayout->addWidget(m_textLabel);
hLayout->addStretch();
setLayout(hLayout);
connect(m_iconButton, &QPushButton::clicked, this, [=]() {
2022-03-08 11:17:02 +08:00
this->click();
m_iconButton->setChecked(this->isChecked());
});
connect(m_iconButton, &QPushButton::toggled, this, [=] (bool checked) {
if (checked || g_GSettingWrapper.isDarkTheme())
m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "white", m_pixSize));
else
m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "default", m_pixSize));
2022-03-08 11:17:02 +08:00
});
2022-03-08 11:17:02 +08:00
connect(this, &MyIconButton::toggled, this, [=](bool checked) {
m_iconButton->setChecked(this->isChecked());
if (checked) {
this->setStyleSheet("QPushButton:checked{background-color: palette(highlight);border-radius: 6px;}");
m_textLabel->setStyleSheet("color:white");
} else {
this->setStyleSheet(QString("QPushButton:hover{background-color:%1;border-radius: 6px;}"
"QPushButton:pressed{background-color:%2;border-radius: 6px;}").arg(m_hoverColor).arg(m_clickColor));
m_textLabel->setStyleSheet("color:palette(windowText)");
}
2022-03-08 11:17:02 +08:00
});
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
Q_UNUSED(isDark)
// 深浅主题切换时,因为调色板已经更换,高亮等颜色已经改变,所以要重新加载图标。
if (this->isChecked() || isDark)
m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "white", m_pixSize));
else
m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "default", m_pixSize));
m_hoverColor = pluginBtnHoverColor(true);
m_clickColor = pluginBtnHoverColor(false);
if (!this->isChecked())
this->setStyleSheet(QString("QPushButton:hover{background-color:%1;border-radius: 6px;}"
"QPushButton:pressed{background-color:%2;border-radius: 6px;}").arg(m_hoverColor).arg(m_clickColor));
2022-03-08 11:17:02 +08:00
});
2021-09-16 16:05:46 +08:00
}
MyIconButton::~MyIconButton()
{}
2021-10-13 16:31:58 +08:00
void MyIconButton::setThemeIcon(const QString &themeIconName, const QString &defaultIconName, int size)
2021-09-16 16:05:46 +08:00
{
m_themeIconName = themeIconName;
2021-10-13 16:31:58 +08:00
m_defaultIconName = defaultIconName;
m_pixSize = size;
2021-09-23 11:21:58 +08:00
m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "default", m_pixSize));
2021-09-16 16:05:46 +08:00
}
void MyIconButton::setDesplayText(const QString &text)
{
2021-09-23 11:21:58 +08:00
m_originalText = text;
2021-10-13 16:31:58 +08:00
m_textLabel->setDeplayText(text);
2021-09-16 16:05:46 +08:00
}
/**
* 使qss了
*/
2021-09-23 11:21:58 +08:00
void MyIconButton::changePalette(bool checked)
{
2022-02-23 12:10:39 +08:00
m_iconButton->setChecked(checked);
QPalette pal = m_textLabel->palette();
2022-02-26 14:42:00 +08:00
// png格式的图标会自动跟随主题不需再手动设置像素颜色
2022-02-23 12:10:39 +08:00
if (g_GSettingWrapper.isDarkTheme()) {
if (checked) {
pal.setColor(QPalette::ButtonText, this->palette().highlightedText().color());
} else {
pal.setColor(QPalette::ButtonText, this->palette().windowText().color());
}
2021-09-23 11:21:58 +08:00
} else {
2022-02-23 12:10:39 +08:00
if (checked) {
pal.setColor(QPalette::ButtonText, this->palette().highlightedText().color());
} else {
pal.setColor(QPalette::ButtonText, this->palette().windowText().color());
}
2021-09-23 11:21:58 +08:00
}
m_textLabel->setPalette(pal);
}
QString MyIconButton::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;
}