summaryrefslogtreecommitdiffstats
path: root/src/progress.cpp
blob: cf5328be2ab1f56d44fbdd867120b91ccd12ca35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "progress.h"

#include <QProgressBar>
#include <QHBoxLayout>
#include <QResizeEvent>
#include <QLabel>

Progress::Progress(QString text, QWidget *parent)
    : QWidget(parent)
    , _label(new QLabel(text, this))
    , _progress(new QProgressBar(this))
    , _hidden(false)
{
    auto *l = new QHBoxLayout(this);
    l->setMargin(0);
    setLayout(l);
    _progress->setRange(0, 100);
    _label->setMinimumHeight(16);
    _progress->setMinimumHeight(16);
    _label->show();
    _progress->show();
    l->addWidget(_label);
    l->addWidget(_progress);
    _label->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
    _progress->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred));
}

Progress::~Progress()
{

}

void Progress::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    auto size = event->size();
    int width = size.width() - 110;
    if (width < 120) {
        width = size.width();
        _progress->hide();
    } else if (_progress->value() != -1) {
        _progress->show();
    }
    _label->setMinimumWidth(width);
}

void Progress::setProgress(int percent)
{
    if (percent == -1) {
        if (!_hidden || _progress->isVisible()) {
            _hidden = true;
            _progress->hide();
        }
    } else {
        _progress->setValue(percent);
        if (_hidden) {
            _hidden = false;
            if (_label->width() > 200) {
                _progress->show();
            }
        }
    }
}

void Progress::setCaption(const QString &caption)
{
    _label->setText(caption);
}