This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
modern-linbo-gui/sources/qmodernprogressbar.cpp

119 lines
3.7 KiB
C++
Raw Normal View History

2020-11-25 14:40:16 +01:00
/****************************************************************************
** Modern Linbo GUI
** Copyright (C) 2020 Dorian Zedler <dorian@itsblue.de>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as published
** by the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "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();
// 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);
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();
}
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;
// 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 {
// 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-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));
painter.end();
QWidget::paintEvent(e);
}