annotate include/gui/widgets/graph.h @ 258:862d0d8619f6

*: HUUUGE changes animia has been renamed to animone, so instead of thinking of a health condition, you think of a beautiful flower :) I've also edited some of the code for animone, but I have no idea if it even works or not because I don't have a mac or windows machine lying around. whoops! ... anyway, all of the changes divergent from Anisthesia are now licensed under BSD. it's possible that I could even rewrite most of the code to where I don't even have to keep the MIT license, but that's thinking too far into the future I've been slacking off on implementing the anime seasons page, mostly out of laziness. I think I'd have to create another db file specifically for the seasons anyway, this code is being pushed *primarily* because the hard drive it's on is failing! yay :)
author Paper <paper@paper.us.eu.org>
date Mon, 01 Apr 2024 02:43:44 -0400
parents 45a0967485f1
children 0362f3c4534c
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
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:
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
33 std::unordered_map<T, unsigned long> map = {};
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
35 QSize minimumSizeHint() const override {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
36 QFontMetrics metric(font());
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
37 /* wtf?... */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
38 return QSize(100, (metric.height() * map.size()) + (2 * map.size()));
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
39 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
40
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
41 /* helper functions */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
42 inline unsigned long GetTotal() {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
43 unsigned long count = 0;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
44
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
45 for (const auto& item : map)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
46 count += item.second;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
47
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
48 return count;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
49 }
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 inline unsigned long GetTextWidth() {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
52 unsigned long ret = 0;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
53 QFontMetrics metric(font());
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
54
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
55 for (const auto& item : map) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
56 unsigned long width = metric.horizontalAdvance(QString::number(item.first), -1);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
57 if (width > ret)
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
58 ret = width;
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
59 }
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
60
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
61 return ret;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
62 }
171
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 void paintEvent(QPaintEvent* event) override {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
65 static constexpr int HORIZ_SPACING = 5;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
66 static constexpr int VERT_SPACING = 2;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
67
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
68 /* these are retrieved from the QPaintEvent */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
69 const QRect rect = event->rect();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
70 const int width = event->rect().width();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
71 const int x = rect.x();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
72 int y = rect.y();
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
73
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
74 /* these are calculated from font metrics and such */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
75 const int total = GetTotal();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
76 const int text_width = GetTextWidth();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
77 const int each_height = QFontMetrics(font()).height();
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
78
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
79 /* now we do the actual painting */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
80 QPainter painter(this);
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
81
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
82 for (const auto& [key, value] : map) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
83 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
84 QString::number(key));
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
85
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
86 /* only draw this if we actually have any data */
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
87 if (total) {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
88 QPainterPath path;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
89 path.addRect(x + text_width + HORIZ_SPACING, y,
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
90 (static_cast<double>(value) / total) * (width - text_width - HORIZ_SPACING), each_height);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
91 painter.fillPath(path, Qt::darkBlue);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
92 painter.drawPath(path);
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
93 }
171
03b444cbe55f graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
94
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
95 y += each_height + VERT_SPACING;
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
96 }
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 172
diff changeset
97 }
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
98 };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 #endif // __gui__widgets__graph_h