annotate include/gui/widgets/graph.h @ 118:39521c47c7a3

*: another huge megacommit, SORRY The torrents page works a lot better now Added the edit option to the anime list right click menu Vectorized currently playing files Available player and extensions are now loaded at runtime from files in (dotpath)/players.json and (dotpath)/extensions.json These paths are not permanent and will likely be moved to (dotpath)/recognition ... ... ...
author Paper <mrpapersonic@gmail.com>
date Tue, 07 Nov 2023 23:40:54 -0500
parents b315f3759c56
children 03b444cbe55f
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
94
2f373d48f889 *: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents: 93
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;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
27 inline unsigned long GetTotal() {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
28 unsigned long count = 0;
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
29 for (const auto& item : map)
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
30 count += item.second;
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
31 return count;
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
32 }
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
33 QSize minimumSizeHint() const override {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
34 QFontMetrics metric(font());
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
35 return QSize(100, metric.height() * map.size() + 2 * (map.size() - 2));
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
36 };
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
37 inline unsigned long GetTextWidth() {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
38 unsigned long ret = 0;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
39 QFontMetrics metric(font());
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
40 for (const auto& item : map) {
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
41 unsigned long width = metric.boundingRect(QString::number(item.first)).width();
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
42 if (width > ret)
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
43 ret = width;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
44 }
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
45 return ret;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
46 }
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
47 void paintEvent(QPaintEvent* event) override {
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
48 const QRect rect = event->rect();
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
49 const int height_of_each = QFontMetrics(font()).height();
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
50 const int size = GetTotal();
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
51 const int text_width = GetTextWidth();
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
52 int current_y = rect.y();
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
54 /* now we do the actual painting */
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
55 QPainter painter(this);
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
57 for (const auto& item : map) {
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
58 painter.drawText(QRect(rect.x(), current_y, text_width, height_of_each), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first));
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
60 if (size) {
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
61 painter.save();
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
63 QPen pen(Qt::transparent, 0);
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
64 painter.setPen(pen);
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
66 QPainterPath path;
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
67 path.addRect(rect.x() + text_width + SPACING, current_y, (static_cast<double>(item.second)/size)*(rect.width() - text_width - SPACING), height_of_each);
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
68 painter.fillPath(path, Qt::darkBlue);
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
69 painter.drawPath(path);
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
71 painter.restore();
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
72 }
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
73 current_y += height_of_each + 2;
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 }
96
bd68e4393e6f statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents: 94
diff changeset
75 };
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 96
diff changeset
76 std::unordered_map<T, unsigned long> map = {};
93
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 };
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78
d5efb81540b3 statistics: add graph!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 #endif // __gui__widgets__graph_h