From b76c398abe532abebea4e3086446956948f55a1c Mon Sep 17 00:00:00 2001 From: Satori Date: Mon, 2 Aug 2021 14:59:10 +0100 Subject: [PATCH] [Qt] Support setting button icon from font glyph --- src/xenia/ui/qt/widgets/push_button.cc | 25 +++++++++++++++++++++++++ src/xenia/ui/qt/widgets/push_button.h | 2 ++ 2 files changed, 27 insertions(+) diff --git a/src/xenia/ui/qt/widgets/push_button.cc b/src/xenia/ui/qt/widgets/push_button.cc index 3261ce642..9630635f8 100644 --- a/src/xenia/ui/qt/widgets/push_button.cc +++ b/src/xenia/ui/qt/widgets/push_button.cc @@ -20,6 +20,31 @@ XPushButton::XPushButton(const QIcon& icon, const QString& text, Build(); } +void XPushButton::SetIconFromGlyph(QChar glyph, QColor color, int size) { + auto glyph_font = QFont("Segoe MDL2 Assets", size); + // Measure the Glyph + QFontMetrics measure(glyph_font); + QRect icon_rect = measure.boundingRect(glyph); + double max = qMax(icon_rect.width(), icon_rect.height()); + + // Create the Pixmap + // boundingRect can be inaccurate so add a 4px padding to be safe + QPixmap pixmap(max + 4, max + 4); + pixmap.fill(Qt::transparent); + + // Paint the Glyph + QPainter painter(&pixmap); + painter.setFont(glyph_font); + painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); + painter.setPen(QPen(color)); + + painter.drawText(pixmap.rect(), Qt::AlignVCenter, glyph); + + painter.end(); + + this->setIcon(pixmap); +} + void XPushButton::Build() { setFlat(true); setCursor(Qt::PointingHandCursor); diff --git a/src/xenia/ui/qt/widgets/push_button.h b/src/xenia/ui/qt/widgets/push_button.h index 02bad5db7..991ef4baf 100644 --- a/src/xenia/ui/qt/widgets/push_button.h +++ b/src/xenia/ui/qt/widgets/push_button.h @@ -16,6 +16,8 @@ class XPushButton : public Themeable { XPushButton(const QIcon& icon, const QString& text, QWidget* parent = nullptr); + void SetIconFromGlyph(QChar glyph, QColor color = Qt::white, int size = 64); + private: void Build(); };