1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#include "aotextboxwidgets.h"
// Sane outlined QLabel solution ported from PyQt solution on StackOverflow by alec
// https://stackoverflow.com/questions/64290561/qlabel-correct-positioning-for-text-outline
AOChatboxLabel::AOChatboxLabel(QWidget *parent)
: QLabel(parent)
{
setBrush(QBrush(Qt::white));
setPen(QPen(Qt::black));
}
void AOChatboxLabel::setIsOutlined(bool outlined)
{
m_outline = outlined;
}
bool AOChatboxLabel::pointMode()
{
return m_pointmode;
}
void AOChatboxLabel::setPointMode(bool mode)
{
m_pointmode = mode;
}
double AOChatboxLabel::outlineThickness()
{
if (pointMode())
{
return m_outline_width * font().pointSize();
}
else
return m_outline_width;
}
void AOChatboxLabel::setOutlineThickness(double w)
{
m_outline_width = w;
}
void AOChatboxLabel::setBrush(QBrush brush)
{
m_brush = brush;
}
void AOChatboxLabel::setPen(QPen pen)
{
m_pen = pen;
}
QSize AOChatboxLabel::sizeHint()
{
int nrml_w = std::ceil(outlineThickness() * 2);
return QLabel::sizeHint() + QSize(nrml_w, nrml_w);
}
QSize AOChatboxLabel::minimumSizeHint()
{
int nrml_w = std::ceil(outlineThickness() * 2);
return QLabel::minimumSizeHint() + QSize(nrml_w, nrml_w);
}
void AOChatboxLabel::paintEvent(QPaintEvent *event)
{
if (m_outline)
{
double w = outlineThickness();
QRectF rect = this->rect();
QFontMetrics metrics = QFontMetrics(this->font());
QRect tr = metrics.boundingRect(text()).adjusted(0, 0, w, w);
int l_indent;
int x;
int y;
if (indent() == -1)
{
if (frameWidth())
{
l_indent = (metrics.boundingRect("x").width() + w * 2) / 2;
}
else
{
l_indent = w;
}
}
else
{
l_indent = indent();
}
if (alignment() & Qt::AlignLeft)
{
x = rect.left() + l_indent - std::min(metrics.leftBearing(text()[0]), 0);
}
else if (alignment() & Qt::AlignRight)
{
x = rect.x() + rect.width() - l_indent - tr.width();
}
else
{
x = (rect.width() - tr.width()) / 2;
}
if (alignment() & Qt::AlignTop)
{
y = rect.top() + l_indent + metrics.ascent();
}
else if (alignment() & Qt::AlignBottom)
{
y = rect.y() + rect.height() - l_indent - metrics.descent();
}
else
{
y = (rect.height() + metrics.ascent() - metrics.descent()) / 2;
}
m_pen.setWidth(w * 2);
QPainterPath path;
path.addText(x, y, font(), text());
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.strokePath(path, m_pen);
if (1 < m_brush.style() && m_brush.style() < 15)
painter.fillPath(path, palette().window());
painter.fillPath(path, m_brush);
}
else
{
// Use the default renderer
QLabel::paintEvent(event);
}
}
|