2021-09-16 16:05:46 +08:00
|
|
|
|
#include "myiconbutton.h"
|
|
|
|
|
#include <QFontMetrics>
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
#include <QStyleOptionButton>
|
|
|
|
|
#include <QPainter>
|
2021-09-23 11:21:58 +08:00
|
|
|
|
#include <QApplication>
|
2022-02-23 14:50:21 +08:00
|
|
|
|
// #include "imageutil.h"
|
2022-02-23 12:10:39 +08:00
|
|
|
|
#include "../gsettingswrapper.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);
|
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);
|
|
|
|
|
|
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);
|
2022-03-08 09:56:06 +08:00
|
|
|
|
// m_textLabel->setScaledContents(true);
|
2021-09-16 16:05:46 +08:00
|
|
|
|
|
|
|
|
|
QHBoxLayout *hLayout = new QHBoxLayout();
|
|
|
|
|
hLayout->setContentsMargins(8, 0, 0, 0);
|
|
|
|
|
hLayout->addWidget(m_iconButton, Qt::AlignCenter);
|
|
|
|
|
hLayout->addWidget(m_textLabel);
|
|
|
|
|
hLayout->addStretch();
|
|
|
|
|
setLayout(hLayout);
|
|
|
|
|
|
2022-02-15 16:51:03 +08:00
|
|
|
|
connect(m_iconButton, &QPushButton::clicked, this, &MyIconButton::click);
|
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;
|
2021-09-23 11:21:58 +08:00
|
|
|
|
|
2021-10-13 16:31:58 +08:00
|
|
|
|
QIcon icon = QIcon::fromTheme(themeIconName, QIcon(defaultIconName));
|
2021-09-23 11:21:58 +08:00
|
|
|
|
m_iconButton->setIcon(icon.pixmap(icon.actualSize(QSize(size, size))));
|
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
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 16:31:58 +08:00
|
|
|
|
void MyIconButton::paintEvent(QPaintEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QPushButton::paintEvent(event);
|
2022-02-23 12:10:39 +08:00
|
|
|
|
changePalette(isChecked());
|
2021-10-13 16:31:58 +08:00
|
|
|
|
m_textLabel->setFixedWidth(this->width() - 38);
|
|
|
|
|
m_textLabel->setDeplayText(m_originalText);
|
|
|
|
|
}
|
|
|
|
|
|