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 committed Aug 5, 2024
1 parent c8b8148 commit 656e8d0
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 9 deletions.
31 changes: 31 additions & 0 deletions examples/collections/progressbarexample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <DWaterProgress>
#include <QPropertyAnimation>
#include <DColoredProgressBar>
#include <DIndeterminateProgressbar>
#include "progressbarexample.h"

DWIDGET_USE_NAMESPACE
Expand All @@ -28,6 +29,7 @@ ProgressBarExampleWindow::ProgressBarExampleWindow(QWidget *parent)
addExampleWindow(new DProgressBarExample(this));
addExampleWindow(new DWaterProgressExample(this));
addExampleWindow(new DColoredProgressBarExample(this));
addExampleWindow(new DIndeterminateProgressBarExample(this));
}

DProgressBarExample::DProgressBarExample(QWidget *parent)
Expand Down Expand Up @@ -181,3 +183,32 @@ int DColoredProgressBarExample::getFixedHeight() const
{
return 200;
}

DIndeterminateProgressBarExample::DIndeterminateProgressBarExample(QWidget *parent)
: ExampleWindowInterface(parent)
{
auto mainLayout = new QVBoxLayout(this);
auto indeterBar = new DIndeterminateProgressbar();
indeterBar->setFixedSize(500, 35);
mainLayout->addWidget(indeterBar, 0, Qt::AlignCenter);
setLayout(mainLayout);
}

QString DIndeterminateProgressBarExample::getTitleName() const
{
return "DIndeterminateProgressbar";
}

QString DIndeterminateProgressBarExample::getDescriptionInfo() const
{
return QString("一个模糊进度条,不展示具体进度值,\n"
"用于等待时间不确定的情况。主要用\n"
"在小工具主窗口内部,作为一个中间状态\n"
"展示给用户,最终的结果往往会跟随成功\n"
"或者失败的图标。");
}

int DIndeterminateProgressBarExample::getFixedHeight() const
{
return 200;
}
12 changes: 12 additions & 0 deletions examples/collections/progressbarexample.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,16 @@ class DColoredProgressBarExample : public ExampleWindowInterface
int getFixedHeight() const override;
};

class DIndeterminateProgressBarExample : public ExampleWindowInterface
{
Q_OBJECT

public:
explicit DIndeterminateProgressBarExample(QWidget *parent = nullptr);

QString getTitleName() const override;
QString getDescriptionInfo() const override;
int getFixedHeight() const override;
};

#endif // PROGRESSBAREXAMPLE_H
1 change: 1 addition & 0 deletions include/DWidget/DIndeterminateProgressbar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dindeterminateprogressbar.h"
26 changes: 26 additions & 0 deletions include/widgets/dindeterminateprogressbar.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 DINDETERMINATEPROGRESSBAR_H
#define DINDETERMINATEPROGRESSBAR_H

#include <DObject>
#include <QWidget>

class DIndeterminateProgressbarPrivate;
class DIndeterminateProgressbar : public QWidget, public DTK_CORE_NAMESPACE::DObject
{
Q_OBJECT
public:
explicit DIndeterminateProgressbar(QWidget *parent = nullptr);

protected:
void paintEvent(QPaintEvent *e) override;
void resizeEvent(QResizeEvent *e) override;

private:
D_DECLARE_PRIVATE(DIndeterminateProgressbar)
};

#endif // DINDETERMINATEPROGRESSBAR_H
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>
133 changes: 133 additions & 0 deletions src/widgets/dindeterminateprogressbar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "private/dindeterminateprogressbar_p.h"

#include <DStyle>

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

const int SPOT_WIDGET_WIDTH = 200;

DIndeterminateProgressbarPrivate::DIndeterminateProgressbarPrivate(DIndeterminateProgressbar *qq)
: DObjectPrivate(qq)
, 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))
{
}

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);

d->m_sliderWidget->setFixedWidth(150);
d->m_sliderWidget->move(0, 0);

d->m_timer->setInterval(10);
static int step = 0;
connect(d->m_timer, &QTimer::timeout, this, [this, d]() {
if (d->m_sliderWidget->geometry().right() >= rect().right()) {
d->m_leftToRight = false;
}

if (d->m_sliderWidget->geometry().left() <= rect().left()) {
d->m_leftToRight = true;
}

d->m_leftToRight ? step += 2 : step -= 2;
d->m_sliderWidget->move(step, 0);
});
d->m_timer->start();
}

void DIndeterminateProgressbar::resizeEvent(QResizeEvent *e)
{
D_D(DIndeterminateProgressbar);
d->m_sliderWidget->setFixedHeight(height());
d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height());

d->m_animation->setStartValue(QPoint(-SPOT_WIDGET_WIDTH, 0));
d->m_animation->setEndValue(QPoint(rect().right(), 0));
d->m_animation->setDuration(3000);
d->m_animation->setEasingCurve(QEasingCurve::InQuad);
d->m_animation->start();
connect(d->m_animation, &QPropertyAnimation::finished, this, [d]() {
d->m_animation->start();
});
QWidget::resizeEvent(e);
}

void DIndeterminateProgressbar::paintEvent(QPaintEvent *e)
{
D_D(DIndeterminateProgressbar);
QWidget::paintEvent(e);
QPainter p(this);

p.setRenderHint(QPainter::Antialiasing);
int radius;
this->height() <= DTK_WIDGET_NAMESPACE::DStyle::pixelMetric(style(), DTK_WIDGET_NAMESPACE::DStyle::PM_FrameRadius) * 2
? 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)));
p.setPen(Qt::NoPen);

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

QPen pen;
pen.setWidth(1);
pen.setColor(QColor(0, 0, 0, int(0.2 * 255)));
p.setBrush(Qt::NoBrush);
p.setPen(pen);
p.drawRoundedRect(rect().marginsRemoved(QMargins(1, 1, 1, 1)), 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)));
p.setBrush(Qt::NoBrush);
p.setPen(pen);
p.drawRoundedRect(d->m_sliderWidget->geometry().marginsRemoved(QMargins(1, 1, 1, 1)), 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());

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);

p.setBrush(linear);
p.setPen(Qt::NoPen);

QPainterPath clipPath;
clipPath.addRoundedRect(d->m_sliderWidget->geometry(), radius, radius);
p.setClipPath(clipPath);
p.setClipping(true);
p.drawRoundedRect(d->m_spotWidget->geometry().marginsRemoved(QMargins(2, 2, 2, 2)), radius, radius);
p.setClipping(false);
}
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
44 changes: 40 additions & 4 deletions src/widgets/dswitchbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// 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>

DWIDGET_BEGIN_NAMESPACE

Expand Down Expand Up @@ -48,12 +50,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(0, -12, 0, 12), d->player.currentImage());
}

/*!
Expand Down Expand Up @@ -108,7 +114,37 @@ void DSwitchButtonPrivate::init()
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);
DDciIconPalette palette;
palette.setHighlight(q->palette().highlight().color());
player.setPalette(palette);
};

initPlayer();
player.setDevicePixelRatio(qApp->devicePixelRatio());
player.setIconSize(120);

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);

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

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

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

DWIDGET_END_NAMESPACE
2 changes: 1 addition & 1 deletion src/widgets/dtitlebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ void DTitlebarPrivate::setIconVisible(bool visible)
return;

if (visible) {
if (auto spacerItem = dynamic_cast<QSpacerItem *>(leftLayout->takeAt(0)))
if (dynamic_cast<QSpacerItem *>(leftLayout->itemAt(0)))
delete leftLayout->takeAt(0);

leftLayout->insertSpacing(0, 10);
Expand Down
29 changes: 29 additions & 0 deletions src/widgets/private/dindeterminateprogressbar_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef DINDETERMINATEPROGRESSBAR_P_H
#define DINDETERMINATEPROGRESSBAR_P_H

#include <DObjectPrivate>
#include <DIndeterminateProgressbar>

#include <QTimer>

class QPropertyAnimation;
class DIndeterminateProgressbarPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
{
public:
DIndeterminateProgressbarPrivate(DIndeterminateProgressbar *qq);

Check warning on line 17 in src/widgets/private/dindeterminateprogressbar_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

Class 'DIndeterminateProgressbarPrivate' 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.

QWidget *m_sliderWidget;
QTimer *m_timer;
bool m_leftToRight;
QWidget *m_spotWidget;
QPropertyAnimation *m_animation;

Check warning on line 23 in src/widgets/private/dindeterminateprogressbar_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

The class 'DIndeterminateProgressbarPrivate' is unsafe, wrong usage can cause memory/resource leaks for 'DIndeterminateProgressbarPrivate::m_animation'. This can for instance be fixed by adding proper cleanup in the destructor.

private:
D_DECLARE_PUBLIC(DIndeterminateProgressbar)
};

#endif // DINDETERMINATEPROGRESSBAR_P_H
4 changes: 4 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,8 @@ class DSwitchButtonPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
double animationStartValue = 0.0;
double animationEndValue = 0.0;

DDciIconPlayer player;

public:
D_DECLARE_PUBLIC(DSwitchButton)
};
Expand Down

0 comments on commit 656e8d0

Please sign in to comment.