Mercurial > minori
diff include/gui/widgets/graph.h @ 96:bd68e4393e6f
statistics: forward declare Graph
graph: fix missing #include (does this fix the build errors??)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 01 Nov 2023 15:16:49 -0400 |
parents | 2f373d48f889 |
children | b315f3759c56 |
line wrap: on
line diff
--- a/include/gui/widgets/graph.h Wed Nov 01 14:39:43 2023 -0400 +++ b/include/gui/widgets/graph.h Wed Nov 01 15:16:49 2023 -0400 @@ -1,6 +1,8 @@ #ifndef __gui__widgets__graph_h #define __gui__widgets__graph_h +/* This class is defined as a template, so that means everything gets defined here as well :) */ + #include <QWidget> #include <QSize> #include <QPaintEvent> @@ -9,53 +11,54 @@ #include <QPainter> #include <QPainterPath> #include <QPen> +#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 Clear() { map.clear(); update(); updateGeometry(); }; + public: + Graph(QWidget* parent = nullptr) : QWidget(parent) {}; + void AddItem(T key, int 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; - for (const auto& item : map) - count += item.second; - return count; - } - QSize minimumSizeHint() const override { return QSize(100, ITEM_HEIGHT * map.size()); }; - void paintEvent(QPaintEvent* event) override { - const QRect rect = event->rect(); - const int height_of_each = rect.height() / map.size(); - const int size = GetTotal(); + protected: + static constexpr int ITEM_HEIGHT = 20; + static constexpr int TEXT_WIDTH = 30; + inline int GetTotal() { + int count = 0; + for (const auto& item : map) + count += item.second; + return count; + } + QSize minimumSizeHint() const override { return QSize(100, ITEM_HEIGHT * map.size()); }; + void paintEvent(QPaintEvent* event) override { + const QRect rect = event->rect(); + const int height_of_each = rect.height() / map.size(); + const int size = GetTotal(); - /* now we do the actual painting */ - QPainter painter(this); + /* 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)); + 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)); - if (size) { - painter.save(); + if (size) { + painter.save(); - QPen pen(Qt::transparent, 0); - painter.setPen(pen); + QPen pen(Qt::transparent, 0); + 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); - painter.drawPath(path); + 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); + painter.drawPath(path); - painter.restore(); + painter.restore(); + } + i++; } - i++; - } - }; - std::unordered_map<T, int> map = {}; + }; + std::unordered_map<T, int> map = {}; }; #endif // __gui__widgets__graph_h \ No newline at end of file