qt-material-widgets/components/progress.cpp

177 lines
3.4 KiB
C++
Raw Normal View History

2022-02-17 15:17:19 +00:00
#include "progress.h"
#include "progress_p.h"
2017-09-29 13:33:26 +00:00
#include <QPropertyAnimation>
#include <QPainter>
2020-08-03 14:16:38 +00:00
#include <QPainterPath>
2022-02-17 15:17:19 +00:00
#include "progress_internal.h"
#include "lib/style.h"
2022-02-17 02:33:48 +00:00
namespace md
{
2017-09-29 13:33:26 +00:00
/*!
2022-02-17 02:33:48 +00:00
* \class ProgressBarPrivate
2017-09-29 13:33:26 +00:00
* \internal
*/
2022-02-17 02:33:48 +00:00
ProgressBarPrivate::ProgressBarPrivate(ProgressBar *q)
2017-09-29 13:33:26 +00:00
: q_ptr(q)
{
}
2022-02-17 02:33:48 +00:00
ProgressBarPrivate::~ProgressBarPrivate()
2017-09-29 13:33:26 +00:00
{
}
2022-02-17 02:33:48 +00:00
void ProgressBarPrivate::init()
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_Q(ProgressBar);
2017-09-29 13:33:26 +00:00
2022-02-17 02:33:48 +00:00
delegate = new ProgressBarDelegate(q);
2017-09-29 13:33:26 +00:00
progressType = Material::IndeterminateProgress;
useThemeColors = true;
QPropertyAnimation *animation;
animation = new QPropertyAnimation(q);
animation->setPropertyName("offset");
animation->setTargetObject(delegate);
animation->setStartValue(0);
animation->setEndValue(1);
animation->setDuration(1000);
animation->setLoopCount(-1);
animation->start();
}
/*!
* \class QtMaterialProgress
*/
2022-02-17 02:33:48 +00:00
ProgressBar::ProgressBar(QWidget *parent)
2017-09-29 13:33:26 +00:00
: QProgressBar(parent),
2022-02-17 02:33:48 +00:00
d_ptr(new ProgressBarPrivate(this))
2017-09-29 13:33:26 +00:00
{
d_func()->init();
}
2022-02-17 02:33:48 +00:00
ProgressBar::~ProgressBar()
2017-09-29 13:33:26 +00:00
{
}
2022-02-17 02:33:48 +00:00
void ProgressBar::setProgressType(Material::ProgressType type)
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_D(ProgressBar);
2017-09-29 13:33:26 +00:00
d->progressType = type;
update();
}
2022-02-17 02:33:48 +00:00
Material::ProgressType ProgressBar::progressType() const
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_D(const ProgressBar);
2017-09-29 13:33:26 +00:00
return d->progressType;
}
2022-02-17 02:33:48 +00:00
void ProgressBar::setUseThemeColors(bool state)
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_D(ProgressBar);
2017-09-29 13:33:26 +00:00
if (d->useThemeColors == state) {
return;
}
d->useThemeColors = state;
update();
}
2022-02-17 02:33:48 +00:00
bool ProgressBar::useThemeColors() const
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_D(const ProgressBar);
2017-09-29 13:33:26 +00:00
return d->useThemeColors;
}
2022-02-17 02:33:48 +00:00
void ProgressBar::setProgressColor(const QColor &color)
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_D(ProgressBar);
2017-09-29 13:33:26 +00:00
d->progressColor = color;
2017-09-29 18:09:22 +00:00
MATERIAL_DISABLE_THEME_COLORS
update();
2017-09-29 13:33:26 +00:00
}
2022-02-17 02:33:48 +00:00
QColor ProgressBar::progressColor() const
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_D(const ProgressBar);
2017-09-29 13:33:26 +00:00
if (d->useThemeColors || !d->progressColor.isValid()) {
2022-02-17 02:33:48 +00:00
return Style::instance().themeColor("primary1");
2017-09-29 13:33:26 +00:00
} else {
return d->progressColor;
}
}
2022-02-17 02:33:48 +00:00
void ProgressBar::setBackgroundColor(const QColor &color)
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_D(ProgressBar);
2017-09-29 13:33:26 +00:00
d->backgroundColor = color;
2017-09-29 18:09:22 +00:00
MATERIAL_DISABLE_THEME_COLORS
update();
2017-09-29 13:33:26 +00:00
}
2022-02-17 02:33:48 +00:00
QColor ProgressBar::backgroundColor() const
2017-09-29 13:33:26 +00:00
{
2022-02-17 02:33:48 +00:00
Q_D(const ProgressBar);
2017-09-29 13:33:26 +00:00
if (d->useThemeColors || !d->backgroundColor.isValid()) {
2022-02-17 02:33:48 +00:00
return Style::instance().themeColor("border");
2017-09-29 13:33:26 +00:00
} else {
return d->backgroundColor;
}
}
/*!
* \reimp
*/
2022-02-17 02:33:48 +00:00
void ProgressBar::paintEvent(QPaintEvent *event)
2017-09-29 13:33:26 +00:00
{
Q_UNUSED(event)
2022-02-17 02:33:48 +00:00
Q_D(ProgressBar);
2017-09-29 13:33:26 +00:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(isEnabled() ? backgroundColor()
2022-02-17 02:33:48 +00:00
: Style::instance().themeColor("disabled"));
2017-09-29 13:33:26 +00:00
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
QPainterPath path;
path.addRoundedRect(0, height()/2-3, width(), 6, 3, 3);
painter.setClipPath(path);
painter.drawRect(0, 0, width(), height());
if (isEnabled())
{
brush.setColor(progressColor());
painter.setBrush(brush);
if (Material::IndeterminateProgress == d->progressType) {
painter.drawRect(d->delegate->offset()*width()*2-width(), 0, width(), height());
} else {
qreal p = static_cast<qreal>(width())*(value()-minimum())/(maximum()-minimum());
painter.drawRect(0, 0, p, height());
}
}
}
2022-02-17 02:33:48 +00:00
}