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重绘toolbutton, 增加动画 #594

Merged
merged 1 commit 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
15 changes: 13 additions & 2 deletions include/widgets/dtoolbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,34 @@
#ifndef DTOOLBUTTON_H
#define DTOOLBUTTON_H

#include <QToolButton>
#include <dtkwidget_global.h>

#include <QToolButton>

#include <DDciIcon>
#include <DDciIconPlayer>

DWIDGET_BEGIN_NAMESPACE
DGUI_USE_NAMESPACE

class LIBDTKWIDGETSHARED_EXPORT DToolButton : public QToolButton
class DToolButtonPrivate;
class LIBDTKWIDGETSHARED_EXPORT DToolButton : public QToolButton, public DCORE_NAMESPACE::DObject
{
Q_OBJECT
public:
DToolButton(QWidget *parent = nullptr);
void setAlignment(Qt::Alignment flag);
Qt::Alignment alignment() const;
void setDciIcon(const DDciIcon &dciIcon);

protected:
void paintEvent(QPaintEvent *event) override;
void initStyleOption(QStyleOptionToolButton *option) const;
QSize sizeHint() const override;
bool event(QEvent *e) override;

private:
D_DECLARE_PRIVATE(DToolButton)
};

DWIDGET_END_NAMESPACE
Expand Down
Binary file not shown.
Binary file not shown.
Binary file added src/widgets/assets/icons/bloom/radio_checked.dci
Binary file not shown.
Binary file not shown.
4 changes: 4 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,9 @@
<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="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>
<file alias="checkbox_unchecked.dci">bloom/checkbox_unchecked.dci</file>
</qresource>
</RCC>
77 changes: 75 additions & 2 deletions src/widgets/dtoolbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "dtoolbutton.h"
#include "private/dtoolbutton_p.h"

#include <DGuiApplicationHelper>
#include <DStyle>

#include <QStyleOptionButton>
#include <QStylePainter>
#include <QEvent>
#include <QDebug>
#include <QKeyEvent>

DWIDGET_BEGIN_NAMESPACE

Dtk::Widget::DToolButtonPrivate::DToolButtonPrivate(DToolButton *qq)
: DObjectPrivate(qq)
{

}

/*!
@~english
@class Dtk::Widget::DToolButton
Expand All @@ -17,8 +29,15 @@ DWIDGET_BEGIN_NAMESPACE

DToolButton::DToolButton(QWidget *parent)
: QToolButton(parent)
, DObject(*new DToolButtonPrivate(this))
{

D_D(DToolButton);
connect(this, &DToolButton::pressed, this, [d]() {
d->m_dciPlayer.play(DDciIcon::Pressed);
});
connect(this, &DToolButton::released, this, [d]() {
d->m_dciPlayer.play(DDciIcon::Normal);
});
}

/*!
Expand All @@ -30,10 +49,26 @@ DToolButton::DToolButton(QWidget *parent)

void DToolButton::paintEvent(QPaintEvent *event)
{
D_D(DToolButton);
Q_UNUSED(event)
QStylePainter p(this);
QStyleOptionToolButton opt;
initStyleOption(&opt);

if (!d->m_dciIcon.isNull()) {
p.setRenderHint(QPainter::SmoothPixmapTransform);
p.drawImage(rect(), d->m_dciPlayer.currentImage());

if (opt.state & QStyle::State_HasFocus) {
p.setPen(QPen(palette().highlight().color(), 2));
p.setBrush(Qt::NoBrush);
p.setRenderHint(QPainter::Antialiasing);
int radius = DStyle::pixelMetric(style(), DStyle::PM_FrameRadius);
p.drawRoundedRect(opt.rect.marginsRemoved(QMargins(1, 1, 1, 1)), radius, radius);
}
return;
}

p.drawComplexControl(QStyle::CC_ToolButton, opt);
}

Expand All @@ -60,6 +95,26 @@ QSize DToolButton::sizeHint() const
return QToolButton::sizeHint();
}

bool DToolButton::event(QEvent *e)
{
D_D(DToolButton);
if (d->m_dciIcon.isNull())
return QToolButton::event(e);

if (e->type() == QEvent::WindowActivate) {
auto palette = DDciIconPalette::fromQPalette(this->palette());
d->m_dciPlayer.setPalette(palette);
d->m_dciPlayer.setTheme(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType
? DDciIcon::Dark : DDciIcon::Light);
d->m_dciPlayer.setMode(DDciIcon::Normal);
} else if (e->type() == QEvent::HoverEnter) {
d->m_dciPlayer.play(DDciIcon::Hover);
} else if (e->type() == QEvent::HoverLeave) {
d->m_dciPlayer.play(DDciIcon::Normal);
}
return QToolButton::event(e);
}

/*!
@~english
@fn void DToolButton::setAlignment(Qt::Alignment flag)
Expand All @@ -85,4 +140,22 @@ Qt::Alignment DToolButton::alignment() const
return Qt::AlignLeft;
}

void DToolButton::setDciIcon(const DDciIcon &dciIcon)
{
D_D(DToolButton);
d->m_dciIcon = dciIcon;
d->m_dciPlayer.setIcon(dciIcon);
d->m_dciPlayer.setIconSize(120);

connect(&d->m_dciPlayer, &DDciIconPlayer::updated, this, [this]() {
update();
});

connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [this, d](DGuiApplicationHelper::ColorType colorType) {
auto palette = DDciIconPalette::fromQPalette(this->palette());
d->m_dciPlayer.setPalette(palette);
d->m_dciPlayer.setTheme(colorType ? DDciIcon::Dark : DDciIcon::Light);
});
}

DWIDGET_END_NAMESPACE
26 changes: 26 additions & 0 deletions src/widgets/private/dtoolbutton_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef DABOUTDIALOG_P_H
#define DABOUTDIALOG_P_H

#include <dtoolbutton.h>
#include <DObjectPrivate>

DWIDGET_BEGIN_NAMESPACE

class DToolButtonPrivate : public DCORE_NAMESPACE::DObjectPrivate

Check warning on line 13 in src/widgets/private/dtoolbutton_p.h

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

class Dtk::Widget::DToolButtonPrivate is not documented!
{
public:
DToolButtonPrivate(DToolButton *qq);

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

View workflow job for this annotation

GitHub Actions / cppcheck

Class 'DToolButtonPrivate' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.

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

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

function Dtk::Widget::DToolButtonPrivate::DToolButtonPrivate is not documented!

DDciIcon m_dciIcon;

Check warning on line 18 in src/widgets/private/dtoolbutton_p.h

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

variable DDciIcon Dtk::Widget::DToolButtonPrivate::m_dciIcon is not documented!
DDciIconPlayer m_dciPlayer;

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

View workflow job for this annotation

GitHub Actions / check_job / DOXYGEN_CHECK

variable DDciIconPlayer Dtk::Widget::DToolButtonPrivate::m_dciPlayer is not documented!

Q_DECLARE_PUBLIC(DToolButton)
};

DWIDGET_END_NAMESPACE

#endif // DABOUTDIALOG_P_H
Loading