From d24f813f3f2a05c112e803e4256b53535895fc98 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 14 Jul 2021 11:49:10 +1200 Subject: Initial mirror commit --- src/study/CardsStatusBar.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/study/CardsStatusBar.cpp (limited to 'src/study/CardsStatusBar.cpp') diff --git a/src/study/CardsStatusBar.cpp b/src/study/CardsStatusBar.cpp new file mode 100644 index 0000000..e29f9f8 --- /dev/null +++ b/src/study/CardsStatusBar.cpp @@ -0,0 +1,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; +} -- cgit