Mercurial > minori
annotate include/gui/widgets/graph.h @ 94:2f373d48f889
*: etc changes to graph stuff
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 01 Nov 2023 14:30:26 -0400 |
parents | d5efb81540b3 |
children | bd68e4393e6f |
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 |
93 | 4 #include <QWidget> |
5 #include <QSize> | |
6 #include <QPaintEvent> | |
7 #include <QSize> | |
8 #include <QRect> | |
9 #include <QPainter> | |
10 #include <QPainterPath> | |
11 #include <QPen> | |
12 | |
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
13 template <typename T> |
93 | 14 class Graph final : public QWidget { |
15 public: | |
16 Graph(QWidget* parent = nullptr) : QWidget(parent) {}; | |
17 void AddItem(T key, int val) { map[key] = val; update(); updateGeometry(); }; | |
18 void Clear() { map.clear(); update(); updateGeometry(); }; | |
19 | |
20 protected: | |
21 static constexpr int ITEM_HEIGHT = 20; | |
22 static constexpr int TEXT_WIDTH = 30; | |
23 inline int GetTotal() { | |
24 int count = 0; | |
25 for (const auto& item : map) | |
26 count += item.second; | |
27 return count; | |
28 } | |
29 QSize minimumSizeHint() const override { return QSize(100, ITEM_HEIGHT * map.size()); }; | |
30 void paintEvent(QPaintEvent* event) override { | |
31 const QRect rect = event->rect(); | |
32 const int height_of_each = rect.height() / map.size(); | |
33 const int size = GetTotal(); | |
34 | |
35 /* now we do the actual painting */ | |
36 QPainter painter(this); | |
37 | |
38 int i = 0; | |
39 for (const auto& item : map) { | |
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
40 painter.drawText(QRect(rect.x(), rect.y() + i * ITEM_HEIGHT, TEXT_WIDTH, ITEM_HEIGHT), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); |
93 | 41 |
42 if (size) { | |
43 painter.save(); | |
44 | |
45 QPen pen(Qt::transparent, 0); | |
46 painter.setPen(pen); | |
47 | |
48 QPainterPath path; | |
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
49 path.addRect(rect.x()+35, rect.y() + i * ITEM_HEIGHT, (static_cast<double>(item.second)/size)*(rect.width()-35), ITEM_HEIGHT); |
93 | 50 painter.fillPath(path, Qt::blue); |
51 painter.drawPath(path); | |
52 | |
53 painter.restore(); | |
54 } | |
55 i++; | |
56 } | |
57 }; | |
58 std::unordered_map<T, int> map = {}; | |
59 }; | |
60 | |
61 #endif // __gui__widgets__graph_h |