Skip to content

Commit d99ef32

Browse files
committed
qt: Add GUIUtil::ThemedLabel class
The ThemedLabel class correctly handles appearance changes on macOS.
1 parent c054720 commit d99ef32

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/qt/guiutil.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <qt/bitcoinaddressvalidator.h>
88
#include <qt/bitcoinunits.h>
9+
#include <qt/platformstyle.h>
910
#include <qt/qvalidatedlineedit.h>
1011
#include <qt/sendcoinsrecipient.h>
1112

@@ -61,6 +62,7 @@
6162
#include <QUrlQuery>
6263
#include <QtGlobal>
6364

65+
#include <cassert>
6466
#include <chrono>
6567

6668
#if defined(Q_OS_MAC)
@@ -786,6 +788,35 @@ qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal m
786788
return font_size;
787789
}
788790

791+
ThemedLabel::ThemedLabel(const PlatformStyle* platform_style, QWidget* parent)
792+
: QLabel{parent}, m_platform_style{platform_style}
793+
{
794+
assert(m_platform_style);
795+
}
796+
797+
void ThemedLabel::setThemedPixmap(const QString& image_filename, int width, int height)
798+
{
799+
m_image_filename = image_filename;
800+
m_pixmap_width = width;
801+
m_pixmap_height = height;
802+
updateThemedPixmap();
803+
}
804+
805+
void ThemedLabel::changeEvent(QEvent* e)
806+
{
807+
#ifdef Q_OS_MACOS
808+
if (e->type() == QEvent::PaletteChange) {
809+
updateThemedPixmap();
810+
}
811+
#endif
812+
QLabel::changeEvent(e);
813+
}
814+
815+
void ThemedLabel::updateThemedPixmap()
816+
{
817+
setPixmap(m_platform_style->SingleColorIcon(m_image_filename).pixmap(m_pixmap_width, m_pixmap_height));
818+
}
819+
789820
void ClickableLabel::mouseReleaseEvent(QMouseEvent *event)
790821
{
791822
Q_EMIT clicked(event->pos());

src/qt/guiutil.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <chrono>
2828
#include <utility>
2929

30+
class PlatformStyle;
3031
class QValidatedLineEdit;
3132
class SendCoinsRecipient;
3233

@@ -221,6 +222,25 @@ namespace GUIUtil
221222

222223
qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14);
223224

225+
class ThemedLabel : public QLabel
226+
{
227+
Q_OBJECT
228+
229+
public:
230+
explicit ThemedLabel(const PlatformStyle* platform_style, QWidget* parent = nullptr);
231+
void setThemedPixmap(const QString& image_filename, int width, int height);
232+
233+
protected:
234+
void changeEvent(QEvent* e) override;
235+
236+
private:
237+
const PlatformStyle* m_platform_style;
238+
QString m_image_filename;
239+
int m_pixmap_width;
240+
int m_pixmap_height;
241+
void updateThemedPixmap();
242+
};
243+
224244
class ClickableLabel : public QLabel
225245
{
226246
Q_OBJECT

0 commit comments

Comments
 (0)