Skip to content

Commit 2aadffd

Browse files
committed
frontend: Add icon recoloring to state style event filter
1 parent 9309182 commit 2aadffd

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

shared/qt/idian/include/Idian/StateEventFilter.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <Idian/Utils.hpp>
33

44
#include <QAbstractButton>
5-
#include <QLabel>
5+
#include <QStyleOptionButton>
66

77
namespace idian {
88
StateEventFilter::StateEventFilter(idian::Utils *utils, QWidget *target) : QObject(target), target(target), utils(utils)
@@ -22,7 +22,16 @@ bool StateEventFilter::eventFilter(QObject *obj, QEvent *event)
2222
QWidget *widget = qobject_cast<QWidget *>(obj);
2323
QFocusEvent *focusEvent = nullptr;
2424

25+
bool updateIconColors = true;
26+
2527
switch (event->type()) {
28+
case QEvent::StyleChange:
29+
case QEvent::ThemeChange:
30+
utils->repolish(widget);
31+
32+
utils->polishChildren(widget);
33+
34+
break;
2635
case QEvent::FocusIn:
2736
utils->toggleClass(widget, "focus", true);
2837

@@ -34,6 +43,7 @@ bool StateEventFilter::eventFilter(QObject *obj, QEvent *event)
3443
}
3544

3645
utils->polishChildren(widget);
46+
3747
break;
3848
case QEvent::FocusOut:
3949
utils->toggleClass(widget, "focus", false);
@@ -44,27 +54,37 @@ bool StateEventFilter::eventFilter(QObject *obj, QEvent *event)
4454
utils->polishChildren(widget);
4555
}
4656

47-
break;
48-
case QEvent::MouseButtonPress:
57+
utils->polishChildren(widget);
58+
4959
break;
5060
case QEvent::HoverEnter:
5161
if (widget->isEnabled()) {
5262
utils->toggleClass(widget, "hover", true);
5363
}
5464

5565
utils->polishChildren(widget);
66+
5667
break;
5768
case QEvent::HoverLeave:
5869
utils->toggleClass(widget, "hover", false);
5970

6071
utils->polishChildren(widget);
72+
6173
break;
6274
case QEvent::EnabledChange:
63-
bool widgetEnabled = widget->isEnabled();
64-
utils->toggleClass(widget, "disabled", !widgetEnabled);
75+
utils->toggleClass(widget, "disabled", !widget->isEnabled());
6576

6677
utils->polishChildren(widget);
78+
6779
break;
80+
default:
81+
updateIconColors = false;
82+
break;
83+
}
84+
85+
if (updateIconColors) {
86+
// Delay icon update
87+
QTimer::singleShot(0, this, [this, widget]() { utils->applyColorToIcon(widget); });
6888
}
6989

7090
return QObject::eventFilter(obj, event);

shared/qt/idian/include/Idian/StateEventFilter.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <Idian/Utils.hpp>
44

55
#include <QWidget>
6-
#include <QAbstractbutton>
7-
#include <QStyleOptionButton>
86

97
namespace idian {
108
class StateEventFilter : public QObject {
@@ -21,5 +19,7 @@ public slots:
2119
private:
2220
Utils *utils;
2321
QWidget *target;
22+
23+
void applyColorToIcon();
2424
};
2525
} // namespace idian

shared/qt/idian/include/Idian/Utils.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
#pragma once
1919

20+
#include <QAbstractButton>
2021
#include <QFocusEvent>
2122
#include <QPainter>
2223
#include <QRegularExpression>
2324
#include <QStyle>
25+
#include <QStyleOptionButton>
26+
#include <QTimer>
2427
#include <QWidget>
2528

2629
namespace idian {
@@ -125,6 +128,34 @@ class Utils {
125128
}
126129
}
127130

131+
static void applyColorToIcon(QWidget *widget)
132+
{
133+
QAbstractButton *button = qobject_cast<QAbstractButton *>(widget);
134+
if (button && !button->icon().isNull()) {
135+
// Filter is on a widget with an icon set, update it's colors
136+
QStyleOptionButton opt;
137+
opt.initFrom(button);
138+
139+
QColor color = opt.palette.color(QPalette::ButtonText);
140+
QPixmap tinted = recolorPixmap(button->icon().pixmap(button->iconSize(), QIcon::Normal), color);
141+
QIcon tintedIcon;
142+
tintedIcon.addPixmap(tinted, QIcon::Normal);
143+
tintedIcon.addPixmap(tinted, QIcon::Disabled);
144+
145+
button->setIcon(tintedIcon);
146+
}
147+
}
148+
149+
static QPixmap recolorPixmap(const QPixmap &src, const QColor &color)
150+
{
151+
QImage img = src.toImage();
152+
QPainter p(&img);
153+
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
154+
p.fillRect(img.rect(), color);
155+
p.end();
156+
return QPixmap::fromImage(img);
157+
}
158+
128159
void applyStateStylingEventFilter(QWidget *widget);
129160
};
130161

0 commit comments

Comments
 (0)