86 lines
2.7 KiB
C++
Executable File
86 lines
2.7 KiB
C++
Executable File
#include "myiconbutton.h"
|
|
#include <QFontMetrics>
|
|
#include <QHBoxLayout>
|
|
#include <QStyleOptionButton>
|
|
#include <QPainter>
|
|
#include <QApplication>
|
|
#include "imageutil.h"
|
|
|
|
MyIconButton::MyIconButton(QWidget *parent) :
|
|
QPushButton(parent)
|
|
{
|
|
this->setCheckable(false);
|
|
|
|
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_textLabel = new MyLabel(this);
|
|
QSizePolicy textLabelPolicy = m_textLabel->sizePolicy();
|
|
textLabelPolicy.setHorizontalPolicy(QSizePolicy::Fixed);
|
|
textLabelPolicy.setVerticalPolicy(QSizePolicy::Fixed);
|
|
m_textLabel->setSizePolicy(textLabelPolicy);
|
|
m_textLabel->setScaledContents(true);
|
|
|
|
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, &MyIconButton::click);
|
|
// 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);
|
|
}
|
|
|
|
MyIconButton::~MyIconButton()
|
|
{}
|
|
|
|
void MyIconButton::setThemeIcon(const QString &themeIconName, const QString &defaultIconName, int size)
|
|
{
|
|
m_themeIconName = themeIconName;
|
|
m_defaultIconName = defaultIconName;
|
|
|
|
QIcon icon = QIcon::fromTheme(themeIconName, QIcon(defaultIconName));
|
|
m_iconButton->setIcon(icon.pixmap(icon.actualSize(QSize(size, size))));
|
|
}
|
|
|
|
void MyIconButton::setDesplayText(const QString &text)
|
|
{
|
|
m_originalText = text;
|
|
m_textLabel->setDeplayText(text);
|
|
}
|
|
|
|
void MyIconButton::changePalette(bool checked)
|
|
{
|
|
// m_iconButton->setChecked(checked);
|
|
QPalette pal(m_textLabel->palette());
|
|
QIcon icon = QIcon::fromTheme(m_themeIconName, QIcon(m_defaultIconName));
|
|
QPixmap pix;
|
|
if (checked) {
|
|
pal.setColor(QPalette::ButtonText, pal.color(QPalette::HighlightedText));
|
|
pix = ImageUtil::loadPixmap(icon, QString("white"));
|
|
} else {
|
|
pal.setColor(QPalette::ButtonText, pal.color(QPalette::WindowText));
|
|
pix = ImageUtil::loadPixmap(icon, QString("default"));
|
|
}
|
|
m_textLabel->setPalette(pal);
|
|
m_iconButton->setIcon(pix);
|
|
}
|
|
|
|
void MyIconButton::paintEvent(QPaintEvent *event)
|
|
{
|
|
QPushButton::paintEvent(event);
|
|
m_textLabel->setFixedWidth(this->width() - 38);
|
|
m_textLabel->setDeplayText(m_originalText);
|
|
}
|
|
|