annotate include/gui/widgets/graph.h @ 171:03b444cbe55f

graph: improve? drawing the text dark stylesheet: add borders on QMenu, taking inspiration from MPC-HC media: fix warning about missing enums
author Paper <mrpapersonic@gmail.com>
date Mon, 27 Nov 2023 13:51:27 -0500
parents b315f3759c56
children 45a0967485f1
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:
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
26 static constexpr int SPACING = 5;
171
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 std::unordered_map<T, unsigned long> map = {};
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
29
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
30 QSize minimumSizeHint() const override {
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
31 QFontMetrics metric(font());
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
32 /* wtf?... */
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
33 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
34 }
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
35
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
36 /* helper functions */
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
37 inline unsigned long GetTotal() {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
38 unsigned long count = 0;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
39
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
40 for (const auto& item : map)
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
41 count += item.second;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
42
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
43 return count;
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
44 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
45
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
46 inline unsigned long GetTextWidth() {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
47 unsigned long ret = 0;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
48 QFontMetrics metric(font());
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
49
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
50 for (const auto& item : map) {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
51 unsigned long width = metric.boundingRect(QString::number(item.first)).width();
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
52 if (width > ret)
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
53 ret = width;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
54 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
55
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
56 return ret;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
57 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
58
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
59 void paintEvent(QPaintEvent* event) override {
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
60 const QRect rect = event->rect();
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
61 const int height_of_each = QFontMetrics(font()).height();
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
62 const int size = GetTotal();
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
63 const int text_width = GetTextWidth();
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
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
67 /* now we do the actual painting */
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
68 QPainter painter(this);
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
70 for (const auto& item : map) {
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
71 painter.drawText(QRect(x + 2, y, text_width - 2, height_of_each), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first));
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
73 if (size) {
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
74 painter.save();
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
76 QPen pen(Qt::transparent, 0);
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
77 painter.setPen(pen);
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
79 QPainterPath path;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
80 path.addRect(x + text_width + 2 + SPACING, y, (static_cast<double>(item.second)/size) * (rect.width() - text_width + 2 - SPACING), height_of_each);
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
81 painter.fillPath(path, Qt::darkBlue);
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
82 painter.drawPath(path);
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
84 painter.restore();
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
85 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
86
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
87 y += height_of_each + 2;
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
89 }
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 #endif // __gui__widgets__graph_h