annotate include/gui/widgets/graph.h @ 367:8d45d892be88 default tip

*: instead of pugixml, use Qt XML features this means we have one extra Qt dependency though...
author Paper <paper@tflc.us>
date Sun, 17 Nov 2024 22:55:47 -0500
parents f81bed4e04ac
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
261
3ec7804abf17 include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents: 259
diff changeset
1 #ifndef MINORI_GUI_WIDGETS_GRAPH_H_
3ec7804abf17 include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents: 259
diff changeset
2 #define MINORI_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
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
6 #include <QDebug>
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <QPaintEvent>
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>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
11 #include <QRect>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
12 #include <QSize>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
13 #include <QWidget>
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
14 #include <algorithm>
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
15 #include <unordered_map>
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
17 template<typename T>
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 class Graph final : public QWidget {
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
19 public:
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
20 Graph(QWidget* parent = nullptr) : QWidget(parent){};
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
21 void AddItem(T key, unsigned long val) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
22 map[key] = val;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
23 update();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
24 updateGeometry();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
25 };
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
26 void Clear() {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
27 map.clear();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
28 update();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
29 updateGeometry();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
30 };
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
31
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
32 protected:
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
33 static constexpr int HORIZ_SPACING = 5;
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
34 static constexpr int VERT_SPACING = 3;
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
35
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
36 std::unordered_map<T, unsigned long> map = {};
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
38 QSize minimumSizeHint() const override {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
39 QFontMetrics metric(font());
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
40 /* wtf?... */
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
41 return QSize(100, (metric.height() * map.size()) + (VERT_SPACING * map.size()));
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
42 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
43
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
44 /* helper functions */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
45 inline unsigned long GetTotal() {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
46 unsigned long count = 0;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
47
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
48 for (const auto& item : map)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
49 count += item.second;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
50
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
51 return count;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
52 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
53
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
54 inline unsigned long GetTextWidth() {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
55 unsigned long ret = 0;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
56 QFontMetrics metric(font());
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
57
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
58 for (const auto& item : map) {
273
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
59 unsigned long width = metric.boundingRect(QString::number(item.first)).width();
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
60 if (width > ret)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
61 ret = width;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
62 }
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
63
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
64 return ret;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
65 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
66
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
67 inline unsigned long GetValueWidth() {
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
68 unsigned long ret = 0;
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
69 QFontMetrics metric(font());
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
70
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
71 for (const auto& item : map) {
273
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
72 unsigned long width = metric.boundingRect(QString::number(item.second)).width();
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
73 if (width > ret)
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
74 ret = width;
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
75 }
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
76
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
77 return ret;
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
78 }
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
79
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
80 void paintEvent(QPaintEvent* event) override {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
81 /* these are retrieved from the QPaintEvent */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
82 const QRect rect = event->rect();
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
83 const int width = rect.width();
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
84 const int x = rect.x();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
85 int y = rect.y();
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
86
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
87 /* these are calculated from font metrics and such */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
88 const int total = GetTotal();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
89 const int text_width = GetTextWidth();
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
90 const int value_width = GetValueWidth();
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
91 const int each_height = QFontMetrics(font()).height();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
92
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
93 /* now we do the actual painting */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
94 QPainter painter(this);
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
95
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
96 for (const auto& [key, value] : map) {
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
97 int offset = 0;
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
98
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
99 painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter | Qt::AlignRight,
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
100 QString::number(key));
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
101
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
102 offset += text_width + HORIZ_SPACING;
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
103
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
104 /* only draw this if we actually have any data */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
105 if (total) {
365
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
106 const int rect_width = (static_cast<double>(value) / total) * (width - offset - HORIZ_SPACING - value_width);
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
107
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
108 painter.save();
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
109
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
110 QPen pen(painter.pen());
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
111 pen.setStyle(Qt::NoPen);
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
112 painter.setPen(pen);
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
113
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
114 QPainterPath path;
365
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
115 path.addRect(x + offset, y, rect_width, each_height);
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
116 painter.fillPath(path, Qt::darkGreen);
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
117 painter.drawPath(path);
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
118
365
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
119 offset += rect_width;
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
120
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
121 painter.restore();
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
122 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
123
259
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
124 offset += HORIZ_SPACING;
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
125
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
126 painter.drawText(QRect(x + offset, y, value_width, each_height), Qt::AlignVCenter | Qt::AlignLeft, QString::number(value));
0362f3c4534c widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
127
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
128 y += each_height + VERT_SPACING;
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
129 }
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
130 }
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
131 };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
132
261
3ec7804abf17 include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents: 259
diff changeset
133 #endif // MINORI_GUI_WIDGETS_GRAPH_H_