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

149 lines
4.0 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "mylabel.h"
#include <QPalette>
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
/**
* @brief 通用构造Label方法
* @param parent
*/
MyLabel::MyLabel(QWidget* parent) :
QLabel(parent),
m_bAutoTheme(true)
{
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
// 只有黑白两色手动跟随主题,其它颜色不需要程序员手动设置(自动即可)
if (!m_bAutoTheme) {
if (isDark) {
this->setFontColor(Qt::white);
} else {
this->setFontColor(Qt::black);
}
}
});
}
/**
* @brief 构造固定大小、颜色、内容居中等的label一般用于label控件不会变化重绘的场景
* @param text
* @param parent
* @param color
*/
MyLabel::MyLabel(const QString& text, QWidget* parent /*= nullptr*/, Qt::Alignment align /*= Qt::AlignCenter*/) :
QLabel(parent),
m_text(text),
m_bAutoTheme(true)
{
this->setAlignment(align);
this->setScaledContents(true);
this->setText(text);
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
// 只有黑白两色手动跟随主题,其它颜色不需要程序员手动设置(自动即可)
if (!m_bAutoTheme) {
if (isDark) {
this->setFontColor(Qt::white);
} else {
this->setFontColor(Qt::black);
}
}
});
}
MyLabel::~MyLabel()
{}
/**
* @brief 设置字体颜色建议颜色使用Qt::black这种模式的
* @param color
*/
void MyLabel::setFontColor(QColor color)
{
// 只有黑白两色手动跟随主题,其它颜色不需要程序员手动设置(自动即可)
if (color == QColor(Qt::black)) {
m_bAutoTheme = false;
if (g_GSettingWrapper.isDarkTheme()) {
color = QColor(Qt::white);
}
} else if (color == QColor(Qt::white)) {
m_bAutoTheme = false;
if (!g_GSettingWrapper.isDarkTheme()) {
color = QColor(Qt::black);
}
} else {
m_bAutoTheme = true;
}
QPalette palette = this->palette();
palette.setColor(QPalette::WindowText, color);
this->setPalette(palette);
}
void MyLabel::setFontSize(int size)
{
QFont font = this->font();
font.setPixelSize(size);
// 默认为大字体粗体显示
if (size > 20)
font.setBold(true);
this->setFont(font);
}
/**
* @brief 设置字体是否自动换行展示
* @param on
* @note 一般用于setGeometry固定label位置和大小的场景
*/
void MyLabel::setFontWordWrap(bool on)
{
m_bWordWrap = on;
setWordWrap(on);
m_width = this->width();
m_height = this->height();
m_rect = geometry();
}
void MyLabel::paintEvent(QPaintEvent *event)
{
// 1、场景一布局动态变化场景使用原始的QLabel绘制
if (m_isOriginal) {
this->setText(m_text);
QLabel::paintEvent(event);
return ;
}
// 2、场景二setGeometry固定label位置和大小的场景
QFontMetrics fontMetrics(this->font());
int fontSize = fontMetrics.width(m_text);
if (m_bWordWrap && m_width > 0) {
// resize(m_width, m_height);
// setGeometry(m_rect);
// 恢复控件宽度,如果不固定宽度则换行位置不好控制
this->setFixedWidth(m_width);
}
if (fontSize > this->width()) {
if (m_bWordWrap) {
// 调整控件大小
adjustSize();
setAlignment(Qt::AlignTop | Qt::AlignLeft);
this->setText(m_text);
} else {
this->setText(fontMetrics.elidedText(m_text, Qt::ElideRight, this->width()));
}
} else {
if (m_bWordWrap) {
// 恢复控件大小及位置
setGeometry(m_rect);
setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
}
this->setText(m_text);
}
QLabel::paintEvent(event);
}