Skip to content

Commit

Permalink
sync: from linuxdeepin/dtkwidget
Browse files Browse the repository at this point in the history
Synchronize source files from linuxdeepin/dtkwidget.

Source-pull-request: linuxdeepin/dtkwidget#593
  • Loading branch information
deepin-ci-robot authored and FeiWang1119 committed Sep 25, 2024
1 parent ea66362 commit cb5e8ed
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
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,5 +72,7 @@
<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>
</qresource>
</RCC>
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 @@ -1452,9 +1452,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,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
Expand All @@ -28,6 +30,9 @@ class DSwitchButtonPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
double animationStartValue = 0.0;
double animationEndValue = 0.0;

DDciIconPlayer player;
QTimer *timer;

public:
D_DECLARE_PUBLIC(DSwitchButton)
};
Expand Down

0 comments on commit cb5e8ed

Please sign in to comment.