Mercurial > minori
changeset 172:45a0967485f1
graph, statistics: make my code a little less messy
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 28 Nov 2023 13:22:35 -0500 |
parents | 03b444cbe55f |
children | de0a8d2f28b3 |
files | include/gui/widgets/graph.h src/gui/pages/statistics.cc |
diffstat | 2 files changed, 43 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/include/gui/widgets/graph.h Mon Nov 27 13:51:27 2023 -0500 +++ b/include/gui/widgets/graph.h Tue Nov 28 13:22:35 2023 -0500 @@ -23,8 +23,6 @@ void Clear() { map.clear(); update(); updateGeometry(); }; protected: - static constexpr int SPACING = 5; - std::unordered_map<T, unsigned long> map = {}; QSize minimumSizeHint() const override { @@ -48,7 +46,7 @@ QFontMetrics metric(font()); for (const auto& item : map) { - unsigned long width = metric.boundingRect(QString::number(item.first)).width(); + unsigned long width = metric.horizontalAdvance(QString::number(item.first), -1); if (width > ret) ret = width; } @@ -57,34 +55,35 @@ } void paintEvent(QPaintEvent* event) override { + static constexpr int HORIZ_SPACING = 5; + static constexpr int VERT_SPACING = 2; + + /* these are retrieved from the QPaintEvent */ const QRect rect = event->rect(); - const int height_of_each = QFontMetrics(font()).height(); - const int size = GetTotal(); - const int text_width = GetTextWidth(); + const int width = event->rect().width(); const int x = rect.x(); int y = rect.y(); + /* these are calculated from font metrics and such */ + const int total = GetTotal(); + const int text_width = GetTextWidth() + 10; + const int each_height = QFontMetrics(font()).height(); + /* now we do the actual painting */ QPainter painter(this); - for (const auto& item : map) { - painter.drawText(QRect(x + 2, y, text_width - 2, height_of_each), Qt::AlignRight | Qt::AlignVCenter, QString::number(item.first)); - - if (size) { - painter.save(); + for (const auto& [key, value] : map) { + painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter, QString::number(key)); - QPen pen(Qt::transparent, 0); - painter.setPen(pen); - + /* only draw this if we actually have any data */ + if (total) { QPainterPath path; - path.addRect(x + text_width + 2 + SPACING, y, (static_cast<double>(item.second)/size) * (rect.width() - text_width + 2 - SPACING), height_of_each); + path.addRect(x + text_width + HORIZ_SPACING, y, (static_cast<double>(value)/total) * (width - text_width - HORIZ_SPACING), each_height); painter.fillPath(path, Qt::darkBlue); painter.drawPath(path); - - painter.restore(); } - y += height_of_each + 2; + y += each_height + VERT_SPACING; } } };
--- a/src/gui/pages/statistics.cc Mon Nov 27 13:51:27 2023 -0500 +++ b/src/gui/pages/statistics.cc Tue Nov 28 13:22:35 2023 -0500 @@ -24,35 +24,39 @@ tr("Anime list"), tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), "", this)); + layout->addWidget(_anime_list.get()); - QWidget* score_dist_widget = new QWidget(this); - QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); + { + QWidget* score_dist_widget = new QWidget(this); + QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); - score_dist_layout->addWidget(new TextWidgets::Header(tr("Score distribution"), score_dist_widget)); + score_dist_layout->addWidget(new TextWidgets::Header(tr("Score distribution"), score_dist_widget)); + + /* Ew */ + { + QWidget* score_graph_parent = new QWidget(score_dist_widget); + QVBoxLayout* score_parent_layout = new QVBoxLayout(score_graph_parent); - /* I have to explain myself here: I hate this :). This makes a widget as a layer to create a margin, - similar to what I do in text.cc with sections. I hate it and it should really be put into a separate - class, but whatever. */ - QWidget* content = new QWidget(score_dist_widget); - QVBoxLayout* content_layout = new QVBoxLayout(content); - _score_distribution_graph.reset(new Graph<int>(content)); - content_layout->addWidget(_score_distribution_graph.get()); - content_layout->setSpacing(0); - /* For some reason, when we set the margin to 12 on any paragraphs it - actually doesn't draw them 12 pixels away. It draws them ~15 pixels - away! I'm assuming this is just Qt's widgets being weird (they usually are) - and I hope it's nothing I *really* have to worry about... */ - content_layout->setContentsMargins(15, 0, 0, 0); - content->setContentsMargins(0, 0, 0, 0); + _score_distribution_graph.reset(new Graph<int>(score_graph_parent)); + score_parent_layout->addWidget(_score_distribution_graph.get()); + + score_parent_layout->setSpacing(0); + score_parent_layout->setContentsMargins(12, 0, 0, 0); + + score_graph_parent->setContentsMargins(3, 0, 0, 0); - score_dist_layout->addWidget(content); - score_dist_layout->setContentsMargins(0, 0, 0, 0); + score_dist_layout->addWidget(score_graph_parent); + } + + score_dist_layout->setContentsMargins(0, 0, 0, 0); + + layout->addWidget(score_dist_widget); + } + _application.reset(new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this)); + layout->addWidget(_application.get()); - layout->addWidget(_anime_list.get()); - layout->addWidget(score_dist_widget); - layout->addWidget(_application.get()); layout->addStretch(); QTimer* timer = new QTimer(this);