160 lines
6.4 KiB
C++
Executable File
160 lines
6.4 KiB
C++
Executable File
#include "myiconbutton.h"
|
||
#include <QFontMetrics>
|
||
#include <QHBoxLayout>
|
||
#include <QStyleOptionButton>
|
||
#include <QApplication>
|
||
#include <QPainter>
|
||
#include "imageutil.h"
|
||
#include "../gsettingswrapper.h"
|
||
#include "../globalbackupinfo.h"
|
||
|
||
MyIconButton::MyIconButton(QWidget *parent) :
|
||
QPushButton(parent)
|
||
{
|
||
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;
|
||
|
||
m_iconButton = new QPushButton(this);
|
||
m_iconButton->setCheckable(true);
|
||
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);
|
||
|
||
m_textLabel = new MyLabel(this);
|
||
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, [=]() {
|
||
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));
|
||
});
|
||
|
||
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)");
|
||
}
|
||
});
|
||
|
||
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));
|
||
});
|
||
}
|
||
|
||
MyIconButton::~MyIconButton()
|
||
{}
|
||
|
||
void MyIconButton::setThemeIcon(const QString &themeIconName, const QString &defaultIconName, int size)
|
||
{
|
||
m_themeIconName = themeIconName;
|
||
m_defaultIconName = defaultIconName;
|
||
m_pixSize = size;
|
||
|
||
m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "default", m_pixSize));
|
||
}
|
||
|
||
void MyIconButton::setDesplayText(const QString &text)
|
||
{
|
||
m_originalText = text;
|
||
m_textLabel->setDeplayText(text);
|
||
}
|
||
|
||
/**
|
||
* 废弃,改为使用qss了
|
||
*/
|
||
void MyIconButton::changePalette(bool checked)
|
||
{
|
||
m_iconButton->setChecked(checked);
|
||
QPalette pal = m_textLabel->palette();
|
||
// png格式的图标会自动跟随主题,不需再手动设置像素颜色
|
||
if (g_GSettingWrapper.isDarkTheme()) {
|
||
if (checked) {
|
||
pal.setColor(QPalette::ButtonText, this->palette().highlightedText().color());
|
||
} else {
|
||
pal.setColor(QPalette::ButtonText, this->palette().windowText().color());
|
||
}
|
||
} else {
|
||
if (checked) {
|
||
pal.setColor(QPalette::ButtonText, this->palette().highlightedText().color());
|
||
} else {
|
||
pal.setColor(QPalette::ButtonText, this->palette().windowText().color());
|
||
}
|
||
}
|
||
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;
|
||
}
|