Skip to content

Commit

Permalink
Fix QLineEdit "clear" button foreground color when changing theme
Browse files Browse the repository at this point in the history
  • Loading branch information
oclero committed Nov 11, 2024
1 parent 1b9b001 commit ba9eb86
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
35 changes: 26 additions & 9 deletions lib/src/style/EventFilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace oclero::qlementine {
LineEditButtonEventFilter::LineEditButtonEventFilter(
QlementineStyle& style, WidgetAnimationManager& animManager, QToolButton* button)
QlementineStyle* style, WidgetAnimationManager& animManager, QToolButton* button)
: QObject(button)
, _style(style)
, _animManager(animManager)
Expand All @@ -52,7 +52,7 @@ bool LineEditButtonEventFilter::eventFilter(QObject* watchedObject, QEvent* evt)
// Instead, place the button by ourselves.
const auto* parentLineEdit = _button->parentWidget();
const auto parentRect = parentLineEdit->rect();
const auto& theme = _style.theme();
const auto& theme = _style ? _style->theme() : Theme{};
const auto buttonH = theme.controlHeightMedium;
const auto buttonW = buttonH;
const auto spacing = theme.spacing / 2;
Expand All @@ -71,29 +71,46 @@ bool LineEditButtonEventFilter::eventFilter(QObject* watchedObject, QEvent* evt)
const auto hovered = _button->underMouse();
const auto pressed = _button->isDown();
const auto mouse = getMouseState(pressed, hovered, enabled);
const auto& theme = _style.theme();
const auto& theme = _style ? _style->theme() : Theme{};
const auto rect = _button->rect();
const auto& bgColor = _style.toolButtonBackgroundColor(mouse, ColorRole::Secondary);

const auto& bgColor =
_style ? _style->toolButtonBackgroundColor(mouse, ColorRole::Secondary)
: _button->style()->standardPalette().color(getPaletteColorGroup(mouse), QPalette::ColorRole::ButtonText);
const auto& fgColor =
_style ? _style->toolButtonForegroundColor(mouse, ColorRole::Secondary)
: _button->style()->standardPalette().color(getPaletteColorGroup(mouse), QPalette::ColorRole::Button);
const auto animationDuration = _style ? _style->theme().animationDuration : 0;
const auto& currentBgColor = _animManager.animateBackgroundColor(_button, bgColor, animationDuration);
const auto& currentFgColor = _animManager.animateForegroundColor(_button, fgColor, animationDuration);

// Get opacity animated in qlinedit_p.cpp:436
const auto opacity = _button->property(QByteArrayLiteral("opacity")).toDouble();

const auto circleH = theme.controlHeightMedium;
const auto circleW = circleH;
const auto circleX = rect.x() + (rect.width() - circleW) / 2;
const auto circleY = rect.y() + (rect.height() - circleH) / 2;
const auto circleRect = QRect(QPoint{ circleX, circleY }, QSize{ circleW, circleH });
// Get opacity animated in qlinedit_p.cpp:436
const auto opacity = _button->property(QByteArrayLiteral("opacity")).toDouble();

const auto pixmap = getPixmap(_button->icon(), theme.iconSize, mouse, CheckState::NotChecked, _button);
const auto autoIconColor = _style ? _style->autoIconColor(_button) : AutoIconColor::None;
const auto& colorizedPixmap = _style->getColorizedPixmap(pixmap, autoIconColor, currentFgColor, currentFgColor);
const auto pixmapX = circleRect.x() + (circleRect.width() - theme.iconSize.width()) / 2;
const auto pixmapY = circleRect.y() + (circleRect.height() - theme.iconSize.height()) / 2;
const auto pixmapRect = QRect{ { pixmapX, pixmapY }, theme.iconSize };
const auto& currentBgColor = _animManager.animateBackgroundColor(_button, bgColor, theme.animationDuration);

QPainter p(_button);
p.setOpacity(opacity);
p.setPen(Qt::NoPen);
p.setBrush(currentBgColor);
p.setRenderHint(QPainter::Antialiasing, true);

// Background.
p.setBrush(currentBgColor);
p.drawEllipse(circleRect);
p.drawPixmap(pixmapRect, pixmap);

// Foreground.
p.drawPixmap(pixmapRect, colorizedPixmap);

evt->accept();
return true;
Expand Down
5 changes: 3 additions & 2 deletions lib/src/style/EventFilters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
#include <QListView>
#include <QComboBox>
#include <QCommandLinkButton>
#include <QPointer>

namespace oclero::qlementine {
class LineEditButtonEventFilter : public QObject {
public:
LineEditButtonEventFilter(QlementineStyle& style, WidgetAnimationManager& animManager, QToolButton* button);
LineEditButtonEventFilter(QlementineStyle* style, WidgetAnimationManager& animManager, QToolButton* button);

bool eventFilter(QObject* watchedObject, QEvent* evt) override;

private:
QlementineStyle& _style;
QPointer<QlementineStyle> _style;
WidgetAnimationManager& _animManager;
QToolButton* _button{ nullptr };
};
Expand Down
2 changes: 1 addition & 1 deletion lib/src/style/QlementineStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4766,7 +4766,7 @@ void QlementineStyle::polish(QWidget* w) {

// Special case for the Qt-private buttons in a QLineEdit.
if (w->inherits("QLineEditIconButton")) {
w->installEventFilter(new LineEditButtonEventFilter(*this, _impl->animations, qobject_cast<QToolButton*>(w)));
w->installEventFilter(new LineEditButtonEventFilter(this, _impl->animations, qobject_cast<QToolButton*>(w)));
w->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// Fix hardcoded width in qlineedit_p.cpp:493
w->setFixedSize(_impl->theme.controlHeightMedium, _impl->theme.controlHeightMedium);
Expand Down

0 comments on commit ba9eb86

Please sign in to comment.