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>
|
|
|
|
#include "imageutil.h"
|
2021-09-16 16:05:46 +08:00
|
|
|
|
|
|
|
MyIconButton::MyIconButton(QWidget *parent) :
|
|
|
|
QPushButton(parent)
|
|
|
|
{
|
|
|
|
m_iconButton = new QPushButton(this);
|
|
|
|
m_iconButton->setCheckable(false);
|
|
|
|
m_iconButton->setFixedSize(QSize(24, 24));
|
|
|
|
m_iconButton->setFocusPolicy(Qt::NoFocus);
|
|
|
|
m_iconButton->setFlat(true);
|
|
|
|
// m_iconButton->setStyleSheet("QPushButton:checked{border: none;}"
|
|
|
|
// "QPushButton:!checked{border: none;}");
|
|
|
|
|
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);
|
2021-09-23 11:21:58 +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);
|
|
|
|
|
2021-09-23 11:21:58 +08:00
|
|
|
connect(m_iconButton, &QPushButton::clicked, this, &MyIconButton::clicked);
|
|
|
|
// connect(this, &QPushButton::toggled, this, [=] (bool checked) {
|
|
|
|
// if (checked)
|
|
|
|
// m_textLabel->setStyleSheet("color:white");
|
|
|
|
// else
|
|
|
|
// m_textLabel->setStyleSheet("color:palette(windowText)");
|
|
|
|
// });
|
|
|
|
connect(this, &QPushButton::toggled, this, &MyIconButton::changePalette);
|
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)
|
|
|
|
{
|
|
|
|
QPalette pal(m_textLabel->palette());
|
2021-10-13 16:31:58 +08:00
|
|
|
QIcon icon = QIcon::fromTheme(m_themeIconName, QIcon(m_defaultIconName));
|
2021-09-23 11:21:58 +08:00
|
|
|
QPixmap pix;
|
|
|
|
if (checked) {
|
|
|
|
pal.setColor(QPalette::ButtonText, pal.color(QPalette::HighlightedText));
|
2021-10-13 16:31:58 +08:00
|
|
|
pix = ImageUtil::loadPixmap(icon, QString("white"));
|
2021-09-23 11:21:58 +08:00
|
|
|
} else {
|
|
|
|
pal.setColor(QPalette::ButtonText, pal.color(QPalette::WindowText));
|
2021-10-13 16:31:58 +08:00
|
|
|
pix = ImageUtil::loadPixmap(icon, QString("default"));
|
2021-09-23 11:21:58 +08:00
|
|
|
}
|
|
|
|
m_textLabel->setPalette(pal);
|
|
|
|
m_iconButton->setIcon(pix);
|
|
|
|
}
|
|
|
|
|
2021-10-13 16:31:58 +08:00
|
|
|
void MyIconButton::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
QPushButton::paintEvent(event);
|
|
|
|
m_textLabel->setFixedWidth(this->width() - 38);
|
|
|
|
m_textLabel->setDeplayText(m_originalText);
|
|
|
|
}
|
|
|
|
|