qt-material-widgets/components/progress.cpp

152 lines
2.9 KiB
C++
Raw Normal View History

2016-06-12 08:21:58 +00:00
#include "progress.h"
2016-06-19 14:27:21 +00:00
#include "progress_p.h"
2016-06-18 09:36:40 +00:00
#include <QPainter>
#include <QPropertyAnimation>
2016-06-19 10:34:10 +00:00
#include <QDebug>
#include "progress_internal.h"
#include "lib/style.h"
2016-06-18 09:12:20 +00:00
ProgressPrivate::ProgressPrivate(Progress *q)
: q_ptr(q),
2016-06-21 12:09:16 +00:00
delegate(new ProgressDelegate(q)),
progressType(Material::IndeterminateProgress),
useThemeColors(true)
2016-06-18 09:12:20 +00:00
{
}
ProgressPrivate::~ProgressPrivate()
{
}
void ProgressPrivate::init()
{
Q_Q(Progress);
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();
2016-06-18 09:12:20 +00:00
}
2016-06-12 08:21:58 +00:00
Progress::Progress(QWidget *parent)
2016-06-19 09:18:05 +00:00
: QProgressBar(parent),
2016-06-18 09:12:20 +00:00
d_ptr(new ProgressPrivate(this))
2016-06-12 08:21:58 +00:00
{
2016-06-18 09:12:20 +00:00
d_func()->init();
2016-06-12 08:21:58 +00:00
}
Progress::~Progress()
{
}
2016-06-18 09:36:40 +00:00
2016-06-19 09:18:05 +00:00
void Progress::setProgressType(Material::ProgressType type)
{
Q_D(Progress);
d->progressType = type;
update();
}
Material::ProgressType Progress::progressType() const
{
Q_D(const Progress);
return d->progressType;
}
void Progress::setUseThemeColors(bool state)
{
Q_D(Progress);
d->useThemeColors = state;
update();
}
bool Progress::useThemeColors() const
{
Q_D(const Progress);
return d->useThemeColors;
}
void Progress::setProgressColor(const QColor &color)
{
Q_D(Progress);
d->progressColor = color;
setUseThemeColors(false);
}
QColor Progress::progressColor() const
{
Q_D(const Progress);
if (d->useThemeColors || !d->progressColor.isValid()) {
return Style::instance().themeColor("primary1");
} else {
return d->progressColor;
}
}
void Progress::setBackgroundColor(const QColor &color)
{
Q_D(Progress);
d->backgroundColor = color;
setUseThemeColors(false);
}
QColor Progress::backgroundColor() const
{
Q_D(const Progress);
if (d->useThemeColors || !d->backgroundColor.isValid()) {
return Style::instance().themeColor("accent3");
} else {
return d->backgroundColor;
}
}
2016-06-18 09:36:40 +00:00
void Progress::paintEvent(QPaintEvent *event)
{
2016-06-19 09:18:05 +00:00
Q_UNUSED(event)
Q_D(Progress);
2016-06-18 09:36:40 +00:00
QPainter painter(this);
2016-06-19 09:18:05 +00:00
painter.setRenderHint(QPainter::Antialiasing);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(backgroundColor());
2016-06-19 09:18:05 +00:00
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
QPainterPath path;
path.addRoundedRect(0, height()/2-3, width(), 6, 3, 3);
2016-06-19 09:18:05 +00:00
painter.setClipPath(path);
painter.drawRect(0, 0, width(), height());
brush.setColor(progressColor());
2016-06-19 09:18:05 +00:00
painter.setBrush(brush);
2016-06-18 09:36:40 +00:00
2016-06-19 10:34:10 +00:00
if (Material::IndeterminateProgress == d->progressType)
{
painter.drawRect(d->delegate->offset()*width()*2-width(), 0, width(), height());
2016-06-19 10:34:10 +00:00
}
else
{
qreal p = static_cast<qreal>(width())*(value()-minimum())/(maximum()-minimum());
painter.drawRect(0, 0, p, height());
}
2016-06-18 09:36:40 +00:00
}