Mercurial > minori
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 |
rev | line source |
---|---|
93 | 1 #ifndef __gui__widgets__graph_h |
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 | 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: | |
33 std::unordered_map<T, unsigned long> map = {}; | |
93 | 34 |
258 | 35 QSize minimumSizeHint() const override { |
36 QFontMetrics metric(font()); | |
37 /* wtf?... */ | |
38 return QSize(100, (metric.height() * map.size()) + (2 * map.size())); | |
39 } | |
40 | |
41 /* helper functions */ | |
42 inline unsigned long GetTotal() { | |
43 unsigned long count = 0; | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
44 |
258 | 45 for (const auto& item : map) |
46 count += item.second; | |
47 | |
48 return count; | |
49 } | |
50 | |
51 inline unsigned long GetTextWidth() { | |
52 unsigned long ret = 0; | |
53 QFontMetrics metric(font()); | |
54 | |
55 for (const auto& item : map) { | |
56 unsigned long width = metric.horizontalAdvance(QString::number(item.first), -1); | |
57 if (width > ret) | |
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 | 61 return ret; |
62 } | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
63 |
258 | 64 void paintEvent(QPaintEvent* event) override { |
65 static constexpr int HORIZ_SPACING = 5; | |
66 static constexpr int VERT_SPACING = 2; | |
67 | |
68 /* these are retrieved from the QPaintEvent */ | |
69 const QRect rect = event->rect(); | |
70 const int width = event->rect().width(); | |
71 const int x = rect.x(); | |
72 int y = rect.y(); | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
73 |
258 | 74 /* these are calculated from font metrics and such */ |
75 const int total = GetTotal(); | |
76 const int text_width = GetTextWidth(); | |
77 const int each_height = QFontMetrics(font()).height(); | |
78 | |
79 /* now we do the actual painting */ | |
80 QPainter painter(this); | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
81 |
258 | 82 for (const auto& [key, value] : map) { |
83 painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter | Qt::AlignRight, | |
84 QString::number(key)); | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
85 |
258 | 86 /* only draw this if we actually have any data */ |
87 if (total) { | |
88 QPainterPath path; | |
89 path.addRect(x + text_width + HORIZ_SPACING, y, | |
90 (static_cast<double>(value) / total) * (width - text_width - HORIZ_SPACING), each_height); | |
91 painter.fillPath(path, Qt::darkBlue); | |
92 painter.drawPath(path); | |
102 | 93 } |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
94 |
258 | 95 y += each_height + VERT_SPACING; |
102 | 96 } |
258 | 97 } |
93 | 98 }; |
99 | |
100 #endif // __gui__widgets__graph_h |