Skip to content

Commit

Permalink
chore: 使用dci icon重绘switchButton, 增加动画
Browse files Browse the repository at this point in the history
使用dci icon重绘switchButton, 增加动画

Log:
  • Loading branch information
Whale107 committed Aug 5, 2024
1 parent b8497a4 commit dcbc782
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/widgets/dstyle.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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;
Expand Down
44 changes: 40 additions & 4 deletions src/widgets/dswitchbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "dswitchbutton.h"
#include <DStyle>
#include <DStyleOptionButton>
#include "private/dswitchbutton_p.h"

#include <QApplication>
#include <DStyleOptionButton>
#include <DStyle>
#include <DDciIcon>
#include <DGuiApplicationHelper>

#include <QApplication>

DWIDGET_BEGIN_NAMESPACE

Expand Down Expand Up @@ -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());
}

/*!
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/widgets/private/dswitchbutton_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#define DSWITCHBUTTON_P_H

#include <DSwitchButton>
#include <DDciIconPlayer>

#include <DObjectPrivate>

DGUI_USE_NAMESPACE
DWIDGET_BEGIN_NAMESPACE

class DSwitchButtonPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate

Check warning on line 16 in src/widgets/private/dswitchbutton_p.h

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

class Dtk::Widget::DSwitchButtonPrivate is not documented!
Expand All @@ -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)
};
Expand Down

0 comments on commit dcbc782

Please sign in to comment.