mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-09 01:23:57 -07:00
fix: render text in home widget correctly
This commit is contained in:
parent
f7ffcc58fe
commit
f0ecd1b524
1 changed files with 13 additions and 8 deletions
|
|
@ -76,7 +76,7 @@ QString HomeStyledButton::generateButtonStylesheet(const QPair<QColor, QColor> &
|
||||||
|
|
||||||
void HomeStyledButton::paintEvent(QPaintEvent *event)
|
void HomeStyledButton::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event); // Event is just used for update clipping, we redraw the whole widget.
|
Q_UNUSED(event);
|
||||||
QStyleOptionButton opt;
|
QStyleOptionButton opt;
|
||||||
initStyleOption(&opt);
|
initStyleOption(&opt);
|
||||||
opt.text.clear(); // prevent style from drawing text
|
opt.text.clear(); // prevent style from drawing text
|
||||||
|
|
@ -84,7 +84,6 @@ void HomeStyledButton::paintEvent(QPaintEvent *event)
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
|
style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
|
||||||
|
|
||||||
// Draw white text with a black outline
|
|
||||||
painter.setRenderHint(QPainter::Antialiasing);
|
painter.setRenderHint(QPainter::Antialiasing);
|
||||||
painter.setRenderHint(QPainter::TextAntialiasing);
|
painter.setRenderHint(QPainter::TextAntialiasing);
|
||||||
|
|
||||||
|
|
@ -92,14 +91,20 @@ void HomeStyledButton::paintEvent(QPaintEvent *event)
|
||||||
font.setBold(true);
|
font.setBold(true);
|
||||||
painter.setFont(font);
|
painter.setFont(font);
|
||||||
|
|
||||||
QFontMetrics fm(font);
|
QFontMetricsF fm(font);
|
||||||
QSize textSize = fm.size(Qt::TextSingleLine, this->text());
|
QPainterPath textPath;
|
||||||
QPointF center((width() - textSize.width()) / 2.0, (height() + textSize.height() / 2.0) / 2.0);
|
textPath.setFillRule(Qt::WindingFill);
|
||||||
|
textPath.addText(0, fm.ascent(), font, this->text());
|
||||||
|
|
||||||
QPainterPath path;
|
// Calculate translation to center the text path
|
||||||
path.addText(center, font, this->text());
|
QRectF pathBounds = textPath.boundingRect();
|
||||||
|
qreal tx = (width() - pathBounds.width()) / 2.0 - pathBounds.left();
|
||||||
|
qreal ty = (height() - pathBounds.height()) / 2.0 - pathBounds.top();
|
||||||
|
|
||||||
|
textPath.translate(tx, ty);
|
||||||
|
|
||||||
|
// Draw outline then fill
|
||||||
painter.setPen(QPen(Qt::black, 2.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
painter.setPen(QPen(Qt::black, 2.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
||||||
painter.setBrush(Qt::white);
|
painter.setBrush(Qt::white);
|
||||||
painter.drawPath(path);
|
painter.drawPath(textPath);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue