Mercurial > minori
annotate include/gui/widgets/graph.h @ 135:0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
old compilers will croak compiling this, but it's not like we
*really* need to support them (they probably croak compiling
Qt as well)
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Thu, 09 Nov 2023 18:01:56 -0500 |
| parents | b315f3759c56 |
| children | 03b444cbe55f |
| 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 |
|
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
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; |
| 27 inline unsigned long GetTotal() { | |
| 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 | 33 QSize minimumSizeHint() const override { |
| 34 QFontMetrics metric(font()); | |
| 35 return QSize(100, metric.height() * map.size() + 2 * (map.size() - 2)); | |
| 36 }; | |
| 37 inline unsigned long GetTextWidth() { | |
| 38 unsigned long ret = 0; | |
| 39 QFontMetrics metric(font()); | |
| 40 for (const auto& item : map) { | |
| 41 unsigned long width = metric.boundingRect(QString::number(item.first)).width(); | |
| 42 if (width > ret) | |
| 43 ret = width; | |
| 44 } | |
| 45 return ret; | |
| 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 | 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 | 51 const int text_width = GetTextWidth(); |
| 52 int current_y = rect.y(); | |
| 93 | 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 | 56 |
|
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
57 for (const auto& item : map) { |
| 102 | 58 painter.drawText(QRect(rect.x(), current_y, text_width, height_of_each), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); |
| 93 | 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 | 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 | 65 |
|
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
66 QPainterPath path; |
| 102 | 67 path.addRect(rect.x() + text_width + SPACING, current_y, (static_cast<double>(item.second)/size)*(rect.width() - text_width - SPACING), height_of_each); |
| 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 | 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 | 73 current_y += height_of_each + 2; |
| 93 | 74 } |
|
96
bd68e4393e6f
statistics: forward declare Graph
Paper <mrpapersonic@gmail.com>
parents:
94
diff
changeset
|
75 }; |
| 102 | 76 std::unordered_map<T, unsigned long> map = {}; |
| 93 | 77 }; |
| 78 | |
| 79 #endif // __gui__widgets__graph_h |
