Mercurial > minori
comparison include/gui/widgets/graph.h @ 96:bd68e4393e6f
statistics: forward declare Graph
graph: fix missing #include (does this fix the build errors??)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 01 Nov 2023 15:16:49 -0400 |
parents | 2f373d48f889 |
children | b315f3759c56 |
comparison
equal
deleted
inserted
replaced
95:8043152ef9d4 | 96:bd68e4393e6f |
---|---|
1 #ifndef __gui__widgets__graph_h | 1 #ifndef __gui__widgets__graph_h |
2 #define __gui__widgets__graph_h | 2 #define __gui__widgets__graph_h |
3 | |
4 /* This class is defined as a template, so that means everything gets defined here as well :) */ | |
3 | 5 |
4 #include <QWidget> | 6 #include <QWidget> |
5 #include <QSize> | 7 #include <QSize> |
6 #include <QPaintEvent> | 8 #include <QPaintEvent> |
7 #include <QSize> | 9 #include <QSize> |
8 #include <QRect> | 10 #include <QRect> |
9 #include <QPainter> | 11 #include <QPainter> |
10 #include <QPainterPath> | 12 #include <QPainterPath> |
11 #include <QPen> | 13 #include <QPen> |
14 #include <unordered_map> | |
12 | 15 |
13 template <typename T> | 16 template <typename T> |
14 class Graph final : public QWidget { | 17 class Graph final : public QWidget { |
15 public: | 18 public: |
16 Graph(QWidget* parent = nullptr) : QWidget(parent) {}; | 19 Graph(QWidget* parent = nullptr) : QWidget(parent) {}; |
17 void AddItem(T key, int val) { map[key] = val; update(); updateGeometry(); }; | 20 void AddItem(T key, int val) { map[key] = val; update(); updateGeometry(); }; |
18 void Clear() { map.clear(); update(); updateGeometry(); }; | 21 void Clear() { map.clear(); update(); updateGeometry(); }; |
19 | 22 |
20 protected: | 23 protected: |
21 static constexpr int ITEM_HEIGHT = 20; | 24 static constexpr int ITEM_HEIGHT = 20; |
22 static constexpr int TEXT_WIDTH = 30; | 25 static constexpr int TEXT_WIDTH = 30; |
23 inline int GetTotal() { | 26 inline int GetTotal() { |
24 int count = 0; | 27 int count = 0; |
25 for (const auto& item : map) | 28 for (const auto& item : map) |
26 count += item.second; | 29 count += item.second; |
27 return count; | 30 return count; |
28 } | 31 } |
29 QSize minimumSizeHint() const override { return QSize(100, ITEM_HEIGHT * map.size()); }; | 32 QSize minimumSizeHint() const override { return QSize(100, ITEM_HEIGHT * map.size()); }; |
30 void paintEvent(QPaintEvent* event) override { | 33 void paintEvent(QPaintEvent* event) override { |
31 const QRect rect = event->rect(); | 34 const QRect rect = event->rect(); |
32 const int height_of_each = rect.height() / map.size(); | 35 const int height_of_each = rect.height() / map.size(); |
33 const int size = GetTotal(); | 36 const int size = GetTotal(); |
34 | 37 |
35 /* now we do the actual painting */ | 38 /* now we do the actual painting */ |
36 QPainter painter(this); | 39 QPainter painter(this); |
37 | 40 |
38 int i = 0; | 41 int i = 0; |
39 for (const auto& item : map) { | 42 for (const auto& item : map) { |
40 painter.drawText(QRect(rect.x(), rect.y() + i * ITEM_HEIGHT, TEXT_WIDTH, ITEM_HEIGHT), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); | 43 painter.drawText(QRect(rect.x(), rect.y() + i * ITEM_HEIGHT, TEXT_WIDTH, ITEM_HEIGHT), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); |
41 | 44 |
42 if (size) { | 45 if (size) { |
43 painter.save(); | 46 painter.save(); |
44 | 47 |
45 QPen pen(Qt::transparent, 0); | 48 QPen pen(Qt::transparent, 0); |
46 painter.setPen(pen); | 49 painter.setPen(pen); |
47 | 50 |
48 QPainterPath path; | 51 QPainterPath path; |
49 path.addRect(rect.x()+35, rect.y() + i * ITEM_HEIGHT, (static_cast<double>(item.second)/size)*(rect.width()-35), ITEM_HEIGHT); | 52 path.addRect(rect.x()+35, rect.y() + i * ITEM_HEIGHT, (static_cast<double>(item.second)/size)*(rect.width()-35), ITEM_HEIGHT); |
50 painter.fillPath(path, Qt::blue); | 53 painter.fillPath(path, Qt::blue); |
51 painter.drawPath(path); | 54 painter.drawPath(path); |
52 | 55 |
53 painter.restore(); | 56 painter.restore(); |
57 } | |
58 i++; | |
54 } | 59 } |
55 i++; | 60 }; |
56 } | 61 std::unordered_map<T, int> map = {}; |
57 }; | |
58 std::unordered_map<T, int> map = {}; | |
59 }; | 62 }; |
60 | 63 |
61 #endif // __gui__widgets__graph_h | 64 #endif // __gui__widgets__graph_h |