dde-dock/widgets/tipswidget.cpp
qiuchangxing deeb523fea fix: 修复切换藏语,任务栏中tooltip的应用名称显示异常,藏语头部被截断
tooltip的应用名称显示绘制时,字体所占像素调整为字体实际使用rect高度的一半减一最为合适,否则碰到藏文这种特殊字体会导致显示不全

Log: 修复切换藏语,任务栏中tooltip的应用名称显示异常,藏语头部被截断
Bug: https://pms.uniontech.com/zentao/bug-view-76847.html
Change-Id: Ia999ef70acb93bdf9b6d7039c9facac5b21e9250
2021-04-29 14:48:16 +09:00

111 lines
2.9 KiB
C++
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 "tipswidget.h"
#include <QPainter>
#include <QAccessible>
#include <QTextDocument>
namespace Dock{
TipsWidget::TipsWidget(QWidget *parent)
: QFrame(parent)
, m_width(0)
, m_type(SingleLine)
{
}
void TipsWidget::setText(const QString &text)
{
m_type = TipsWidget::SingleLine;
// 如果传递的是富文本,获取富文本中的纯文本内容进行显示
QTextDocument document;
document.setHtml(text);
// 同时去掉两边的空白信息例如qBittorrent的提示
m_text = document.toPlainText().simplified();
setFixedSize(fontMetrics().width(m_text) + 20, fontMetrics().height());
update();
#ifndef QT_NO_ACCESSIBILITY
if (accessibleName().isEmpty()) {
QAccessibleEvent event(this, QAccessible::NameChanged);
QAccessible::updateAccessibility(&event);
}
#endif
}
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) + 20;
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);
QFont textFont;
textFont.setPixelSize(rect().height() / 2 - 1);
painter.setFont(textFont);
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;
}
}
bool TipsWidget::event(QEvent *event)
{
if (event->type() == QEvent::FontChange) {
if (m_type == SingleLine) {
if (!m_text.trimmed().isEmpty()) {
setFixedSize(fontMetrics().width(m_text) + 6, fontMetrics().height());
update();
}
} else {
if (m_textList.size() > 0) {
int maxLength = 0;
setFixedHeight(fontMetrics().height() * m_textList.size());
for (QString text : m_textList) {
int fontLength = fontMetrics().width(text) + 6;
maxLength = qMax(maxLength,fontLength);
}
m_width = maxLength;
setFixedWidth(maxLength);
update();
}
}
}
return QFrame::event(event);
}
}