diff --git a/src/widgets/dstyle.cpp b/src/widgets/dstyle.cpp index 234d0da24..d19556fc6 100644 --- a/src/widgets/dstyle.cpp +++ b/src/widgets/dstyle.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2019 - 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2019 - 2024 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later @@ -1443,9 +1443,6 @@ void DStyle::drawControl(const QStyle *style, DStyle::ControlElement ce, const Q DStyleOptionButton option = *btn; option.dpalette = btn->dpalette; option.rect = dstyle.subElementRect(SE_SwitchButtonGroove, opt, w); - dstyle.drawPrimitive(PE_SwitchButtonGroove, &option, p, w); - option.rect = dstyle.subElementRect(SE_SwitchButtonHandle, opt, w); - dstyle.drawPrimitive(PE_SwitchButtonHandle, &option, p, w); if (btn->state & State_HasFocus) { QStyleOptionFocusRect fropt; diff --git a/src/widgets/dswitchbutton.cpp b/src/widgets/dswitchbutton.cpp index 273269737..dcbd18c2a 100644 --- a/src/widgets/dswitchbutton.cpp +++ b/src/widgets/dswitchbutton.cpp @@ -3,12 +3,14 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #include "dswitchbutton.h" -#include -#include #include "private/dswitchbutton_p.h" -#include +#include +#include +#include +#include +#include DWIDGET_BEGIN_NAMESPACE @@ -48,12 +50,16 @@ QSize DSwitchButton::sizeHint() const */ void DSwitchButton::paintEvent(QPaintEvent *e) { + D_D(DSwitchButton); Q_UNUSED(e); DStylePainter painter(this); DStyleOptionButton opt; initStyleOption(&opt); painter.drawControl(DStyle::CE_SwitchButton, opt); + + painter.setRenderHint(QPainter::SmoothPixmapTransform); + painter.drawImage(rect().adjusted(0, -12, 0, 12), d->player.currentImage()); } /*! @@ -108,7 +114,37 @@ void DSwitchButtonPrivate::init() q->setObjectName("DSwitchButton"); q->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); q->setCheckable(true); - q->connect(q, &DSwitchButton::toggled, q, &DSwitchButton::checkedChanged); + + auto initPlayer= [this, q]() { + DDciIcon icon = !checked ? DDciIcon::fromTheme("switch_on") : DDciIcon::fromTheme("switch_off"); + player.setIcon(icon); + player.setMode(DDciIcon::Mode::Normal); + DDciIconPalette palette; + palette.setHighlight(q->palette().highlight().color()); + player.setPalette(palette); + }; + + initPlayer(); + player.setDevicePixelRatio(qApp->devicePixelRatio()); + player.setIconSize(120); + + q->connect(q, &DSwitchButton::toggled, q, [q, this](bool ckd) { + if (checked == ckd) + return; + + checked = ckd; + DDciIcon icon = checked ? DDciIcon::fromTheme("switch_on") : DDciIcon::fromTheme("switch_off"); + player.setIcon(icon); + player.play(DDciIcon::Mode::Normal); + + Q_EMIT q->checkedChanged(checked); + }); + + q->connect(&player, &DDciIconPlayer::updated, q, [q, this]() { + q->update(); + }); + + q->connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, q, initPlayer); } DWIDGET_END_NAMESPACE diff --git a/src/widgets/private/dswitchbutton_p.h b/src/widgets/private/dswitchbutton_p.h index 6fc76e72e..81c8513ad 100644 --- a/src/widgets/private/dswitchbutton_p.h +++ b/src/widgets/private/dswitchbutton_p.h @@ -6,9 +6,11 @@ #define DSWITCHBUTTON_P_H #include +#include #include +DGUI_USE_NAMESPACE DWIDGET_BEGIN_NAMESPACE class DSwitchButtonPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate @@ -28,6 +30,8 @@ class DSwitchButtonPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate double animationStartValue = 0.0; double animationEndValue = 0.0; + DDciIconPlayer player; + public: D_DECLARE_PUBLIC(DSwitchButton) };