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>
|
2018-07-02 16:21:43 +08:00
|
|
|
|
|
2020-06-13 19:19:30 +08:00
|
|
|
|
TipsWidget::TipsWidget(QWidget *parent)
|
|
|
|
|
: QFrame(parent)
|
|
|
|
|
, m_width(0)
|
|
|
|
|
, 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
|
|
|
|
|
2020-06-12 20:03:00 +08:00
|
|
|
|
setFixedSize(fontMetrics().width(m_text) + 6, fontMetrics().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;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2019-12-31 14:12:31 +08:00
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
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));
|
2018-07-02 16:21:43 +08:00
|
|
|
|
QTextOption option;
|
2020-05-06 18:44:11 +08:00
|
|
|
|
int fontHeight = fontMetrics().height();
|
2018-07-02 16:21:43 +08:00
|
|
|
|
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) {
|
|
|
|
|
painter.drawText(QRect(0, y, m_width, fontHeight), text, option);
|
|
|
|
|
y += fontHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
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) {
|
2020-06-27 14:57:24 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
2020-06-24 15:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QFrame::event(event);
|
|
|
|
|
}
|