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