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#606
  • Loading branch information
deepin-ci-robot authored and FeiWang1119 committed Sep 26, 2024
1 parent 40a4a63 commit e3c9426
Show file tree
Hide file tree
Showing 21 changed files with 354 additions and 40 deletions.
1 change: 1 addition & 0 deletions include/DWidget/DBounceAnimation
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dbounceanimation.h"
31 changes: 31 additions & 0 deletions include/widgets/dbounceanimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef DBOUNCEANIMATION_H
#define DBOUNCEANIMATION_H

#include <DObject>
#include <QObject>

class QPropertyAnimation;
class QAbstractScrollArea;
class DBounceAnimationPrivate;
class DBounceAnimation : public QObject, public DTK_CORE_NAMESPACE::DObject
{
Q_OBJECT
public:
explicit DBounceAnimation(QObject *parent = nullptr);

void setAnimationTarget(QAbstractScrollArea *w);
void setAniMationEnable(bool enable);

protected:
bool eventFilter(QObject *o, QEvent *e) override;
void bounceBack(Qt::Orientation orientation);

private:
D_DECLARE_PRIVATE(DBounceAnimation)

};

#endif // DBOUNCEANIMATION_H
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.
Binary file modified src/widgets/assets/icons/bloom/switch_off.dci
Binary file not shown.
Binary file modified src/widgets/assets/icons/bloom/switch_on.dci
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 @@ -74,5 +74,9 @@
<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>
<file alias="checkbox_unchecked.dci">bloom/checkbox_unchecked.dci</file>
</qresource>
</RCC>
107 changes: 107 additions & 0 deletions src/widgets/dbounceanimation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "private/dbounceanimation_p.h"
#include <QPropertyAnimation>
#include <QEvent>
#include <QDebug>
#include <QAbstractScrollArea>
#include <QScrollBar>
#include <QWheelEvent>
#include <QTimer>

DBounceAnimationPrivate::DBounceAnimationPrivate(DBounceAnimation *qq)
: DObjectPrivate (qq)
, m_animation(nullptr)
, m_animationTarget(nullptr)
, m_deltaSum(0)
{
}

DBounceAnimation::DBounceAnimation(QObject *parent)
: QObject(parent)
, DObject(*new DBounceAnimationPrivate(this))
{
}

void DBounceAnimation::setAnimationTarget(QAbstractScrollArea *w)
{
D_D(DBounceAnimation);
if (!w)
return;

if (d->m_animationTarget == w)
return;

d->m_animationTarget = w;
}

void DBounceAnimation::setAniMationEnable(bool enable)
{
D_D(DBounceAnimation);
enable ? d->m_animationTarget->installEventFilter(this)
: d->m_animationTarget->removeEventFilter(this);
}

bool DBounceAnimation::eventFilter(QObject *o, QEvent *e)
{
D_D(DBounceAnimation);
if (e->type() == QEvent::Wheel) {
if (auto absscroll = dynamic_cast<QAbstractScrollArea *>(o)) {
if (auto wheelEvent = dynamic_cast<QWheelEvent *>(e)) {
if (absscroll->verticalScrollBar()->value() <= 0 || absscroll->verticalScrollBar()->value() >= absscroll->verticalScrollBar()->maximum()) {
d->m_deltaSum += wheelEvent->pixelDelta().x() != 0 ? wheelEvent->pixelDelta().x() : wheelEvent->pixelDelta().y();
bounceBack(wheelEvent->angleDelta().x() == 0 ? Qt::Vertical : Qt::Horizontal);
}
}
}
}

return false;
}

void DBounceAnimation::bounceBack(Qt::Orientation orientation)
{
D_D(DBounceAnimation);
if (d->m_animation)
return;

if (orientation & Qt::Vertical && d->m_animationTarget->verticalScrollBar()->maximum() == d->m_animationTarget->verticalScrollBar()->minimum())
return;

if (orientation & Qt::Horizontal && d->m_animationTarget->horizontalScrollBar()->maximum() == d->m_animationTarget->horizontalScrollBar()->minimum())
return;

d->m_animation = new QPropertyAnimation(this);
d->m_animation->setTargetObject(d->m_animationTarget->viewport());
d->m_animation->setPropertyName("pos");
d->m_animation->setDuration(100);
d->m_animation->setEasingCurve(QEasingCurve::InQuart);
d->m_animation->setStartValue(QPoint(d->m_animationTarget->viewport()->x(), d->m_animationTarget->viewport()->y()));

QTimer::singleShot(100, this, [this, d, orientation]() {

if (orientation & Qt::Vertical) {
d->m_animation->setEndValue(
QPoint(d->m_animationTarget->viewport()->x(), d->m_animationTarget->viewport()->y() + d->m_deltaSum / 16));
} else {
d->m_animation->setEndValue(
QPoint(d->m_animationTarget->viewport()->x() + d->m_deltaSum / 16, d->m_animationTarget->viewport()->y()));
}

d->m_animation->start();

connect(d->m_animation, &QPropertyAnimation::finished, this, [d]() {
if (d->m_animation->direction() == QPropertyAnimation::Backward) {
delete d->m_animation;
d->m_animation = nullptr;
return;
}

d->m_animation->setDirection(QPropertyAnimation::Direction::Backward);
d->m_animation->setDuration(1000);
d->m_animation->start(QPropertyAnimation::DeleteWhenStopped);
d->m_deltaSum = 0;
});
});
}
29 changes: 17 additions & 12 deletions src/widgets/dindeterminateprogressbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
#include "private/dindeterminateprogressbar_p.h"

#include <DStyle>
#include <DGuiApplicationHelper>

#include <QPainter>
#include <QTimer>
#include <QPropertyAnimation>
#include <QDebug>
#include <QPainterPath>

DGUI_USE_NAMESPACE

const int SPOT_WIDGET_WIDTH = 200;

DIndeterminateProgressbarPrivate::DIndeterminateProgressbarPrivate(DIndeterminateProgressbar *qq)
Expand Down Expand Up @@ -48,6 +51,7 @@ DIndeterminateProgressbar::DIndeterminateProgressbar(QWidget *parent)

d->m_leftToRight ? step += 2 : step -= 2;
d->m_sliderWidget->move(step, 0);
update();
});
d->m_timer->start();
}
Expand Down Expand Up @@ -81,42 +85,43 @@ void DIndeterminateProgressbar::paintEvent(QPaintEvent *e)
? radius = height() / 2
: radius = DTK_WIDGET_NAMESPACE::DStyle::pixelMetric(style(), DTK_WIDGET_NAMESPACE::DStyle::PM_FrameRadius);

p.setBrush(QColor(0, 0, 0, int(0.1 * 255)));
bool isDarkType = DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType;
QColor color = isDarkType ? QColor(255, 255, 255, int(0.1 * 255)) : QColor(0, 0, 0, int(0.1 * 255));
p.setBrush(color);
p.setPen(Qt::NoPen);

p.drawRoundedRect(rect(), radius, radius);

QPen pen;
pen.setWidth(1);
pen.setColor(QColor(0, 0, 0, int(0.2 * 255)));
pen.setColor(color);
p.setBrush(Qt::NoBrush);
p.setPen(pen);
p.drawRoundedRect(rect().marginsRemoved(QMargins(1, 1, 1, 1)), radius, radius);
p.drawRoundedRect(rect(), radius, radius);

p.setPen(Qt::NoPen);
p.setBrush(palette().highlight().color());
p.drawRoundedRect(d->m_sliderWidget->geometry(), radius, radius);

pen.setColor(QColor(0, 0, 0, int(0.3 * 255)));
QColor highLightColor(palette().highlight().color());
auto borderColor = isDarkType ? DGuiApplicationHelper::adjustColor(highLightColor, 0, 0, +10, 0, 0, 0, 0)
: DGuiApplicationHelper::adjustColor(highLightColor, 0, 0, -20, 0, 0, 0, -20);
pen.setColor(borderColor);
p.setBrush(Qt::NoBrush);
p.setPen(pen);
p.drawRoundedRect(d->m_sliderWidget->geometry().marginsRemoved(QMargins(1, 1, 1, 1)), radius, radius);
p.drawRoundedRect(d->m_sliderWidget->geometry(), radius, radius);

if (d->m_sliderWidget->width() < d->m_spotWidget->width() / 2)
return;

QPointF pointStart(d->m_spotWidget->geometry().left(), d->m_spotWidget->geometry().center().y());
QPointF pointEnd(d->m_spotWidget->geometry().right(), d->m_spotWidget->geometry().center().y());

QColor shadowColor(0, 0, 0, int(0.15 * 255));
QColor spotColor(255, 255, 255, int(0.5 * 255));
QColor highLightColor(palette().highlight().color());
QColor spotColor = DGuiApplicationHelper::adjustColor(highLightColor, 0, +30, +30, 0, 0, 0, 0);

QLinearGradient linear(pointStart, pointEnd);
linear.setColorAt(0, highLightColor);
linear.setColorAt(0.35, shadowColor);
linear.setColorAt(0.5, spotColor);
linear.setColorAt(0.65, shadowColor);
linear.setColorAt(1, highLightColor);
linear.setSpread(QGradient::PadSpread);
linear.setInterpolationMode(QLinearGradient::InterpolationMode::ColorInterpolation);
Expand All @@ -125,9 +130,9 @@ void DIndeterminateProgressbar::paintEvent(QPaintEvent *e)
p.setPen(Qt::NoPen);

QPainterPath clipPath;
clipPath.addRoundedRect(d->m_sliderWidget->geometry(), radius, radius);
clipPath.addRoundedRect(d->m_sliderWidget->geometry().marginsRemoved(QMargins(1, 1, 1, 1)), radius - 1, radius - 1);
p.setClipPath(clipPath);
p.setClipping(true);
p.drawRoundedRect(d->m_spotWidget->geometry().marginsRemoved(QMargins(2, 2, 2, 2)), radius, radius);
p.drawRoundedRect(d->m_spotWidget->geometry(), radius, radius);
p.setClipping(false);
}
7 changes: 7 additions & 0 deletions src/widgets/dlistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "dstyleditemdelegate.h"
#include "dstyle.h"

#include <DBounceAnimation>

DWIDGET_BEGIN_NAMESPACE

DVariantListModel::DVariantListModel(QObject *parent) :
Expand Down Expand Up @@ -196,6 +198,11 @@ DListView::DListView(QWidget *parent) :
DObject(*new DListViewPrivate(this))
{
d_func()->init();
if (!qEnvironmentVariableIsSet("DTK_DISABLE_LISTVIEW_ANIMATION")) {
auto ani = new DBounceAnimation(this);
ani->setAnimationTarget(this);
ani->setAniMationEnable(true);
}
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/dmessagemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ImageLabel : public QLabel {
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.setOpacity(m_opacity);
p.drawPixmap(rect().marginsRemoved(contentsMargins()), *pixmap());
p.drawPixmap(rect().marginsRemoved(contentsMargins()), pixmap(Qt::ReturnByValue));
};
private:
qreal m_opacity;
Expand Down
55 changes: 46 additions & 9 deletions src/widgets/dsearchedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ DWIDGET_BEGIN_NAMESPACE
DCORE_USE_NAMESPACE
DGUI_USE_NAMESPACE

constexpr int ANI_DURATION = 200;
constexpr int HIDE_CURSOR_MARGIN = -4;

#ifdef ENABLE_AI
class VoiceDevice : public QIODevice
{
Expand Down Expand Up @@ -270,7 +273,11 @@ DSearchEditPrivate::DSearchEditPrivate(DSearchEdit *q)
, action(nullptr)
, iconWidget(nullptr)
, label(nullptr)
, animation(new QPropertyAnimation)
{
animation->setPropertyName("pos");
animation->setEasingCurve(QEasingCurve::OutCubic);
animation->setDuration(ANI_DURATION);
}

DSearchEditPrivate::~DSearchEditPrivate()
Expand Down Expand Up @@ -377,15 +384,31 @@ void DSearchEditPrivate::_q_toEditMode(bool focus)
{
D_Q(DSearchEdit);

if (focus || !q->lineEdit()->text().isEmpty()) {
action->setVisible(true);
iconWidget->setVisible(false);
lineEdit->setPlaceholderText(placeholderText);
} else {
action->setVisible(false);
iconWidget->setVisible(true);
lineEdit->setPlaceholderText(QString());
}
if (animation->state() == QPropertyAnimation::Running)
return;

auto textMargins = q->lineEdit()->textMargins();
QMargins marginsInAnimation(HIDE_CURSOR_MARGIN, 0, 0, 0);

if (!animation->parent())
animation->setParent(iconWidget);

animation->setTargetObject(iconWidget);
animation->setStartValue(QPoint(q->lineEdit()->geometry().center().x() - iconWidget->width() / 2, iconWidget->pos().y()));
animation->setEndValue(QPoint(0, iconWidget->pos().y()));

q->connect(animation, &QPropertyAnimation::finished, q, [q, this, textMargins]() {
q->lineEdit()->setTextMargins(textMargins);
if (animation->direction() == QPropertyAnimation::Direction::Forward) {
action->setVisible(true);
iconWidget->setVisible(false);
lineEdit->setPlaceholderText(placeholderText);
} else {
iconWidget->setVisible(true);
lineEdit->setPlaceholderText(QString());
iconWidget->move(QPoint(q->lineEdit()->geometry().center().x() - iconWidget->width() / 2, iconWidget->pos().y()));
}
});

#ifdef ENABLE_AI
//Focus disappears, clear voice check
Expand All @@ -394,6 +417,20 @@ void DSearchEditPrivate::_q_toEditMode(bool focus)
_q_onVoiceActionTrigger(false);
}
#endif

if (!q->lineEdit()->text().isEmpty())
return;

if (focus) {
animation->setDirection(QPropertyAnimation::Direction::Forward);
} else {
action->setVisible(false);
animation->setDirection(QPropertyAnimation::Direction::Backward);
}

iconWidget->setVisible(true);
q->lineEdit()->setTextMargins(marginsInAnimation);
animation->start();
}

void DSearchEditPrivate::_q_onVoiceActionTrigger(bool checked)
Expand Down
Loading

0 comments on commit e3c9426

Please sign in to comment.