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

73 lines
2.6 KiB
C++

#include "myiconlabel.h"
#include <QHBoxLayout>
#include <QIcon>
#include <QApplication>
#include "imageutil.h"
MyIconLabel::MyIconLabel(QWidget *parent /*= nullptr*/) :
QLabel(parent)
{
m_iconLabel = new QLabel(this);
// border:1px solid black;
// const QString greySheetStyle = "min-width: 36px; min-height: 36px;max-width:36px; max-height: 36px;border-radius: 18px; background:grey";
const QString greySheetStyle = "min-width: 36px; min-height: 36px;max-width:36px; max-height: 36px;border-radius: 18px; background:#F4F4F4";
m_iconLabel->setStyleSheet(greySheetStyle);
m_iconLabel->setAlignment(Qt::AlignCenter);
m_textLabel = new QLabel(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_iconLabel, Qt::AlignCenter);
hLayout->addWidget(m_textLabel);
hLayout->addStretch();
setLayout(hLayout);
}
MyIconLabel::~MyIconLabel()
{}
void MyIconLabel::setThemeIcon(const QString &themeIconName, const QString &defaultIconName)
{
m_themeIconName = themeIconName;
m_defaultIconName = defaultIconName;
QIcon icon = QIcon::fromTheme(themeIconName, QIcon(defaultIconName));
// m_iconLabel->setPixmap(icon.pixmap(QSize(24,24)));
m_iconLabel->setPixmap(ImageUtil::loadPixmap(icon, QString("default")));
}
void MyIconLabel::setDesplayText(const QString &text)
{
m_originalText = text;
m_textLabel->setFixedWidth(this->width() - 40);
QFontMetrics fontMetrics(m_textLabel->font());
int fontSize = fontMetrics.width(text);
if (fontSize > m_textLabel->width()) {
m_textLabel->setText(fontMetrics.elidedText(text, Qt::ElideRight, m_textLabel->width()));
} else {
m_textLabel->setText(text);
}
}
void MyIconLabel::changePalette(bool dark)
{
QPalette pal(m_textLabel->palette());
QIcon icon = QIcon::fromTheme(m_themeIconName, QIcon(m_defaultIconName));
QPixmap pix;
if (dark) {
pal.setColor(QPalette::WindowText, pal.color(QPalette::HighlightedText));
pix = ImageUtil::loadPixmap(icon, QString("white"));
} else {
pal.setColor(QPalette::WindowText, this->palette().color(QPalette::WindowText));
pix = ImageUtil::loadPixmap(icon, QString("default"));
}
m_textLabel->setPalette(pal);
m_iconLabel->setPixmap(pix);
}