代码拉取完成,页面将自动刷新
#include "qtmaterialcircularprogress.h"
#include "lib/qtmaterialstyle.h"
#include "qtmaterialcircularprogress_internal.h"
#include "qtmaterialcircularprogress_p.h"
#include <QPainter>
#include <QPainterPath>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
/*!
* \class QtMaterialCircularProgressPrivate
* \internal
*/
QtMaterialCircularProgressPrivate::QtMaterialCircularProgressPrivate(QtMaterialCircularProgress *q)
: q_ptr(q)
{
}
QtMaterialCircularProgressPrivate::~QtMaterialCircularProgressPrivate()
{
}
void QtMaterialCircularProgressPrivate::init()
{
Q_Q(QtMaterialCircularProgress);
delegate = new QtMaterialCircularProgressDelegate(q);
progressType = Material::IndeterminateProgress;
penWidth = 6.25;
size = 64;
useThemeColors = true;
q->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
QParallelAnimationGroup *group = new QParallelAnimationGroup(q);
group->setLoopCount(-1);
QPropertyAnimation *animation;
animation = new QPropertyAnimation(q);
animation->setPropertyName("dashLength");
animation->setTargetObject(delegate);
animation->setEasingCurve(QEasingCurve::InOutQuad);
animation->setStartValue(0.1);
animation->setKeyValueAt(0.15, 0.2);
animation->setKeyValueAt(0.6, 20);
animation->setKeyValueAt(0.7, 20);
animation->setEndValue(20);
animation->setDuration(2050);
group->addAnimation(animation);
animation = new QPropertyAnimation(q);
animation->setPropertyName("dashOffset");
animation->setTargetObject(delegate);
animation->setEasingCurve(QEasingCurve::InOutSine);
animation->setStartValue(0);
animation->setKeyValueAt(0.15, 0);
animation->setKeyValueAt(0.6, -7);
animation->setKeyValueAt(0.7, -7);
animation->setEndValue(-25);
animation->setDuration(2050);
group->addAnimation(animation);
animation = new QPropertyAnimation(q);
animation->setPropertyName("angle");
animation->setTargetObject(delegate);
animation->setStartValue(0);
animation->setEndValue(719);
animation->setDuration(2050);
group->addAnimation(animation);
group->start();
}
/*!
* \class QtMaterialCircularProgress
*/
QtMaterialCircularProgress::QtMaterialCircularProgress(QWidget *parent)
: QProgressBar(parent),
d_ptr(new QtMaterialCircularProgressPrivate(this))
{
d_func()->init();
}
QtMaterialCircularProgress::~QtMaterialCircularProgress()
{
}
void QtMaterialCircularProgress::setProgressType(Material::ProgressType type)
{
Q_D(QtMaterialCircularProgress);
d->progressType = type;
update();
}
Material::ProgressType QtMaterialCircularProgress::progressType() const
{
Q_D(const QtMaterialCircularProgress);
return d->progressType;
}
void QtMaterialCircularProgress::setUseThemeColors(bool value)
{
Q_D(QtMaterialCircularProgress);
if (d->useThemeColors == value) {
return;
}
d->useThemeColors = value;
update();
}
bool QtMaterialCircularProgress::useThemeColors() const
{
Q_D(const QtMaterialCircularProgress);
return d->useThemeColors;
}
void QtMaterialCircularProgress::setLineWidth(qreal width)
{
Q_D(QtMaterialCircularProgress);
d->penWidth = width;
update();
updateGeometry();
}
qreal QtMaterialCircularProgress::lineWidth() const
{
Q_D(const QtMaterialCircularProgress);
return d->penWidth;
}
void QtMaterialCircularProgress::setSize(int size)
{
Q_D(QtMaterialCircularProgress);
d->size = size;
update();
updateGeometry();
}
int QtMaterialCircularProgress::size() const
{
Q_D(const QtMaterialCircularProgress);
return d->size;
}
void QtMaterialCircularProgress::setColor(const QColor &color)
{
Q_D(QtMaterialCircularProgress);
d->color = color;
d->useThemeColors = false;
update();
}
QColor QtMaterialCircularProgress::color() const
{
Q_D(const QtMaterialCircularProgress);
if (d->useThemeColors || !d->color.isValid()) {
return QtMaterialStyle::instance().themeColor(Material::ColorThemePrimaryMain);
}
return d->color;
}
/*!
* \reimp
*/
QSize QtMaterialCircularProgress::sizeHint() const
{
Q_D(const QtMaterialCircularProgress);
const qreal s = d->size + d->penWidth + 8;
return QSize(s, s);
}
/*!
* \reimp
*/
void QtMaterialCircularProgress::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
Q_D(QtMaterialCircularProgress);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// if (!isEnabled())
// {
// QPen pen;
// pen.setCapStyle(Qt::RoundCap);
// pen.setWidthF(d->penWidth);
// pen.setColor(QtMaterialStyle::instance().themeColor(Material::ColorThemeBorder));
// painter.setPen(pen);
// painter.drawLine(rect().center() - QPointF(20, 20), rect().center() + QPointF(20, 20));
// painter.drawLine(rect().center() + QPointF(20, -20), rect().center() - QPointF(20, -20));
// return;
// }
if (d->progressType == Material::IndeterminateProgress) {
painter.translate(width() / 2, height() / 2);
painter.rotate(d->delegate->angle());
}
QPen pen;
pen.setCapStyle(Qt::RoundCap);
pen.setWidthF(d->penWidth);
pen.setColor(color());
if (d->progressType == Material::IndeterminateProgress) {
QVector<qreal> pattern;
pattern << d->delegate->dashLength() * d->size / 50 << 30 * d->size / 50;
pen.setDashOffset(d->delegate->dashOffset() * d->size / 50);
pen.setDashPattern(pattern);
painter.setPen(pen);
painter.drawEllipse(QPoint(0, 0), d->size / 2, d->size / 2);
} else {
painter.setPen(pen);
const qreal x = (width() - d->size) / 2.0;
const qreal y = (height() - d->size) / 2.0;
const qreal a = 360.0 * (value() - minimum()) / (maximum() - minimum());
int startAngle = 90;
painter.drawArc(QRect(x, y, d->size, d->size), startAngle * 16, -a * 16);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。