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

78 lines
2.5 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 "circlelabel.h"
#include <QPainter>
#include <QBrush>
#include <QPalette>
#include "../globalbackupinfo.h"
#include "../gsettingswrapper.h"
CircleLable::CircleLable(const QString& text, QWidget* parent /*= nullptr*/, int size /*= 24*/, QColor backgroundColor /*= QColor(0xCC, 0xCC, 0xCC)*/) :
QLabel(parent),
m_text(text),
m_textColor(Qt::white)
{
this->setFixedSize(QSize(size, size));
setBackgroundColor(backgroundColor);
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) {
if (!m_bAutoTheme) {
if (isDark) {
this->setBackgroundColor(Qt::white);
} else {
this->setBackgroundColor(Qt::black);
}
}
});
}
CircleLable::~CircleLable()
{}
/**
* @brief 设置圆形label的背景色
* @param backgroundColor 背景色建议使用Qt::black、Qt::white等这种颜色表示法
*/
void CircleLable::setBackgroundColor(QColor backgroundColor)
{
// 只有黑白两色不自动跟随主题,需手动设置背景色
if (backgroundColor == QColor(Qt::black)) {
m_bAutoTheme = false;
if (g_GSettingWrapper.isDarkTheme()) {
backgroundColor = QColor(Qt::white);
}
} else if (backgroundColor == QColor(Qt::white)) {
m_bAutoTheme = false;
if (!g_GSettingWrapper.isDarkTheme()) {
backgroundColor = QColor(Qt::black);
}
} else {
m_bAutoTheme = true;
}
m_backgroundColor = backgroundColor;
repaint();
}
void CircleLable::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
// 如果使用的是默认的灰色,则转换为调色板中的按钮背景色
if (m_backgroundColor == QColor(0xCC, 0xCC, 0xCC)) {
QColor background = this->palette().color(QPalette::Button);
painter.setBrush(QBrush(background));
} else {
painter.setBrush(QBrush(m_backgroundColor));
}
painter.setPen(Qt::NoPen);
QRect rect = this->rect();
// 也可用QPainterPath 绘制代替 painter.drawRoundedRect(rect, 15, 15); { QPainterPath painterPath; painterPath.addRoundedRect(rect, 15, 15); p.drawPath(painterPath); }
painter.drawRoundedRect(rect, rect.width()/2, rect.width()/2);
// drawText
rect.setHeight(rect.height() - 2);
painter.setPen(m_textColor);
painter.drawText(rect, Qt::AlignCenter, m_text);
QLabel::paintEvent(event);
}