comparison include/gui/widgets/graph.h @ 93:d5efb81540b3

statistics: add graph! also now we have a VERY VERY simple graph widget that probably sucks and doesn't work half the time :)
author Paper <mrpapersonic@gmail.com>
date Wed, 01 Nov 2023 13:52:34 -0400
parents
children 2f373d48f889
comparison
equal deleted inserted replaced
92:2c27582a177c 93:d5efb81540b3
1 #ifndef __gui__widgets__graph_h
2 #define __gui__widgets__graph_h
3 #include <QWidget>
4 #include <QSize>
5 #include <QPaintEvent>
6 #include <QSize>
7 #include <QRect>
8 #include <QPainter>
9 #include <QPainterPath>
10 #include <QPen>
11
12 template <typename T> /* does this even work?? */
13 class Graph final : public QWidget {
14 public:
15 Graph(QWidget* parent = nullptr) : QWidget(parent) {};
16 void AddItem(T key, int val) { map[key] = val; update(); updateGeometry(); };
17 void Clear() { map.clear(); update(); updateGeometry(); };
18
19 protected:
20 static constexpr int ITEM_HEIGHT = 20;
21 static constexpr int TEXT_WIDTH = 30;
22 inline int GetTotal() {
23 int count = 0;
24 for (const auto& item : map)
25 count += item.second;
26 return count;
27 }
28 QSize minimumSizeHint() const override { return QSize(100, ITEM_HEIGHT * map.size()); };
29 void paintEvent(QPaintEvent* event) override {
30 const QRect rect = event->rect();
31 const int height_of_each = rect.height() / map.size();
32 const int size = GetTotal();
33
34 /* now we do the actual painting */
35 QPainter painter(this);
36
37 int i = 0;
38 for (const auto& item : map) {
39 painter.drawText(QRect(0, i*ITEM_HEIGHT, TEXT_WIDTH, ITEM_HEIGHT), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first));
40
41 if (size) {
42 painter.save();
43
44 QPen pen(Qt::transparent, 0);
45 painter.setPen(pen);
46
47 QPainterPath path;
48 path.addRect(rect.x()+35, rect.y()+(i*ITEM_HEIGHT), (static_cast<double>(item.second)/size)*(rect.width()-35), ITEM_HEIGHT);
49 painter.fillPath(path, Qt::blue);
50 painter.drawPath(path);
51
52 painter.restore();
53 }
54 i++;
55 }
56 };
57 std::unordered_map<T, int> map = {};
58 };
59
60 #endif // __gui__widgets__graph_h