2018-07-02 16:21:43 +08:00
|
|
|
#include "tipswidget.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
2020-05-27 14:24:18 +08:00
|
|
|
#include <QAccessible>
|
2018-07-02 16:21:43 +08:00
|
|
|
|
|
|
|
TipsWidget::TipsWidget(QWidget *parent) : QFrame(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TipsWidget::setText(const QString &text)
|
|
|
|
{
|
2020-05-07 16:28:16 +08:00
|
|
|
m_type = TipsWidget::SingleLine;
|
2018-07-02 16:21:43 +08:00
|
|
|
m_text = text;
|
|
|
|
|
|
|
|
setFixedSize(fontMetrics().width(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
|
|
|
}
|