2018-07-02 16:21:43 +08:00
|
|
|
|
#include "tipswidget.h"
|
|
|
|
|
|
|
|
|
|
#include <QPainter>
|
2020-05-27 14:24:18 +08:00
|
|
|
|
#include <QAccessible>
|
2020-06-12 20:03:00 +08:00
|
|
|
|
#include <QTextDocument>
|
2021-05-12 17:32:04 +08:00
|
|
|
|
|
2020-06-29 15:35:51 +08:00
|
|
|
|
namespace Dock{
|
2020-06-13 19:19:30 +08:00
|
|
|
|
TipsWidget::TipsWidget(QWidget *parent)
|
|
|
|
|
: QFrame(parent)
|
|
|
|
|
, m_type(SingleLine)
|
2018-07-02 16:21:43 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TipsWidget::setText(const QString &text)
|
|
|
|
|
{
|
2020-05-07 16:28:16 +08:00
|
|
|
|
m_type = TipsWidget::SingleLine;
|
2020-06-12 20:03:00 +08:00
|
|
|
|
// 如果传递的是富文本,获取富文本中的纯文本内容进行显示
|
|
|
|
|
QTextDocument document;
|
|
|
|
|
document.setHtml(text);
|
|
|
|
|
// 同时去掉两边的空白信息,例如qBittorrent的提示
|
|
|
|
|
m_text = document.toPlainText().simplified();
|
2018-07-02 16:21:43 +08:00
|
|
|
|
|
2021-05-12 17:32:04 +08:00
|
|
|
|
#if 0 //测试时可以使用下面的语句
|
|
|
|
|
// FIXME:藏语字体绘制会有异常,设置高度时需要使用fontMetrics().boundingRect()去获取整体的边界矩形的高度,
|
|
|
|
|
// 使用fontMetrics().height()去获取时,针对藏语这种字体,其高度和实际显示区域并不等同
|
|
|
|
|
m_text = "བོད་སྐད་ཡིག་གཟུགས་ཚད་ལེན་ཚོད་ལྟའི་སྐོར་གྱི་རྗོད་ཚིག";
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
setFixedSize(fontMetrics().width(m_text) + 20, fontMetrics().boundingRect(m_text).height());
|
2018-07-18 11:12:46 +08:00
|
|
|
|
|
|
|
|
|
update();
|
2020-05-27 14:24:18 +08:00
|
|
|
|
|
|
|
|
|
#ifndef QT_NO_ACCESSIBILITY
|
|
|
|
|
if (accessibleName().isEmpty()) {
|
|
|
|
|
QAccessibleEvent event(this, QAccessible::NameChanged);
|
|
|
|
|
QAccessible::updateAccessibility(&event);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2018-07-02 16:21:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 18:44:11 +08:00
|
|
|
|
void TipsWidget::setTextList(const QStringList &textList)
|
2019-12-31 14:12:31 +08:00
|
|
|
|
{
|
2020-05-07 16:28:16 +08:00
|
|
|
|
m_type = TipsWidget::MultiLine;
|
2020-05-06 18:44:11 +08:00
|
|
|
|
m_textList = textList;
|
|
|
|
|
|
2021-05-12 17:32:04 +08:00
|
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
2020-05-06 18:44:11 +08:00
|
|
|
|
for (QString text : m_textList) {
|
2021-05-12 17:32:04 +08:00
|
|
|
|
width = qMax(width, fontMetrics().width(text) + 20);
|
|
|
|
|
height += fontMetrics().boundingRect(text).height();
|
2020-05-06 18:44:11 +08:00
|
|
|
|
}
|
2021-05-12 17:32:04 +08:00
|
|
|
|
|
|
|
|
|
setFixedSize(width, height);
|
2020-05-06 18:44:11 +08:00
|
|
|
|
|
2019-12-31 14:12:31 +08:00
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 17:41:06 +09:00
|
|
|
|
/**
|
|
|
|
|
* @brief TipsWidget::paintEvent 任务栏插件提示信息绘制
|
|
|
|
|
* @param event
|
|
|
|
|
*/
|
2018-07-02 16:21:43 +08:00
|
|
|
|
void TipsWidget::paintEvent(QPaintEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QFrame::paintEvent(event);
|
|
|
|
|
|
|
|
|
|
QPainter painter(this);
|
2019-09-26 21:11:39 +08:00
|
|
|
|
painter.setPen(QPen(palette().brightText(), 1));
|
2021-05-12 17:32:04 +08:00
|
|
|
|
|
2018-07-02 16:21:43 +08:00
|
|
|
|
QTextOption option;
|
|
|
|
|
option.setAlignment(Qt::AlignCenter);
|
2020-05-06 18:44:11 +08:00
|
|
|
|
|
2020-05-07 16:28:16 +08:00
|
|
|
|
switch (m_type) {
|
|
|
|
|
case SingleLine: {
|
2020-05-06 18:44:11 +08:00
|
|
|
|
painter.drawText(rect(), m_text, option);
|
|
|
|
|
}
|
2020-05-07 16:28:16 +08:00
|
|
|
|
break;
|
|
|
|
|
case MultiLine: {
|
|
|
|
|
int y = 0;
|
2020-05-06 18:44:11 +08:00
|
|
|
|
if (m_textList.size() != 1)
|
|
|
|
|
option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
|
for (QString text : m_textList) {
|
2021-05-12 17:32:04 +08:00
|
|
|
|
int lineHeight = fontMetrics().boundingRect(text).height();
|
|
|
|
|
painter.drawText(QRect(0, y, rect().width(), lineHeight), text, option);
|
|
|
|
|
y += lineHeight;
|
2020-05-06 18:44:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-07 16:28:16 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-07-02 16:21:43 +08:00
|
|
|
|
}
|
2020-06-24 15:51:53 +08:00
|
|
|
|
|
|
|
|
|
bool TipsWidget::event(QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::FontChange) {
|
2021-05-12 17:32:04 +08:00
|
|
|
|
switch (m_type) {
|
|
|
|
|
case SingleLine:
|
|
|
|
|
{
|
|
|
|
|
setText(m_text);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MultiLine:
|
|
|
|
|
{
|
|
|
|
|
setTextList(m_textList);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-06-24 15:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QFrame::event(event);
|
|
|
|
|
}
|
2020-06-29 15:35:51 +08:00
|
|
|
|
}
|