74 lines
2.4 KiB
C++
74 lines
2.4 KiB
C++
#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();
|
|
// finer steps, so the Animation is fluid
|
|
this->setMinimum(0);
|
|
this->setMaximum(1000);
|
|
this->indeterminateAnimtion->start();
|
|
}
|
|
else {
|
|
// reset minimum and maximum
|
|
this->setMinimum(0);
|
|
this->setMaximum(100);
|
|
this->setValue(this->preIndeterminateValue);
|
|
this->indeterminateAnimtion->stop();
|
|
}
|
|
}
|
|
|
|
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"));
|
|
|
|
// progress
|
|
if(this->indeterminate) {
|
|
int maximum = this->maximum() / 2;
|
|
if(this->value() <= maximum)
|
|
// for the first half -> fill from left
|
|
painter.fillRect(QRect(0,0, e->rect().width() * double(double(this->value()) / double(maximum)), e->rect().height()), QColor(0,0,0));
|
|
else
|
|
// for the second half -> empty from right
|
|
painter.fillRect(QRect(e->rect().width() * double(double(this->value()- (maximum)) / double(maximum)),0, e->rect().width(), e->rect().height()), QColor(0,0,0));
|
|
}
|
|
else
|
|
painter.fillRect(QRect(0,0, e->rect().width() * float(float(this->value()) / float(this->maximum())), e->rect().height()), QColor(0,0,0));
|
|
|
|
painter.end();
|
|
|
|
QWidget::paintEvent(e);
|
|
}
|
|
|