-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvertprogressbar.cpp
67 lines (58 loc) · 2.7 KB
/
invertprogressbar.cpp
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
#include "invertprogressbar.h"
#include <QStyle>
#include <QStylePainter>
#include <QStyleOptionProgressBar>
InvertProgressBar::InvertProgressBar(QWidget *parent) : QProgressBar(parent) {}
InvertProgressBar::~InvertProgressBar() {}
void InvertProgressBar::paintEvent(QPaintEvent *ev) {
QStylePainter paint(this);
QStyleOptionProgressBar opt;
initStyleOption(&opt);
paint.drawControl(QStyle::CE_ProgressBarGroove, opt);
paint.drawControl(QStyle::CE_ProgressBarContents, opt);
const QStyleOptionProgressBar *option = &opt;
QStylePainter *painter = &paint;
// Stolen from QFusionStyle::drawControl
if (const QStyleOptionProgressBar *bar =
qstyleoption_cast<const QStyleOptionProgressBar *>(option)) {
QRect leftRect;
QRect rect = bar->rect;
QColor textColor = option->palette.text().color();
QColor alternateTextColor = option->palette.background().color();
painter->save();
bool vertical = false, inverted = false;
vertical = (bar->orientation == Qt::Vertical);
inverted = bar->invertedAppearance;
if (vertical)
rect = QRect(rect.left(), rect.top(), rect.height(),
rect.width()); // flip width and height
const auto totalSteps =
qMax(Q_INT64_C(1), qint64(bar->maximum) - bar->minimum);
const auto progressSteps = qint64(bar->progress) - bar->minimum;
const auto progressIndicatorPos =
progressSteps * rect.width() / totalSteps;
if (progressIndicatorPos >= 0 && progressIndicatorPos <= rect.width())
leftRect = QRect(rect.left(), rect.top(), progressIndicatorPos,
rect.height());
if (vertical)
leftRect.translate(rect.width() - progressIndicatorPos, 0);
bool flip =
(!vertical && (((bar->direction == Qt::RightToLeft) && !inverted) ||
((bar->direction == Qt::LeftToRight) && inverted)));
QRegion rightRect = rect;
rightRect = rightRect.subtracted(leftRect);
painter->setClipRegion(rightRect);
painter->setPen(flip ? alternateTextColor : textColor);
painter->drawText(rect, bar->text,
QTextOption(Qt::AlignAbsolute | Qt::AlignHCenter |
Qt::AlignVCenter));
if (!leftRect.isNull()) {
painter->setPen(flip ? textColor : alternateTextColor);
painter->setClipRect(leftRect);
painter->drawText(rect, bar->text,
QTextOption(Qt::AlignAbsolute | Qt::AlignHCenter |
Qt::AlignVCenter));
}
painter->restore();
}
}