Mercurial > minori
annotate include/gui/widgets/graph.h @ 187:9613d72b097e
*: multiple performance improvements
like marking `static const` when it makes sense...
date: change old stupid heap-based method to a structure which should
make copying the thing actually make a copy.
also many performance-based changes, like removing the std::tie
dependency and forward-declaring nlohmann json
*: replace every instance of QString::fromUtf8 to Strings::ToQString.
the main difference is that our function will always convert exactly
what is in the string, while some other times it would only convert
up to the nearest NUL byte
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 06 Dec 2023 13:43:54 -0500 |
parents | 45a0967485f1 |
children | 862d0d8619f6 |
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: |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
26 std::unordered_map<T, unsigned long> map = {}; |
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 QSize minimumSizeHint() const override { |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
29 QFontMetrics metric(font()); |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
30 /* wtf?... */ |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
31 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
|
32 } |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
33 |
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
34 /* helper functions */ |
102 | 35 inline unsigned long GetTotal() { |
36 unsigned long count = 0; | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
37 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
38 for (const auto& item : map) |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
39 count += item.second; |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
40 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
41 return count; |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
42 } |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
43 |
102 | 44 inline unsigned long GetTextWidth() { |
45 unsigned long ret = 0; | |
46 QFontMetrics metric(font()); | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
47 |
102 | 48 for (const auto& item : map) { |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
49 unsigned long width = metric.horizontalAdvance(QString::number(item.first), -1); |
102 | 50 if (width > ret) |
51 ret = width; | |
52 } | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
53 |
102 | 54 return ret; |
55 } | |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
56 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
57 void paintEvent(QPaintEvent* event) override { |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
58 static constexpr int HORIZ_SPACING = 5; |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
59 static constexpr int VERT_SPACING = 2; |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
60 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
61 /* these are retrieved from the QPaintEvent */ |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
62 const QRect rect = event->rect(); |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
63 const int width = event->rect().width(); |
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 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
67 /* these are calculated from font metrics and such */ |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
68 const int total = GetTotal(); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
69 const int text_width = GetTextWidth() + 10; |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
70 const int each_height = QFontMetrics(font()).height(); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
71 |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
72 /* now we do the actual painting */ |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
73 QPainter painter(this); |
93 | 74 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
75 for (const auto& [key, value] : map) { |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
76 painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter, QString::number(key)); |
93 | 77 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
78 /* only draw this if we actually have any data */ |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
79 if (total) { |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
80 QPainterPath path; |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
81 path.addRect(x + text_width + HORIZ_SPACING, y, (static_cast<double>(value)/total) * (width - text_width - HORIZ_SPACING), each_height); |
102 | 82 painter.fillPath(path, Qt::darkBlue); |
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
83 painter.drawPath(path); |
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
84 } |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
85 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
171
diff
changeset
|
86 y += each_height + VERT_SPACING; |
93 | 87 } |
171
03b444cbe55f
graph: improve? drawing the text
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
88 } |
93 | 89 }; |
90 | |
91 #endif // __gui__widgets__graph_h |