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

sync: from linuxdeepin/dtkwidget #91

Merged
merged 1 commit into from
Oct 28, 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
11 changes: 10 additions & 1 deletion include/widgets/dstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <dtkwidget_global.h>
#include <DPalette>
#include <DDciIcon>
#include <DGuiApplicationHelper>

#include <QCommonStyle>
#include <QPainter>
Expand All @@ -19,9 +20,17 @@ QT_BEGIN_NAMESPACE
class QTextLayout;
QT_END_NAMESPACE

#define ENABLE_ANIMATIONS DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::HasAnimations)
#define ENABLE_ANIMATION_BUTTONBOX (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_BUTTONBOX"))
#define ENABLE_ANIMATION_MESSAGE (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_MESSAGE"))
#define ENABLE_ANIMATION_LISTVIEWBOUNCE (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_LISTVIREBOUNCE"))
#define ENABLE_ANIMATION_SEARCH (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_SEARCH"))
#define ENABLE_ANIMATION_SWITCHBUTTON (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_SWITCHBUTTON"))
#define ENABLE_ANIMATION_PROGRESSBAR (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_PROGRESSBAR"))
#define ENABLE_ANIMATION_RADIOBUTTON (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_RADIOBUTTON"))
#define ENABLE_ANIMATION_CHECKBOX (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_CHECKBOX"))

DWIDGET_BEGIN_NAMESPACE

DGUI_USE_NAMESPACE

namespace DDrawUtils
Expand Down
45 changes: 32 additions & 13 deletions src/widgets/dbuttonbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ void DButtonBoxButton::paintEvent(QPaintEvent *e)
DStylePainter p(this);
DStyleOptionButtonBoxButton option;
initStyleOption(&option);
option.palette.setColor(QPalette::HighlightedText, this->palette().highlight().color());

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX)
option.palette.setColor(QPalette::HighlightedText, this->palette().highlight().color());

p.drawControl(DStyle::CE_ButtonBoxButton, option);
}

Expand Down Expand Up @@ -363,10 +366,13 @@ DButtonBoxPrivate::DButtonBoxPrivate(DButtonBox *qq)
, m_hoverId(-1)
, m_checkedId(-1)
, m_pressId(-1)
, m_hoverAnimation(new QVariantAnimation(qq))
, m_checkMoveAnimation(new QVariantAnimation(qq))
, m_hoverAnimation(nullptr)
, m_checkMoveAnimation(nullptr)
{

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) {
m_hoverAnimation = new QVariantAnimation(qq);
m_checkMoveAnimation = new QVariantAnimation(qq);
}
}

void DButtonBoxPrivate::init()
Expand All @@ -380,14 +386,17 @@ void DButtonBoxPrivate::init()
q->connect(group, SIGNAL(buttonPressed(QAbstractButton*)), q, SIGNAL(buttonPressed(QAbstractButton*)));
q->connect(group, SIGNAL(buttonReleased(QAbstractButton*)), q, SIGNAL(buttonReleased(QAbstractButton*)));
q->connect(group, SIGNAL(buttonToggled(QAbstractButton*, bool)), q, SIGNAL(buttonToggled(QAbstractButton*, bool)));
q->connect(m_hoverAnimation, &QVariantAnimation::valueChanged, q, [q]() {
q->update();
});
q->connect(m_checkMoveAnimation, &QVariantAnimation::valueChanged, q, [q]() {
q->update();
});
m_hoverAnimation->setDuration(HOVER_ANI_DURATION);
m_checkMoveAnimation->setDuration(CHECK_ANI_DURATION);

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) {
q->connect(m_hoverAnimation, &QVariantAnimation::valueChanged, q, [q]() {
q->update();
});
q->connect(m_checkMoveAnimation, &QVariantAnimation::valueChanged, q, [q]() {
q->update();
});
m_hoverAnimation->setDuration(HOVER_ANI_DURATION);
m_checkMoveAnimation->setDuration(CHECK_ANI_DURATION);
}

layout = new QHBoxLayout(q);
layout->setContentsMargins(0, 0, 0, 0);
Expand Down Expand Up @@ -506,7 +515,9 @@ void DButtonBox::setButtonList(const QList<DButtonBoxButton *> &list, bool check
d->group->addButton(button);

button->setCheckable(checkable);
button->installEventFilter(this);

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX)
button->installEventFilter(this);
}
}

Expand Down Expand Up @@ -616,6 +627,11 @@ void DButtonBox::paintEvent(QPaintEvent *e)
opt.state |= QStyle::State_Active;
}

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_BUTTONBOX) {
p.drawControl(QStyle::CE_PushButtonBevel, opt);
return;
}

bool isDarkType = DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType;
int radius = DStyle::pixelMetric(style(), DStyle::PM_FrameRadius);
QColor background;
Expand Down Expand Up @@ -706,6 +722,9 @@ void DButtonBox::paintEvent(QPaintEvent *e)

bool DButtonBox::eventFilter(QObject *o, QEvent *e)
{
if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_BUTTONBOX)
return QWidget::eventFilter(o, e);

D_D(DButtonBox);
for (int i = 0; i < buttonList().size(); ++i) {
if (o == buttonList().at(i)) {
Expand Down
31 changes: 22 additions & 9 deletions src/widgets/dfloatingmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,23 @@ void DFloatingMessagePrivate::init()
iconButton->setIconSize(DSizeModeHelper::element(QSize(20, 20), QSize(30, 30)));

hBoxLayout->addWidget(iconButton);
hBoxLayout->addSpacing(10);

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE)
hBoxLayout->addSpacing(10);

hBoxLayout->addWidget(labMessage);

if (notifyType == DFloatingMessage::MessageType::TransientType) { //临时消息
timer = new QTimer(q);
timer->setInterval(4000);
timer->setSingleShot(true);
q->connect(timer, &QTimer::timeout, q, [q]() {
q->close();
Q_EMIT q->messageClosed();
});
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE)
q->connect(timer, &QTimer::timeout, q, [q]() {
q->close();
Q_EMIT q->messageClosed();
});
else
q->connect(timer, &QTimer::timeout, q, &DFloatingMessage::close);
} else { //常驻消息
content = nullptr;
closeButton = new DDialogCloseButton(q);
Expand All @@ -74,12 +80,19 @@ void DFloatingMessagePrivate::init()

hBoxLayout->addWidget(closeButton);
q->connect(closeButton, &DIconButton::clicked, q, &DFloatingMessage::closeButtonClicked);
q->connect(closeButton, &DIconButton::clicked, q, [q]() {
q->close();
Q_EMIT q->messageClosed();
});

if(ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE)
q->connect(closeButton, &DIconButton::clicked, q, [q]() {
q->close();
Q_EMIT q->messageClosed();
});
else
q->connect(closeButton, &DIconButton::clicked, q, &DFloatingMessage::close);
}

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE)
return;

auto effect = new QGraphicsDropShadowEffect(q);
effect->setColor(QColor(0, 0, 0, 0.1 * 255));
effect->setBlurRadius(20);
Expand Down
23 changes: 19 additions & 4 deletions src/widgets/dindeterminateprogressbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@ DIndeterminateProgressbarPrivate::DIndeterminateProgressbarPrivate(DIndeterminat
, m_sliderWidget(new QWidget(qq))
, m_timer(new QTimer(qq))
, m_leftToRight(true)
, m_spotWidget(new QWidget(qq))
, m_animation(new QPropertyAnimation(m_spotWidget, "pos", qq))
, m_spotWidget(nullptr)
, m_animation(nullptr)
{
if (!ENABLE_ANIMATIONS && !ENABLE_ANIMATION_PROGRESSBAR)
return;

m_spotWidget = new QWidget(qq);
m_animation = new QPropertyAnimation(m_spotWidget, "pos", qq);
}

DIndeterminateProgressbar::DIndeterminateProgressbar(QWidget *parent)
: QWidget(parent)
, DObject(*new DIndeterminateProgressbarPrivate(this))
{
D_D(DIndeterminateProgressbar);
d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height());
d->m_spotWidget->move(-SPOT_WIDGET_WIDTH, 0);

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_PROGRESSBAR) {
d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height());
d->m_spotWidget->move(-SPOT_WIDGET_WIDTH, 0);
}

d->m_sliderWidget->setFixedWidth(150);
d->m_sliderWidget->move(0, 0);
Expand All @@ -60,6 +68,10 @@ void DIndeterminateProgressbar::resizeEvent(QResizeEvent *e)
{
D_D(DIndeterminateProgressbar);
d->m_sliderWidget->setFixedHeight(height());

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_PROGRESSBAR)
QWidget::resizeEvent(e);

d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height());

d->m_animation->setStartValue(QPoint(-SPOT_WIDGET_WIDTH, 0));
Expand Down Expand Up @@ -111,6 +123,9 @@ void DIndeterminateProgressbar::paintEvent(QPaintEvent *e)
p.setPen(pen);
p.drawRoundedRect(d->m_sliderWidget->geometry(), radius, radius);

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_PROGRESSBAR)
return;

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

Expand Down
2 changes: 1 addition & 1 deletion src/widgets/dlistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ DListView::DListView(QWidget *parent) :
DObject(*new DListViewPrivate(this))
{
d_func()->init();
if (!qEnvironmentVariableIsSet("DTK_DISABLE_LISTVIEW_ANIMATION")) {
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_SEARCH) {
auto ani = new DBounceAnimation(this);
ani->setAnimationTarget(this);
ani->setAniMationEnable(true);
Expand Down
103 changes: 74 additions & 29 deletions src/widgets/dmessagemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,19 @@ static void sendMessage_helper(DMessageManager *manager, QWidget *par, IconType

DMessageManagerPrivate::DMessageManagerPrivate(DMessageManager *qq)
: DObjectPrivate(qq)
, m_aniGeometry(new QPropertyAnimation(qq))
, m_aniOpacity(new QPropertyAnimation(qq))
, m_aniGroup(new QParallelAnimationGroup(qq))
, m_label(new ImageLabel)
, m_aniGeometry(nullptr)
, m_aniOpacity(nullptr)
, m_aniGroup(nullptr)
, m_label(nullptr)
{
if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE)
return;

m_aniGeometry = new QPropertyAnimation(qq);
m_aniOpacity = new QPropertyAnimation(qq);
m_aniGroup = new QParallelAnimationGroup(qq);
m_label = new ImageLabel;

m_aniGeometry->setPropertyName("geometry");
m_aniGeometry->setDuration(ANIMATION_DURATION);
m_aniGeometry->setEasingCurve(QEasingCurve::OutCubic);
Expand Down Expand Up @@ -187,14 +195,20 @@ void DMessageManager::sendMessage(QWidget *par, DFloatingMessage *floMsg)
layout->setContentsMargins(0, 0, 0, 0);
layout->setDirection(QBoxLayout::BottomToTop);
}

if (content->layout()->count() >= 1) {
content->layout()->itemAt(content->layout()->count() - 1)->widget()->hide();
delete content->layout()->takeAt(content->layout()->count() - 1);
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) {
if (content->layout()->count() >= 1) {
content->layout()->itemAt(content->layout()->count() - 1)->widget()->hide();
delete content->layout()->takeAt(content->layout()->count() - 1);
}
} else {
content->show();
}

static_cast<QBoxLayout*>(content->layout())->addWidget(floMsg, 0, Qt::AlignHCenter);

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE)
return;

// 限制通知消息的最大宽度
for (DFloatingMessage *message : content->findChildren<DFloatingMessage*>(QString(), Qt::FindDirectChildrenOnly)) {
message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width());
Expand All @@ -214,31 +228,35 @@ void DMessageManager::sendMessage(QWidget *par, DFloatingMessage *floMsg)
d->m_label->setParent(par);
d->m_label->setAlignment(Qt::AlignCenter);
d->m_label->setContentsMargins(MARGIN, 0, MARGIN, 0);
d->m_label->setPixmap(floMsg->grab());

if (floMsg && !floMsg->grab().isNull())
d->m_label->setPixmap(floMsg->grab());

d->m_label->setScaledContents(true);
d->m_label->show();
d->m_aniGeometry->setTargetObject(d->m_label);
d->m_aniOpacity->setTargetObject(d->m_label);
d->m_aniGeometry->setStartValue(QRect(par->rect().center().x(), par->rect().bottom(), 0, 0));
d->m_aniGeometry->setEndValue(content->geometry());
d->m_aniGroup->setDirection(QAbstractAnimation::Forward);
d->m_aniGroup->start();
connect(d->m_aniGroup, &QPropertyAnimation::finished, this, [d, content]() {
if (d->m_aniGroup->direction() == QAbstractAnimation::Backward) {
d->m_aniGroup->setDirection(QAbstractAnimation::Forward);
} else {
content->show();
}
connect(d->m_aniGroup, &QPropertyAnimation::finished, this, [d, this, content]() {
content->show();
d->m_label->hide();
disconnect(d->m_aniGroup, &QPropertyAnimation::finished, this, nullptr);
});

connect(floMsg, &DFloatingMessage::messageClosed, [=, this]() {
connect(floMsg, &DFloatingMessage::messageClosed, [=]() {
d->m_aniGeometry->setStartValue(QRect(par->rect().center().x(), par->rect().bottom(), 0, 0));
d->m_aniGeometry->setEndValue(content->geometry());
d->m_label->setPixmap(floMsg->grab());

d->m_aniGroup->setDirection(QAbstractAnimation::Backward);
if (floMsg && !floMsg->grab().isNull())
d->m_label->setPixmap(floMsg->grab());

d->m_label->show();
d->m_aniGroup->setDirection(QAbstractAnimation::Backward);
d->m_aniGroup->start();
content->hide();
});
}

Expand Down Expand Up @@ -287,20 +305,47 @@ bool DMessageManager::setContentMargens(QWidget *par, const QMargins &margins)
*/
bool DMessageManager::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::Resize) {
if (auto content = watched->findChild<QWidget *>(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly)) {
bool isOnlyResizeEvent = ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE ? event->type() == QEvent::Resize : event->type() == QEvent::LayoutRequest || event->type() == QEvent::Resize;

auto par = qobject_cast<QWidget *>(watched);
if (isOnlyResizeEvent) {
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) {
if (auto content = watched->findChild<QWidget *>(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly)) {

for (DFloatingMessage *message : content->findChildren<DFloatingMessage*>(QString(), Qt::FindDirectChildrenOnly)) {
message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width());
message->setMinimumHeight(message->sizeHint().height());
}
auto par = qobject_cast<QWidget *>(watched);

for (DFloatingMessage *message : content->findChildren<DFloatingMessage*>(QString(), Qt::FindDirectChildrenOnly)) {
message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width());
message->setMinimumHeight(message->sizeHint().height());
}

QRect geometry(QPoint(0, 0), content->sizeHint());
geometry.moveCenter(par->rect().center());
geometry.moveBottom(par->rect().bottom() - MESSGAE_HEIGHT);
content->setGeometry(geometry);
QRect geometry(QPoint(0, 0), content->sizeHint());
geometry.moveCenter(par->rect().center());
geometry.moveBottom(par->rect().bottom() - MESSGAE_HEIGHT);
content->setGeometry(geometry);
}
} else {
if (QWidget *widget = qobject_cast<QWidget *>(watched)) {
QWidget *content;

if (widget->objectName() == D_MESSAGE_MANAGER_CONTENT) {
content = widget;
} else {
content = widget->findChild<QWidget*>(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly);
}

QWidget *par = content->parentWidget();

// 限制通知消息的最大宽度
for (DFloatingMessage *message : content->findChildren<DFloatingMessage*>(QString(), Qt::FindDirectChildrenOnly)) {
message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width());
message->setMinimumHeight(message->sizeHint().height());
}

QRect geometry(QPoint(0, 0), content->sizeHint());
geometry.moveCenter(par->rect().center());
geometry.moveBottom(par->rect().bottom());
content->setGeometry(geometry);
}
}
} else if (event->type() == QEvent::ChildRemoved) {
// 如果是通知消息被删除的事件
Expand Down
Loading
Loading