Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 93:d5efb81540b3 | 94:2f373d48f889 |
|---|---|
| 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 | |
| 3 #include <QWidget> | 4 #include <QWidget> |
| 4 #include <QSize> | 5 #include <QSize> |
| 5 #include <QPaintEvent> | 6 #include <QPaintEvent> |
| 6 #include <QSize> | 7 #include <QSize> |
| 7 #include <QRect> | 8 #include <QRect> |
| 8 #include <QPainter> | 9 #include <QPainter> |
| 9 #include <QPainterPath> | 10 #include <QPainterPath> |
| 10 #include <QPen> | 11 #include <QPen> |
| 11 | 12 |
| 12 template <typename T> /* does this even work?? */ | 13 template <typename T> |
| 13 class Graph final : public QWidget { | 14 class Graph final : public QWidget { |
| 14 public: | 15 public: |
| 15 Graph(QWidget* parent = nullptr) : QWidget(parent) {}; | 16 Graph(QWidget* parent = nullptr) : QWidget(parent) {}; |
| 16 void AddItem(T key, int val) { map[key] = val; update(); updateGeometry(); }; | 17 void AddItem(T key, int val) { map[key] = val; update(); updateGeometry(); }; |
| 17 void Clear() { map.clear(); update(); updateGeometry(); }; | 18 void Clear() { map.clear(); update(); updateGeometry(); }; |
| 34 /* now we do the actual painting */ | 35 /* now we do the actual painting */ |
| 35 QPainter painter(this); | 36 QPainter painter(this); |
| 36 | 37 |
| 37 int i = 0; | 38 int i = 0; |
| 38 for (const auto& item : map) { | 39 for (const auto& item : map) { |
| 39 painter.drawText(QRect(0, i*ITEM_HEIGHT, TEXT_WIDTH, ITEM_HEIGHT), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); | 40 painter.drawText(QRect(rect.x(), rect.y() + i * ITEM_HEIGHT, TEXT_WIDTH, ITEM_HEIGHT), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); |
| 40 | 41 |
| 41 if (size) { | 42 if (size) { |
| 42 painter.save(); | 43 painter.save(); |
| 43 | 44 |
| 44 QPen pen(Qt::transparent, 0); | 45 QPen pen(Qt::transparent, 0); |
| 45 painter.setPen(pen); | 46 painter.setPen(pen); |
| 46 | 47 |
| 47 QPainterPath path; | 48 QPainterPath path; |
| 48 path.addRect(rect.x()+35, rect.y()+(i*ITEM_HEIGHT), (static_cast<double>(item.second)/size)*(rect.width()-35), ITEM_HEIGHT); | 49 path.addRect(rect.x()+35, rect.y() + i * ITEM_HEIGHT, (static_cast<double>(item.second)/size)*(rect.width()-35), ITEM_HEIGHT); |
| 49 painter.fillPath(path, Qt::blue); | 50 painter.fillPath(path, Qt::blue); |
| 50 painter.drawPath(path); | 51 painter.drawPath(path); |
| 51 | 52 |
| 52 painter.restore(); | 53 painter.restore(); |
| 53 } | 54 } |
