changeset 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 dd211ff68b36
files include/gui/widgets/graph.h
diffstat 1 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/include/gui/widgets/graph.h	Mon Apr 01 02:43:44 2024 -0400
+++ b/include/gui/widgets/graph.h	Mon Apr 01 18:11:15 2024 -0400
@@ -30,12 +30,15 @@
 	};
 
 protected:
+	static constexpr int HORIZ_SPACING = 5;
+	static constexpr int VERT_SPACING = 3;
+
 	std::unordered_map<T, unsigned long> map = {};
 
 	QSize minimumSizeHint() const override {
 		QFontMetrics metric(font());
 		/* wtf?... */
-		return QSize(100, (metric.height() * map.size()) + (2 * map.size()));
+		return QSize(100, (metric.height() * map.size()) + (VERT_SPACING * map.size()));
 	}
 
 	/* helper functions */
@@ -61,37 +64,66 @@
 		return ret;
 	}
 
+	inline unsigned long GetValueWidth() {
+		unsigned long ret = 0;
+		QFontMetrics metric(font());
+
+		for (const auto& item : map) {
+			unsigned long width = metric.horizontalAdvance(QString::number(item.second), -1);
+			if (width > ret)
+				ret = width;
+		}
+
+		return ret;
+	}
+
 	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 width = event->rect().width();
+		const int width = 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();
+		const int value_width = GetValueWidth();
 		const int each_height = QFontMetrics(font()).height();
 
 		/* now we do the actual painting */
 		QPainter painter(this);
 
 		for (const auto& [key, value] : map) {
+			int offset = 0;
+
 			painter.drawText(QRect(x, y, text_width, each_height), Qt::AlignVCenter | Qt::AlignRight,
 			                 QString::number(key));
 
+			offset += text_width + HORIZ_SPACING;
+
 			/* only draw this if we actually have any data */
 			if (total) {
+				painter.save();
+
+				QPen pen(painter.pen());
+				pen.setStyle(Qt::NoPen);
+				painter.setPen(pen);
+
 				QPainterPath path;
-				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);
+				path.addRect(x + offset, y,
+				             (static_cast<double>(value) / total) * (width - offset - HORIZ_SPACING - value_width), each_height);
+				painter.fillPath(path, Qt::darkGreen);
 				painter.drawPath(path);
+
+				offset += (static_cast<double>(value) / total) * (width - offset - HORIZ_SPACING - value_width);
+
+				painter.restore();
 			}
 
+			offset += HORIZ_SPACING;
+
+			painter.drawText(QRect(x + offset, y, value_width, each_height), Qt::AlignVCenter | Qt::AlignLeft, QString::number(value));
+
 			y += each_height + VERT_SPACING;
 		}
 	}