Mercurial > minori
diff include/gui/widgets/graph.h @ 102:b315f3759c56
*: big patch
1. use a wrapper for mINI that enables case sensitivity
(personal preference)
2. rename dark_theme.cc to theme.cc and change it to be
a class
3. include the "dep" folder so we don't have stupidity in
json.h or ini.h
4. I think the graph was also tweaked a lot in this, nothing
is constexpr and size is found at runtime...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 03 Nov 2023 21:32:52 -0400 |
parents | bd68e4393e6f |
children | 03b444cbe55f |
line wrap: on
line diff
--- a/include/gui/widgets/graph.h Fri Nov 03 14:06:02 2023 -0400 +++ b/include/gui/widgets/graph.h Fri Nov 03 21:32:52 2023 -0400 @@ -4,6 +4,7 @@ /* This class is defined as a template, so that means everything gets defined here as well :) */ #include <QWidget> +#include <QDebug> #include <QSize> #include <QPaintEvent> #include <QSize> @@ -11,36 +12,50 @@ #include <QPainter> #include <QPainterPath> #include <QPen> +#include <algorithm> #include <unordered_map> template <typename T> class Graph final : public QWidget { public: Graph(QWidget* parent = nullptr) : QWidget(parent) {}; - void AddItem(T key, int val) { map[key] = val; update(); updateGeometry(); }; + void AddItem(T key, unsigned long val) { map[key] = val; update(); updateGeometry(); }; void Clear() { map.clear(); update(); updateGeometry(); }; protected: - static constexpr int ITEM_HEIGHT = 20; - static constexpr int TEXT_WIDTH = 30; - inline int GetTotal() { - int count = 0; + static constexpr int SPACING = 5; + inline unsigned long GetTotal() { + unsigned long count = 0; for (const auto& item : map) count += item.second; return count; } - QSize minimumSizeHint() const override { return QSize(100, ITEM_HEIGHT * map.size()); }; + QSize minimumSizeHint() const override { + QFontMetrics metric(font()); + return QSize(100, metric.height() * map.size() + 2 * (map.size() - 2)); + }; + inline unsigned long GetTextWidth() { + unsigned long ret = 0; + QFontMetrics metric(font()); + for (const auto& item : map) { + unsigned long width = metric.boundingRect(QString::number(item.first)).width(); + if (width > ret) + ret = width; + } + return ret; + } void paintEvent(QPaintEvent* event) override { const QRect rect = event->rect(); - const int height_of_each = rect.height() / map.size(); + const int height_of_each = QFontMetrics(font()).height(); const int size = GetTotal(); + const int text_width = GetTextWidth(); + int current_y = rect.y(); /* now we do the actual painting */ QPainter painter(this); - int i = 0; for (const auto& item : map) { - painter.drawText(QRect(rect.x(), rect.y() + i * ITEM_HEIGHT, TEXT_WIDTH, ITEM_HEIGHT), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); + painter.drawText(QRect(rect.x(), current_y, text_width, height_of_each), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); if (size) { painter.save(); @@ -49,16 +64,16 @@ painter.setPen(pen); QPainterPath path; - path.addRect(rect.x()+35, rect.y() + i * ITEM_HEIGHT, (static_cast<double>(item.second)/size)*(rect.width()-35), ITEM_HEIGHT); - painter.fillPath(path, Qt::blue); + path.addRect(rect.x() + text_width + SPACING, current_y, (static_cast<double>(item.second)/size)*(rect.width() - text_width - SPACING), height_of_each); + painter.fillPath(path, Qt::darkBlue); painter.drawPath(path); painter.restore(); } - i++; + current_y += height_of_each + 2; } }; - std::unordered_map<T, int> map = {}; + std::unordered_map<T, unsigned long> map = {}; }; #endif // __gui__widgets__graph_h \ No newline at end of file