首次提交
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
#include "Text_Shower.h"
|
||||
#include <QPainter>
|
||||
#include <QFontMetrics>
|
||||
#include <algorithm>
|
||||
|
||||
namespace Psc {
|
||||
|
||||
Text_Shower::Text_Shower(QWidget* parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
setAttribute(Qt::WA_TranslucentBackground);
|
||||
|
||||
// 宽度交给布局,高度最小
|
||||
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
|
||||
}
|
||||
|
||||
void Text_Shower::set_text(const QString& text)
|
||||
{
|
||||
m_text = text;
|
||||
updateGeometry(); // 通知布局重新计算
|
||||
update();
|
||||
}
|
||||
|
||||
void Text_Shower::set_title(const QString& title)
|
||||
{
|
||||
m_title = title;
|
||||
updateGeometry();
|
||||
update();
|
||||
}
|
||||
|
||||
void Text_Shower::set_max_width(int w)
|
||||
{
|
||||
m_max_width = w;
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
bool Text_Shower::hasHeightForWidth() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int Text_Shower::heightForWidth(int width) const
|
||||
{
|
||||
width = std::min(width, m_max_width);
|
||||
return calculate_height(width);
|
||||
}
|
||||
|
||||
QSize Text_Shower::sizeHint() const
|
||||
{
|
||||
int w = std::min(400, m_max_width);
|
||||
return QSize(w, calculate_height(w));
|
||||
}
|
||||
|
||||
QSize Text_Shower::minimumSizeHint() const
|
||||
{
|
||||
return QSize(120, 40);
|
||||
}
|
||||
|
||||
int Text_Shower::calculate_height(int width) const
|
||||
{
|
||||
QFontMetrics fm(font());
|
||||
|
||||
int content_width = width - m_padding * 2;
|
||||
if (content_width <= 0)
|
||||
return 40;
|
||||
|
||||
QRect textRect = fm.boundingRect(
|
||||
QRect(0, 0, content_width, 10000),
|
||||
Qt::TextWordWrap,
|
||||
m_text
|
||||
);
|
||||
|
||||
int height = textRect.height();
|
||||
|
||||
if (!m_title.isEmpty())
|
||||
{
|
||||
QFont titleFont = font();
|
||||
titleFont.setBold(true);
|
||||
QFontMetrics titleFm(titleFont);
|
||||
|
||||
height += titleFm.height() + 6;
|
||||
}
|
||||
|
||||
height += m_padding * 2;
|
||||
|
||||
return height;
|
||||
}
|
||||
|
||||
void Text_Shower::paintEvent(QPaintEvent*)
|
||||
{
|
||||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QRect r = rect();
|
||||
|
||||
painter.setBrush(QColor(255,255,255));
|
||||
painter.setPen(QPen(QColor(0,0,0), 1));
|
||||
painter.drawRoundedRect(r.adjusted(0,0,-1,-1), m_radius, m_radius);
|
||||
|
||||
int y = m_padding;
|
||||
|
||||
if (!m_title.isEmpty())
|
||||
{
|
||||
QFont titleFont = font();
|
||||
titleFont.setBold(true);
|
||||
painter.setFont(titleFont);
|
||||
|
||||
QFontMetrics titleFm(titleFont);
|
||||
painter.drawText(m_padding,
|
||||
y + titleFm.ascent(),
|
||||
m_title);
|
||||
|
||||
y += titleFm.height() + 6;
|
||||
painter.setFont(font());
|
||||
}
|
||||
|
||||
QRect textRect(m_padding, y,
|
||||
width() - m_padding * 2,
|
||||
height() - y - m_padding);
|
||||
|
||||
painter.drawText(textRect,
|
||||
Qt::AlignLeft | Qt::TextWordWrap,
|
||||
m_text);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user