Mercurial > minori
comparison include/gui/widgets/graph.h @ 259:0362f3c4534c
widgets/graph: improve drawing code
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 01 Apr 2024 18:11:15 -0400 |
parents | 862d0d8619f6 |
children | 3ec7804abf17 |
comparison
equal
deleted
inserted
replaced
258:862d0d8619f6 | 259:0362f3c4534c |
---|---|
28 update(); | 28 update(); |
29 updateGeometry(); | 29 updateGeometry(); |
30 }; | 30 }; |
31 | 31 |
32 protected: | 32 protected: |
33 static constexpr int HORIZ_SPACING = 5; | |
34 static constexpr int VERT_SPACING = 3; | |
35 | |
33 std::unordered_map<T, unsigned long> map = {}; | 36 std::unordered_map<T, unsigned long> map = {}; |
34 | 37 |
35 QSize minimumSizeHint() const override { | 38 QSize minimumSizeHint() const override { |
36 QFontMetrics metric(font()); | 39 QFontMetrics metric(font()); |
37 /* wtf?... */ | 40 /* wtf?... */ |
38 return QSize(100, (metric.height() * map.size()) + (2 * map.size())); | 41 return QSize(100, (metric.height() * map.size()) + (VERT_SPACING * map.size())); |
39 } | 42 } |
40 | 43 |
41 /* helper functions */ | 44 /* helper functions */ |
42 inline unsigned long GetTotal() { | 45 inline unsigned long GetTotal() { |
43 unsigned long count = 0; | 46 unsigned long count = 0; |
59 } | 62 } |
60 | 63 |
61 return ret; | 64 return ret; |
62 } | 65 } |
63 | 66 |
67 inline unsigned long GetValueWidth() { | |
68 unsigned long ret = 0; | |
69 QFontMetrics metric(font()); | |
70 | |
71 for (const auto& item : map) { | |
72 unsigned long width = metric.horizontalAdvance(QString::number(item.second), -1); | |
73 if (width > ret) | |
74 ret = width; | |
75 } | |
76 | |
77 return ret; | |
78 } | |
79 | |
64 void paintEvent(QPaintEvent* event) override { | 80 void paintEvent(QPaintEvent* event) override { |
65 static constexpr int HORIZ_SPACING = 5; | |
66 static constexpr int VERT_SPACING = 2; | |
67 | |
68 /* these are retrieved from the QPaintEvent */ | 81 /* these are retrieved from the QPaintEvent */ |
69 const QRect rect = event->rect(); | 82 const QRect rect = event->rect(); |
70 const int width = event->rect().width(); | 83 const int width = rect.width(); |
71 const int x = rect.x(); | 84 const int x = rect.x(); |
72 int y = rect.y(); | 85 int y = rect.y(); |
73 | 86 |
74 /* these are calculated from font metrics and such */ | 87 /* these are calculated from font metrics and such */ |
75 const int total = GetTotal(); | 88 const int total = GetTotal(); |
76 const int text_width = GetTextWidth(); | 89 const int text_width = GetTextWidth(); |
90 const int value_width = GetValueWidth(); | |
77 const int each_height = QFontMetrics(font()).height(); | 91 const int each_height = QFontMetrics(font()).height(); |
78 | 92 |
79 /* now we do the actual painting */ | 93 /* now we do the actual painting */ |
80 QPainter painter(this); | 94 QPainter painter(this); |
81 | 95 |
82 for (const auto& [key, value] : map) { | 96 for (const auto& [key, value] : map) { |
97 int offset = 0; | |
98 | |
83 painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter | Qt::AlignRight, | 99 painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter | Qt::AlignRight, |
84 QString::number(key)); | 100 QString::number(key)); |
85 | 101 |
102 offset += text_width + HORIZ_SPACING; | |
103 | |
86 /* only draw this if we actually have any data */ | 104 /* only draw this if we actually have any data */ |
87 if (total) { | 105 if (total) { |
106 painter.save(); | |
107 | |
108 QPen pen(painter.pen()); | |
109 pen.setStyle(Qt::NoPen); | |
110 painter.setPen(pen); | |
111 | |
88 QPainterPath path; | 112 QPainterPath path; |
89 path.addRect(x + text_width + HORIZ_SPACING, y, | 113 path.addRect(x + offset, y, |
90 (static_cast<double>(value) / total) * (width - text_width - HORIZ_SPACING), each_height); | 114 (static_cast<double>(value) / total) * (width - offset - HORIZ_SPACING - value_width), each_height); |
91 painter.fillPath(path, Qt::darkBlue); | 115 painter.fillPath(path, Qt::darkGreen); |
92 painter.drawPath(path); | 116 painter.drawPath(path); |
117 | |
118 offset += (static_cast<double>(value) / total) * (width - offset - HORIZ_SPACING - value_width); | |
119 | |
120 painter.restore(); | |
93 } | 121 } |
122 | |
123 offset += HORIZ_SPACING; | |
124 | |
125 painter.drawText(QRect(x + offset, y, value_width, each_height), Qt::AlignVCenter | Qt::AlignLeft, QString::number(value)); | |
94 | 126 |
95 y += each_height + VERT_SPACING; | 127 y += each_height + VERT_SPACING; |
96 } | 128 } |
97 } | 129 } |
98 }; | 130 }; |