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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#include "CardsStatusBar.h"
const QStringList CardsStatusBar::Colors = {"green", "#fffd7c", "white", "#bd8d71"};
CardsStatusBar::CardsStatusBar(QWidget* aParent) :
QWidget(aParent)
{
setMinimumHeight(10);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
CardsStatusBar::~CardsStatusBar()
{
delete painter;
}
void CardsStatusBar::paintEvent(QPaintEvent* aEvent)
{
QWidget::paintEvent( aEvent );
if(values.isEmpty())
return;
max = getMax();
painter = new QPainter(this);
painter->setRenderHint(QPainter::Antialiasing);
painter->save();
QPainterPath path;
path.addRoundedRect( rect(), Radius, Radius);
painter->setClipPath( path );
barRect = rect().adjusted(0, 0, 1, 0);
sectionLeft = rect().left();
for(int i = 0; i < values.size(); i++)
drawSection(i);
painter->restore();
painter->setPen(QPen("#7c7c7c"));
painter->drawRoundedRect(rect(), Radius, Radius);
}
int CardsStatusBar::getMax() const
{
int max = 0;
foreach(int v, values)
max += v;
if(max == 0)
max = 1;
return max;
}
void CardsStatusBar::drawSection(int index)
{
QRectF sectionRect = getSectionRect(index);
painter->setPen( Qt::NoPen );
painter->setBrush(getSectionGradient(index));
painter->drawRect(sectionRect);
painter->setPen(QPen(QBrush("#a7a7a7"), 0.5));
painter->drawLine(sectionRect.topRight(), sectionRect.bottomRight());
sectionLeft += sectionRect.width();
}
QLinearGradient CardsStatusBar::getSectionGradient(int index)
{
QLinearGradient grad(barRect.topLeft(), barRect.bottomLeft());
grad.setColorAt( 0, Qt::white);
grad.setColorAt( 0.9, QColor(Colors[index]) );
return grad;
}
QRectF CardsStatusBar::getSectionRect(int index)
{
QRectF sectionRect = barRect;
sectionRect.moveLeft(sectionLeft);
qreal sectionWidth = barRect.width() * values[index] / max;
sectionRect.setWidth(sectionWidth);
return sectionRect;
}
|