annotate include/gui/widgets/graph.h @ 238:d2b7e974221e

CI/linux: install glib
author Paper <paper@paper.us.eu.org>
date Fri, 19 Jan 2024 11:17:42 -0500
parents 45a0967485f1
children 862d0d8619f6
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
94
2f373d48f889 *: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents: 93
diff changeset
3
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
4 /* This class is defined as a template, so that means everything gets defined here as well :) */
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
5
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include <QWidget>
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
7 #include <QDebug>
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <QSize>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include <QPaintEvent>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include <QSize>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 #include <QRect>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 #include <QPainter>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 #include <QPainterPath>
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 #include <QPen>
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
15 #include <algorithm>
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
16 #include <unordered_map>
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
18 template<typename T>
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 class Graph final : public QWidget {
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
20 public:
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
21 Graph(QWidget* parent = nullptr) : QWidget(parent) {};
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
22 void AddItem(T key, unsigned long val) { map[key] = val; update(); updateGeometry(); };
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
23 void Clear() { map.clear(); update(); updateGeometry(); };
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
25 protected:
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
26 std::unordered_map<T, unsigned long> map = {};
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
27
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
28 QSize minimumSizeHint() const override {
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
29 QFontMetrics metric(font());
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
30 /* wtf?... */
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
31 return QSize(100, metric.height() * map.size() + (2 * (map.size() - 2)));
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
32 }
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
33
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
34 /* helper functions */
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
35 inline unsigned long GetTotal() {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
36 unsigned long count = 0;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
37
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
38 for (const auto& item : map)
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
39 count += item.second;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
40
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
41 return count;
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
42 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
43
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
44 inline unsigned long GetTextWidth() {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
45 unsigned long ret = 0;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
46 QFontMetrics metric(font());
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
47
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
48 for (const auto& item : map) {
172
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
49 unsigned long width = metric.horizontalAdvance(QString::number(item.first), -1);
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
50 if (width > ret)
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
51 ret = width;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
52 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
53
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
54 return ret;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
55 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
56
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
57 void paintEvent(QPaintEvent* event) override {
172
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
58 static constexpr int HORIZ_SPACING = 5;
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
59 static constexpr int VERT_SPACING = 2;
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
60
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
61 /* these are retrieved from the QPaintEvent */
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
62 const QRect rect = event->rect();
172
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
63 const int width = event->rect().width();
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
64 const int x = rect.x();
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
65 int y = rect.y();
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66
172
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
67 /* these are calculated from font metrics and such */
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
68 const int total = GetTotal();
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
69 const int text_width = GetTextWidth() + 10;
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
70 const int each_height = QFontMetrics(font()).height();
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
71
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
72 /* now we do the actual painting */
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
73 QPainter painter(this);
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74
172
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
75 for (const auto& [key, value] : map) {
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
76 painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter, QString::number(key));
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77
172
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
78 /* only draw this if we actually have any data */
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
79 if (total) {
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
80 QPainterPath path;
172
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
81 path.addRect(x + text_width + HORIZ_SPACING, y, (static_cast<double>(value)/total) * (width - text_width - HORIZ_SPACING), each_height);
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
82 painter.fillPath(path, Qt::darkBlue);
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
83 painter.drawPath(path);
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
84 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
85
172
45a0967485f1 graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents: 171
diff changeset
86 y += each_height + VERT_SPACING;
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
88 }
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 #endif // __gui__widgets__graph_h