diff include/gui/widgets/graph.h @ 202:71832ffe425a

animia: re-add kvm fd source this is all being merged from my wildly out-of-date laptop. SORRY! in other news, I edited the CI file to install the wayland client as well, so the linux CI build might finally get wayland stuff.
author Paper <paper@paper.us.eu.org>
date Tue, 02 Jan 2024 06:05:06 -0500
parents 45a0967485f1
children 862d0d8619f6
line wrap: on
line diff
--- a/include/gui/widgets/graph.h	Sun Nov 19 19:13:28 2023 -0500
+++ b/include/gui/widgets/graph.h	Tue Jan 02 06:05:06 2024 -0500
@@ -15,7 +15,7 @@
 #include <algorithm>
 #include <unordered_map>
 
-template <typename T>
+template<typename T>
 class Graph final : public QWidget {
 	public:
 		Graph(QWidget* parent = nullptr) : QWidget(parent) {};
@@ -23,57 +23,69 @@
 		void Clear() { map.clear(); update(); updateGeometry(); };
 
 	protected:
-		static constexpr int SPACING = 5;
+		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() - 2)));
+		}
+
+		/* helper functions */
 		inline unsigned long GetTotal() {
 			unsigned long count = 0;
+
 			for (const auto& item : map)
 				count += item.second;
+
 			return count;
 		}
-		QSize minimumSizeHint() const override {
-			QFontMetrics metric(font());
-			return QSize(100, metric.height() * map.size() + 2 * (map.size() - 2));
-		};
+
 		inline unsigned long GetTextWidth() {
 			unsigned long ret = 0;
 			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;
 			}
+
 			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 height_of_each = QFontMetrics(font()).height();
-			const int size = GetTotal();
-			const int text_width = GetTextWidth();
-			int current_y = rect.y();
+			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(rect.x(), current_y, text_width, 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(rect.x() + text_width + SPACING, current_y, (static_cast<double>(item.second)/size)*(rect.width() - text_width - 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();
-				}
-				current_y += height_of_each + 2;
+				y += each_height + VERT_SPACING;
 			}
-		};
-		std::unordered_map<T, unsigned long> map = {};
+		}
 };
 
 #endif // __gui__widgets__graph_h
\ No newline at end of file