Mercurial > minori
annotate include/gui/widgets/graph.h @ 362:433eeaca3baf
CI/linux: revert change to linuxdeployqt; add qt plugin
turns out I'm stupid and linuxdeployqt is insanely broken
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 15 Jul 2024 01:28:57 -0400 |
parents | f31305b9f60a |
children | f81bed4e04ac |
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 | 6 #include <QDebug> |
93 | 7 #include <QPaintEvent> |
8 #include <QPainter> | |
9 #include <QPainterPath> | |
10 #include <QPen> | |
258 | 11 #include <QRect> |
12 #include <QSize> | |
13 #include <QWidget> | |
102 | 14 #include <algorithm> |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
15 #include <unordered_map> |
93 | 16 |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
17 template<typename T> |
93 | 18 class Graph final : public QWidget { |
258 | 19 public: |
20 Graph(QWidget* parent = nullptr) : QWidget(parent){}; | |
21 void AddItem(T key, unsigned long val) { | |
22 map[key] = val; | |
23 update(); | |
24 updateGeometry(); | |
25 }; | |
26 void Clear() { | |
27 map.clear(); | |
28 update(); | |
29 updateGeometry(); | |
30 }; | |
31 | |
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 | 36 std::unordered_map<T, unsigned long> map = {}; |
93 | 37 |
258 | 38 QSize minimumSizeHint() const override { |
39 QFontMetrics metric(font()); | |
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 | 42 } |
43 | |
44 /* helper functions */ | |
45 inline unsigned long GetTotal() { | |
46 unsigned long count = 0; | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
47 |
258 | 48 for (const auto& item : map) |
49 count += item.second; | |
50 | |
51 return count; | |
52 } | |
53 | |
54 inline unsigned long GetTextWidth() { | |
55 unsigned long ret = 0; | |
56 QFontMetrics metric(font()); | |
57 | |
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 | 60 if (width > ret) |
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 | 64 return ret; |
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 | 80 void paintEvent(QPaintEvent* event) override { |
81 /* these are retrieved from the QPaintEvent */ | |
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 | 84 const int x = rect.x(); |
85 int y = rect.y(); | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
86 |
258 | 87 /* these are calculated from font metrics and such */ |
88 const int total = GetTotal(); | |
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 | 91 const int each_height = QFontMetrics(font()).height(); |
92 | |
93 /* now we do the actual painting */ | |
94 QPainter painter(this); | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
95 |
258 | 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 | 99 painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter | Qt::AlignRight, |
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 | 104 /* only draw this if we actually have any data */ |
105 if (total) { | |
259
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
106 painter.save(); |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
107 |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
108 QPen pen(painter.pen()); |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
109 pen.setStyle(Qt::NoPen); |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
110 painter.setPen(pen); |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
111 |
258 | 112 QPainterPath path; |
259
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
113 path.addRect(x + offset, y, |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
114 (static_cast<double>(value) / total) * (width - offset - HORIZ_SPACING - value_width), each_height); |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
115 painter.fillPath(path, Qt::darkGreen); |
258 | 116 painter.drawPath(path); |
259
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
117 |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
118 offset += (static_cast<double>(value) / total) * (width - offset - HORIZ_SPACING - value_width); |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
119 |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
120 painter.restore(); |
102 | 121 } |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
122 |
259
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
123 offset += HORIZ_SPACING; |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
124 |
0362f3c4534c
widgets/graph: improve drawing code
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
125 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
|
126 |
258 | 127 y += each_height + VERT_SPACING; |
102 | 128 } |
258 | 129 } |
93 | 130 }; |
131 | |
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
259
diff
changeset
|
132 #endif // MINORI_GUI_WIDGETS_GRAPH_H_ |