From 7d3f27b7caf8f0c73c13a3286d5825edea6a3c87 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 10 Jan 2022 08:59:14 +0300 Subject: [PATCH] Decomposed painting of SettingsButton. --- ui/widgets/buttons.cpp | 26 ++++++++++++++++++++++---- ui/widgets/buttons.h | 7 +++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/ui/widgets/buttons.cpp b/ui/widgets/buttons.cpp index bc9d890..4519357 100644 --- a/ui/widgets/buttons.cpp +++ b/ui/widgets/buttons.cpp @@ -741,19 +741,35 @@ void SettingsButton::setColorOverride(std::optional textColorOverride) { update(); } +const style::SettingsButton &SettingsButton::st() const { + return _st; +} + void SettingsButton::paintEvent(QPaintEvent *e) { Painter p(this); - auto paintOver = (isOver() || isDown()) && !isDisabled(); - p.fillRect(e->rect(), paintOver ? _st.textBgOver : _st.textBg); + const auto paintOver = (isOver() || isDown()) && !isDisabled(); + paintBg(p, e->rect(), paintOver); paintRipple(p, 0, 0); - auto outerw = width(); + const auto outerw = width(); + paintText(p, paintOver, outerw); + + if (_toggle) { + paintToggle(p, outerw); + } +} + +void SettingsButton::paintBg(Painter &p, const QRect &rect, bool over) const { + p.fillRect(rect, over ? _st.textBgOver : _st.textBg); +} + +void SettingsButton::paintText(Painter &p, bool over, int outerw) const { p.setFont(_st.font); p.setPen(_textColorOverride ? QPen(*_textColorOverride) - : paintOver + : over ? _st.textFgOver : _st.textFg); p.drawTextLeft( @@ -762,7 +778,9 @@ void SettingsButton::paintEvent(QPaintEvent *e) { outerw, _text, _textWidth); +} +void SettingsButton::paintToggle(Painter &p, int outerw) const { if (_toggle) { auto rect = toggleRect(); _toggle->paint(p, rect.left(), rect.top(), outerw); diff --git a/ui/widgets/buttons.h b/ui/widgets/buttons.h index 0d25b34..0e0c835 100644 --- a/ui/widgets/buttons.h +++ b/ui/widgets/buttons.h @@ -13,6 +13,8 @@ #include +class Painter; + namespace Ui { class RippleAnimation; @@ -273,6 +275,11 @@ protected: void paintEvent(QPaintEvent *e) override; + const style::SettingsButton &st() const; + void paintBg(Painter &p, const QRect &rect, bool over) const; + void paintText(Painter &p, bool over, int outerw) const; + void paintToggle(Painter &p, int outerw) const; + private: void setText(QString &&text); QRect toggleRect() const;