annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #ifndef __gui__widgets__graph_h
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #define __gui__widgets__graph_h
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include <QWidget>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <QSize>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <QPaintEvent>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include <QSize>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <QRect>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <QPainter>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include <QPainterPath>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include <QPen>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 template <typename T> /* does this even work?? */
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 class Graph final : public QWidget {
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 public:
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 Graph(QWidget* parent = nullptr) : QWidget(parent) {};
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 void AddItem(T key, int val) { map[key] = val; update(); updateGeometry(); };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 void Clear() { map.clear(); update(); updateGeometry(); };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 protected:
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 static constexpr int ITEM_HEIGHT = 20;
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 static constexpr int TEXT_WIDTH = 30;
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 inline int GetTotal() {
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 int count = 0;
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 for (const auto& item : map)
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 count += item.second;
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 return count;
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 }
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 QSize minimumSizeHint() const override { return QSize(100, ITEM_HEIGHT * map.size()); };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 void paintEvent(QPaintEvent* event) override {
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 const QRect rect = event->rect();
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 const int height_of_each = rect.height() / map.size();
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 const int size = GetTotal();
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 /* now we do the actual painting */
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 QPainter painter(this);
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 int i = 0;
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 for (const auto& item : map) {
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 painter.drawText(QRect(0, i*ITEM_HEIGHT, TEXT_WIDTH, ITEM_HEIGHT), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first));
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 if (size) {
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 painter.save();
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 QPen pen(Qt::transparent, 0);
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 painter.setPen(pen);
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 QPainterPath path;
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 path.addRect(rect.x()+35, rect.y()+(i*ITEM_HEIGHT), (static_cast<double>(item.second)/size)*(rect.width()-35), ITEM_HEIGHT);
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 painter.fillPath(path, Qt::blue);
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 painter.drawPath(path);
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 painter.restore();
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 }
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 i++;
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 }
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 std::unordered_map<T, int> map = {};
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 #endif // __gui__widgets__graph_h