Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 使用dci icon重绘switchButton, 增加动画 #593

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/widgets/assets/icons/bloom/switch_off.dci
Binary file not shown.
Binary file added src/widgets/assets/icons/bloom/switch_on.dci
Binary file not shown.
2 changes: 2 additions & 0 deletions src/widgets/assets/icons/dtk-icon-theme.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
<file alias="window_maximize.dci">bloom/window_maximize.dci</file>
<file alias="window_minimize.dci">bloom/window_minimize.dci</file>
<file alias="window_normal.dci">bloom/window_normal.dci</file>
<file alias="switch_on.dci">bloom/switch_on.dci</file>
<file alias="switch_off.dci">bloom/switch_off.dci</file>
<file alias="radio_checked.dci">bloom/radio_checked.dci</file>
<file alias="radio_unchecked.dci">bloom/radio_unchecked.dci</file>
<file alias="checkbox_checked.dci">bloom/checkbox_checked.dci</file>
Expand Down
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
59 changes: 55 additions & 4 deletions src/widgets/dswitchbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
// 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>
#include <QTimer>

DWIDGET_BEGIN_NAMESPACE

constexpr int DCI_ICON_SIZE = 120;
constexpr int TIMER_INTERVAL = 200;

/*!
@~english
@brief DSwitchButton::DSwitchButton implements a switch button
Expand Down Expand Up @@ -48,12 +54,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(2, -10, -2, 8), d->player.currentImage()); // 为了显示按钮的阴影所留的空白
}

/*!
Expand Down Expand Up @@ -88,6 +98,7 @@ void DSwitchButton::initStyleOption(DStyleOptionButton *option) const

DSwitchButtonPrivate::DSwitchButtonPrivate(DSwitchButton *qq)
: DObjectPrivate(qq)
, timer(new QTimer(qq))
{

}
Expand All @@ -102,13 +113,53 @@ void DSwitchButtonPrivate::init()
checked = false;
animationStartValue = 0;
animationEndValue = 1;
timer->setInterval(TIMER_INTERVAL);

D_Q(DSwitchButton);

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);
auto palette = DDciIconPalette::fromQPalette(q->palette());
player.setPalette(palette);
player.setDevicePixelRatio(qApp->devicePixelRatio());
player.setIconSize(DCI_ICON_SIZE);
player.setTheme(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType
? DDciIcon::Dark : DDciIcon::Light);
};

initPlayer();

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);
timer->start();

Q_EMIT q->checkedChanged(checked);
});

q->connect(&player, &DDciIconPlayer::updated, q, [q]() {
q->update();
});

q->connect(timer, &QTimer::timeout, q, [q, this]() {
player.stop();
player.setIcon(!q->isChecked() ? DDciIcon::fromTheme("switch_on") : DDciIcon::fromTheme("switch_off"));
player.setMode(DDciIcon::Normal);
timer->stop();
});

q->connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, q, initPlayer);
}

DWIDGET_END_NAMESPACE
5 changes: 5 additions & 0 deletions src/widgets/private/dswitchbutton_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
#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!
{
public:
explicit DSwitchButtonPrivate(DSwitchButton *qq);

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

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

function Dtk::Widget::DSwitchButtonPrivate::DSwitchButtonPrivate is not documented!
~DSwitchButtonPrivate();

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

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

function Dtk::Widget::DSwitchButtonPrivate::~DSwitchButtonPrivate is not documented!

void init();

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

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

function void Dtk::Widget::DSwitchButtonPrivate::init is not documented!

public:
bool checked = false;

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

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

variable bool Dtk::Widget::DSwitchButtonPrivate::checked is not documented!
QColor enabledBackground = qRgba(207, 214, 230, 255);
QColor disabledBackground = qRgba(207, 214, 230, 255);
QColor checkedBackground = qRgba(44, 167, 248, 255);
Expand All @@ -28,6 +30,9 @@
double animationStartValue = 0.0;
double animationEndValue = 0.0;

DDciIconPlayer player;
QTimer *timer;

public:
D_DECLARE_PUBLIC(DSwitchButton)
};
Expand Down
Loading