mirror of
https://github.com/obsproject/obs-studio.git
synced 2026-03-26 18:32:07 -04:00
frontend: Add icon recoloring to state style event filter
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
#include <Idian/Utils.hpp>
|
||||
|
||||
#include <QAbstractButton>
|
||||
#include <QLabel>
|
||||
#include <QStyleOptionButton>
|
||||
|
||||
namespace idian {
|
||||
StateEventFilter::StateEventFilter(idian::Utils *utils, QWidget *target) : QObject(target), target(target), utils(utils)
|
||||
@@ -22,7 +22,16 @@ bool StateEventFilter::eventFilter(QObject *obj, QEvent *event)
|
||||
QWidget *widget = qobject_cast<QWidget *>(obj);
|
||||
QFocusEvent *focusEvent = nullptr;
|
||||
|
||||
bool updateIconColors = true;
|
||||
|
||||
switch (event->type()) {
|
||||
case QEvent::StyleChange:
|
||||
case QEvent::ThemeChange:
|
||||
utils->repolish(widget);
|
||||
|
||||
utils->polishChildren(widget);
|
||||
|
||||
break;
|
||||
case QEvent::FocusIn:
|
||||
utils->toggleClass(widget, "focus", true);
|
||||
|
||||
@@ -34,6 +43,7 @@ bool StateEventFilter::eventFilter(QObject *obj, QEvent *event)
|
||||
}
|
||||
|
||||
utils->polishChildren(widget);
|
||||
|
||||
break;
|
||||
case QEvent::FocusOut:
|
||||
utils->toggleClass(widget, "focus", false);
|
||||
@@ -44,8 +54,8 @@ bool StateEventFilter::eventFilter(QObject *obj, QEvent *event)
|
||||
utils->polishChildren(widget);
|
||||
}
|
||||
|
||||
break;
|
||||
case QEvent::MouseButtonPress:
|
||||
utils->polishChildren(widget);
|
||||
|
||||
break;
|
||||
case QEvent::HoverEnter:
|
||||
if (widget->isEnabled()) {
|
||||
@@ -53,18 +63,28 @@ bool StateEventFilter::eventFilter(QObject *obj, QEvent *event)
|
||||
}
|
||||
|
||||
utils->polishChildren(widget);
|
||||
|
||||
break;
|
||||
case QEvent::HoverLeave:
|
||||
utils->toggleClass(widget, "hover", false);
|
||||
|
||||
utils->polishChildren(widget);
|
||||
|
||||
break;
|
||||
case QEvent::EnabledChange:
|
||||
bool widgetEnabled = widget->isEnabled();
|
||||
utils->toggleClass(widget, "disabled", !widgetEnabled);
|
||||
utils->toggleClass(widget, "disabled", !widget->isEnabled());
|
||||
|
||||
utils->polishChildren(widget);
|
||||
|
||||
break;
|
||||
default:
|
||||
updateIconColors = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (updateIconColors) {
|
||||
// Delay icon update
|
||||
QTimer::singleShot(0, this, [this, widget]() { utils->applyColorToIcon(widget); });
|
||||
}
|
||||
|
||||
return QObject::eventFilter(obj, event);
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
#include <Idian/Utils.hpp>
|
||||
|
||||
#include <QWidget>
|
||||
#include <QAbstractbutton>
|
||||
#include <QStyleOptionButton>
|
||||
|
||||
namespace idian {
|
||||
class StateEventFilter : public QObject {
|
||||
@@ -21,5 +19,7 @@ public slots:
|
||||
private:
|
||||
Utils *utils;
|
||||
QWidget *target;
|
||||
|
||||
void applyColorToIcon();
|
||||
};
|
||||
} // namespace idian
|
||||
|
||||
@@ -17,10 +17,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QAbstractButton>
|
||||
#include <QFocusEvent>
|
||||
#include <QPainter>
|
||||
#include <QRegularExpression>
|
||||
#include <QStyle>
|
||||
#include <QStyleOptionButton>
|
||||
#include <QTimer>
|
||||
#include <QWidget>
|
||||
|
||||
namespace idian {
|
||||
@@ -125,6 +128,34 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
static void applyColorToIcon(QWidget *widget)
|
||||
{
|
||||
QAbstractButton *button = qobject_cast<QAbstractButton *>(widget);
|
||||
if (button && !button->icon().isNull()) {
|
||||
// Filter is on a widget with an icon set, update it's colors
|
||||
QStyleOptionButton opt;
|
||||
opt.initFrom(button);
|
||||
|
||||
QColor color = opt.palette.color(QPalette::ButtonText);
|
||||
QPixmap tinted = recolorPixmap(button->icon().pixmap(button->iconSize(), QIcon::Normal), color);
|
||||
QIcon tintedIcon;
|
||||
tintedIcon.addPixmap(tinted, QIcon::Normal);
|
||||
tintedIcon.addPixmap(tinted, QIcon::Disabled);
|
||||
|
||||
button->setIcon(tintedIcon);
|
||||
}
|
||||
}
|
||||
|
||||
static QPixmap recolorPixmap(const QPixmap &src, const QColor &color)
|
||||
{
|
||||
QImage img = src.toImage();
|
||||
QPainter p(&img);
|
||||
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
||||
p.fillRect(img.rect(), color);
|
||||
p.end();
|
||||
return QPixmap::fromImage(img);
|
||||
}
|
||||
|
||||
void applyStateStylingEventFilter(QWidget *widget);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user