Mercurial > minori
diff include/gui/widgets/graph.h @ 172:45a0967485f1
graph, statistics: make my code a little less messy
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 28 Nov 2023 13:22:35 -0500 |
parents | 03b444cbe55f |
children | 862d0d8619f6 |
line wrap: on
line diff
--- a/include/gui/widgets/graph.h Mon Nov 27 13:51:27 2023 -0500 +++ b/include/gui/widgets/graph.h Tue Nov 28 13:22:35 2023 -0500 @@ -23,8 +23,6 @@ void Clear() { map.clear(); update(); updateGeometry(); }; protected: - static constexpr int SPACING = 5; - std::unordered_map<T, unsigned long> map = {}; QSize minimumSizeHint() const override { @@ -48,7 +46,7 @@ QFontMetrics metric(font()); for (const auto& item : map) { - unsigned long width = metric.boundingRect(QString::number(item.first)).width(); + unsigned long width = metric.horizontalAdvance(QString::number(item.first), -1); if (width > ret) ret = width; } @@ -57,34 +55,35 @@ } void paintEvent(QPaintEvent* event) override { + static constexpr int HORIZ_SPACING = 5; + static constexpr int VERT_SPACING = 2; + + /* these are retrieved from the QPaintEvent */ const QRect rect = event->rect(); - const int height_of_each = QFontMetrics(font()).height(); - const int size = GetTotal(); - const int text_width = GetTextWidth(); + const int width = event->rect().width(); const int x = rect.x(); int y = rect.y(); + /* these are calculated from font metrics and such */ + const int total = GetTotal(); + const int text_width = GetTextWidth() + 10; + const int each_height = QFontMetrics(font()).height(); + /* now we do the actual painting */ QPainter painter(this); - for (const auto& item : map) { - painter.drawText(QRect(x + 2, y, text_width - 2, height_of_each), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); - - if (size) { - painter.save(); + for (const auto& [key, value] : map) { + painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter, QString::number(key)); - QPen pen(Qt::transparent, 0); - painter.setPen(pen); - + /* only draw this if we actually have any data */ + if (total) { QPainterPath path; - path.addRect(x + text_width + 2 + SPACING, y, (static_cast<double>(item.second)/size) * (rect.width() - text_width + 2 - SPACING), height_of_each); + path.addRect(x + text_width + HORIZ_SPACING, y, (static_cast<double>(value)/total) * (width - text_width - HORIZ_SPACING), each_height); painter.fillPath(path, Qt::darkBlue); painter.drawPath(path); - - painter.restore(); } - y += height_of_each + 2; + y += each_height + VERT_SPACING; } } };