Mercurial > minori
annotate include/gui/widgets/graph.h @ 171:03b444cbe55f
graph: improve? drawing the text
dark stylesheet: add borders on QMenu, taking inspiration from MPC-HC
media: fix warning about missing enums
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 27 Nov 2023 13:51:27 -0500 |
parents | b315f3759c56 |
children | 45a0967485f1 |
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 |
93 | 6 #include <QWidget> |
102 | 7 #include <QDebug> |
93 | 8 #include <QSize> |
9 #include <QPaintEvent> | |
10 #include <QSize> | |
11 #include <QRect> | |
12 #include <QPainter> | |
13 #include <QPainterPath> | |
14 #include <QPen> | |
102 | 15 #include <algorithm> |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
16 #include <unordered_map> |
93 | 17 |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
18 template<typename T> |
93 | 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 | 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 | 24 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
25 protected: |
102 | 26 static constexpr int SPACING = 5; |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
27 |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
28 std::unordered_map<T, unsigned long> map = {}; |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
29 |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
30 QSize minimumSizeHint() const override { |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
31 QFontMetrics metric(font()); |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
32 /* wtf?... */ |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
33 return QSize(100, metric.height() * map.size() + (2 * (map.size() - 2))); |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
34 } |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
35 |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
36 /* helper functions */ |
102 | 37 inline unsigned long GetTotal() { |
38 unsigned long count = 0; | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
39 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
40 for (const auto& item : map) |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
41 count += item.second; |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
42 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
43 return count; |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
44 } |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
45 |
102 | 46 inline unsigned long GetTextWidth() { |
47 unsigned long ret = 0; | |
48 QFontMetrics metric(font()); | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
49 |
102 | 50 for (const auto& item : map) { |
51 unsigned long width = metric.boundingRect(QString::number(item.first)).width(); | |
52 if (width > ret) | |
53 ret = width; | |
54 } | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
55 |
102 | 56 return ret; |
57 } | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
58 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
59 void paintEvent(QPaintEvent* event) override { |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
60 const QRect rect = event->rect(); |
102 | 61 const int height_of_each = QFontMetrics(font()).height(); |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
62 const int size = GetTotal(); |
102 | 63 const int text_width = GetTextWidth(); |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
64 const int x = rect.x(); |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
65 int y = rect.y(); |
93 | 66 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
67 /* now we do the actual painting */ |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
68 QPainter painter(this); |
93 | 69 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
70 for (const auto& item : map) { |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
71 painter.drawText(QRect(x + 2, y, text_width - 2, height_of_each), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); |
93 | 72 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
73 if (size) { |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
74 painter.save(); |
93 | 75 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
76 QPen pen(Qt::transparent, 0); |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
77 painter.setPen(pen); |
93 | 78 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
79 QPainterPath path; |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
80 path.addRect(x + text_width + 2 + SPACING, y, (static_cast<double>(item.second)/size) * (rect.width() - text_width + 2 - SPACING), height_of_each); |
102 | 81 painter.fillPath(path, Qt::darkBlue); |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
82 painter.drawPath(path); |
93 | 83 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
84 painter.restore(); |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
85 } |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
86 |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
87 y += height_of_each + 2; |
93 | 88 } |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
89 } |
93 | 90 }; |
91 | |
92 #endif // __gui__widgets__graph_h |