dde-dock/widgets/tipswidget.cpp
zhaolong 4303f90d75 fix(bluetooth):popups display truncated
task20793 【TR4】【桌面专业版】【SP1】【华为】【Kunpeng920】【uos-20-pangu-daliy-20200424-build46】【任务栏】任务栏蓝牙显示截断

(cherry picked from commit 619ba29a258a215868a05e83f543f8a030f08d1d)
2020-05-14 13:14:57 +08:00

65 lines
1.5 KiB
C++

#include "tipswidget.h"
#include <QPainter>
TipsWidget::TipsWidget(QWidget *parent) : QFrame(parent)
{
}
void TipsWidget::setText(const QString &text)
{
m_type = TipsWidget::SingleLine;
m_text = text;
setFixedSize(fontMetrics().width(text) + 6, fontMetrics().height());
update();
}
void TipsWidget::setTextList(const QStringList &textList)
{
m_type = TipsWidget::MultiLine;
m_textList = textList;
int maxLength = 0;
int k = fontMetrics().height() * m_textList.size();
setFixedHeight(k);
for (QString text : m_textList) {
int fontLength = fontMetrics().width(text) + 6;
maxLength = maxLength > fontLength ? maxLength : fontLength;
}
m_width = maxLength;
setFixedWidth(maxLength);
update();
}
void TipsWidget::paintEvent(QPaintEvent *event)
{
QFrame::paintEvent(event);
QPainter painter(this);
painter.setPen(QPen(palette().brightText(), 1));
QTextOption option;
int fontHeight = fontMetrics().height();
option.setAlignment(Qt::AlignCenter);
switch (m_type) {
case SingleLine: {
painter.drawText(rect(), m_text, option);
}
break;
case MultiLine: {
int y = 0;
if (m_textList.size() != 1)
option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
for (QString text : m_textList) {
painter.drawText(QRect(0, y, m_width, fontHeight), text, option);
y += fontHeight;
}
}
break;
}
}