2020-11-19 14:39:32 +01:00
|
|
|
#include "../headers/qmodernprogressbar.h"
|
|
|
|
|
|
|
|
QModernProgressBar::QModernProgressBar(QWidget* parent) : QProgressBar(parent)
|
|
|
|
{
|
|
|
|
this->refreshTimer = new QTimer();
|
|
|
|
this->refreshTimer->setSingleShot(false);
|
|
|
|
this->refreshTimer->setInterval(400);
|
|
|
|
connect(this->refreshTimer, SIGNAL(timeout()), this, SLOT(updateIndeterminate()));
|
|
|
|
|
|
|
|
|
|
|
|
this->indeterminateAnimtion = new QPropertyAnimation(this, "value");
|
|
|
|
this->indeterminateAnimtion->setDuration(2000);
|
|
|
|
this->indeterminateAnimtion->setStartValue(0);
|
|
|
|
this->indeterminateAnimtion->setEndValue(1000);
|
|
|
|
this->indeterminateAnimtion->setEasingCurve(QEasingCurve(QEasingCurve::InOutQuad));
|
|
|
|
this->indeterminateAnimtion->setLoopCount(-1);
|
|
|
|
|
|
|
|
this->setValue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QModernProgressBar::setIndeterminate(bool indeterminate) {
|
|
|
|
if(this->indeterminate == indeterminate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->indeterminate = indeterminate;
|
|
|
|
|
|
|
|
if(this->indeterminate) {
|
|
|
|
this->preIndeterminateValue = this->value();
|
2020-11-24 21:42:47 +01:00
|
|
|
this->preIndeterminateMinimum = this->minimum();
|
|
|
|
this->preIndeterminateMaximum = this->maximum();
|
2020-11-19 14:39:32 +01:00
|
|
|
// finer steps, so the Animation is fluid
|
|
|
|
this->setMinimum(0);
|
|
|
|
this->setMaximum(1000);
|
|
|
|
this->indeterminateAnimtion->start();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// reset minimum and maximum
|
2020-11-24 21:42:47 +01:00
|
|
|
this->setMinimum(this->preIndeterminateMinimum);
|
|
|
|
this->setMaximum(this->preIndeterminateMaximum);
|
2020-11-19 14:39:32 +01:00
|
|
|
this->setValue(this->preIndeterminateValue);
|
|
|
|
this->indeterminateAnimtion->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 21:42:47 +01:00
|
|
|
void QModernProgressBar::setReversed(bool reversed) {
|
|
|
|
if(this->reversed == reversed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->reversed = reversed;
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:39:32 +01:00
|
|
|
bool QModernProgressBar::getIndeterminate() {
|
|
|
|
return this->indeterminate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QModernProgressBar::updateIndeterminate() {
|
|
|
|
qDebug() << "update indeterminate";
|
|
|
|
}
|
|
|
|
|
|
|
|
void QModernProgressBar::paintEvent(QPaintEvent *e) {
|
|
|
|
QPainter painter;
|
|
|
|
painter.begin(this);
|
|
|
|
// background
|
|
|
|
painter.fillRect(e->rect(), QColor("#c7c7c7"));
|
|
|
|
|
2020-11-24 21:42:47 +01:00
|
|
|
double from = 0;
|
|
|
|
double to = 0;
|
|
|
|
|
2020-11-19 14:39:32 +01:00
|
|
|
// progress
|
|
|
|
if(this->indeterminate) {
|
|
|
|
int maximum = this->maximum() / 2;
|
|
|
|
if(this->value() <= maximum)
|
|
|
|
// for the first half -> fill from left
|
2020-11-24 21:42:47 +01:00
|
|
|
to = double(double(this->value()) / double(maximum));
|
|
|
|
else {
|
2020-11-19 14:39:32 +01:00
|
|
|
// for the second half -> empty from right
|
2020-11-24 21:42:47 +01:00
|
|
|
from = double(double(this->value()- (maximum)) / double(maximum));
|
|
|
|
to = 1;
|
|
|
|
}
|
2020-11-19 14:39:32 +01:00
|
|
|
}
|
2020-11-24 21:42:47 +01:00
|
|
|
else {
|
|
|
|
to = double(double(this->value()) / double(this->maximum()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this->reversed) {
|
|
|
|
// if reversed -> reverse and swap from and to
|
|
|
|
double tmp = 1 - from;
|
|
|
|
from = 1 - to;
|
|
|
|
to = tmp;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.fillRect(QRect(e->rect().width() * from, 0, e->rect().width() * to, e->rect().height()), QColor(0,0,0));
|
2020-11-19 14:39:32 +01:00
|
|
|
|
|
|
|
painter.end();
|
|
|
|
|
|
|
|
QWidget::paintEvent(e);
|
|
|
|
}
|
|
|
|
|