diff include/gui/widgets/text.h @ 348:6b0768158dcd

text: redesign almost every widget i.e. Paragraph is now a QLabel, etc etc, some things will probably break, idc
author Paper <paper@paper.us.eu.org>
date Tue, 25 Jun 2024 11:19:54 -0400
parents b82841e76e79
children f81bed4e04ac
line wrap: on
line diff
--- a/include/gui/widgets/text.h	Sun Jun 23 10:32:09 2024 -0400
+++ b/include/gui/widgets/text.h	Tue Jun 25 11:19:54 2024 -0400
@@ -4,131 +4,136 @@
 #include <QLineEdit>
 #include <QPlainTextEdit>
 #include <QSize>
-#include <QString>
 #include <QWidget>
+#include <QLabel>
+#include <QVBoxLayout>
+#include <QPointer>
 
-class QFrame;
-
-#include <QLabel>
+#include <memory>
+#include <unordered_map>
 
 namespace TextWidgets {
 
+/* These used to have getter methods to get the real Qt widgets;
+ * don't make those anymore. Instead, add new methods that are
+ * wrappers around the Qt methods instead. */
+
 class Header : public QWidget {
 	Q_OBJECT
 
 public:
-	Header(const QString& title, QWidget* parent = nullptr);
-	void SetText(const QString& title);
+	Header(QWidget* parent = nullptr);
+	void SetText(const std::string& title);
 
-private:
-	QLabel static_text_title;
-	QFrame static_text_line;
+protected:
+	QPointer<QLabel> title_;
+	QPointer<QFrame> separator_;
 };
 
+/* This is a nice clean wrapper around Label suitable for our needs. */
 class Paragraph : public QWidget {
 	Q_OBJECT
 
 public:
-	Paragraph(const QString& text, QWidget* parent = nullptr);
-	void SetText(const QString& text);
-	QPlainTextEdit* GetLabel();
+	Paragraph(QWidget* parent = nullptr);
+	void SetText(const std::string& text);
+	void SetSelectable(bool enable);
+	void SetWordWrap(bool enable);
+	void SetElidingMode(bool enable);
 
 protected:
-	bool hasHeightForWidth() const override;
-	int heightForWidth(int w) const override;
-
-private:
-	QPlainTextEdit text_edit;
+	QPointer<QLabel> label_;
 };
 
+/* This aligns data with labels */
 class LabelledParagraph final : public QWidget {
 	Q_OBJECT
 
 public:
-	LabelledParagraph(const QString& label, const QString& data, QWidget* parent = nullptr);
-	QLabel* GetLabels();
-	QLabel* GetData();
+	enum Style {
+		BoldedLabels = (1 << 1),
+		ElidedData = (1 << 2), /* does nothing for now */
+	};
+
+	LabelledParagraph(QWidget* parent = nullptr);
+	~LabelledParagraph();
+	void SetData(const std::vector<std::pair<std::string, std::string>>& data);
+	void SetStyle(int style); /* bit-flags from Style enum */
+	void Clear(void);
+
+protected:
+	QPointer<QWidget> contents_;
+	QPointer<QGridLayout> contents_layout_;
 
-	/* synonymous with GetData(), kept for compatibility. don't use in new code!!! */
-	QLabel* GetParagraph();
+	std::vector<std::pair<QSharedPointer<QLabel>, QSharedPointer<QLabel>>> data_;
+};
 
-private:
-	QLabel labels_;
-	QLabel data_;
+/* this is just a generic QLabel with a specific font and foreground role,
+ * which is why it's defined inline */
+class Title final : public Paragraph {
+public:
+	Title(QWidget* parent = nullptr) : Paragraph(parent) {
+		QFont fnt(label_->font());
+		fnt.setPixelSize(16);
+		label_->setFont(fnt);
+
+		label_->setForegroundRole(QPalette::Highlight);
+	}
 };
 
-class Line : public QWidget {
-	Q_OBJECT
+/* ----------------------------------------------------------------------- */
+/* Generic "Section" widget */
 
+template<typename T>
+class Section final : public QWidget {
 public:
-	Line(QWidget* parent = nullptr);
-	Line(const QString& text, QWidget* parent = nullptr);
-	void SetText(const QString& text);
+	Section(QWidget* parent = nullptr) : QWidget(parent) {
+		header_ = new Header(this);
 
-protected:
-	QLineEdit line_edit_;
-};
+		content_container_ = new QWidget(this);
+		content_ = new T(content_container_);
+
+		QVBoxLayout* content_layout = new QVBoxLayout(content_container_);
 
-class Title final : public Line {
-	Q_OBJECT
+		content_layout->addWidget(content_);
+		content_layout->setSpacing(0);
+		content_layout->setContentsMargins(12, 0, 0, 0);
 
-public:
-	Title(const QString& title, QWidget* parent = nullptr);
-};
+		content_container_->setContentsMargins(0, 0, 0, 0);
+
+		content_container_->setLayout(content_layout);
+
+		QVBoxLayout* layout = new QVBoxLayout(this);
 
-class Section final : public QWidget {
-	Q_OBJECT
+		layout->addWidget(header_);
+		layout->addWidget(content_container_);
+		layout->setSpacing(0);
+		layout->setContentsMargins(0, 0, 0, 0);
 
-public:
-	Section(const QString& title, const QString& data, QWidget* parent = nullptr);
-	Header* GetHeader();
-	Paragraph* GetParagraph();
+		setLayout(layout);
+	}
+
+	Header& GetHeader() { return *header_; }
+	T& GetContent() { return *content_; }
 
 private:
-	Header* header;
-	Paragraph* paragraph;
-};
-
-class LabelledSection final : public QWidget {
-	Q_OBJECT
+	Header* header_;
+	T* content_;
 
-public:
-	LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent = nullptr);
-	Header* GetHeader();
-	QLabel* GetLabels();
-	QLabel* GetData();
-	QLabel* GetParagraph();
-
-private:
-	Header* header;
-	LabelledParagraph* content;
+	/* I don't think making a separate container
+	 * widget is necessary anymore, but I'm paranoid */
+	QWidget* content_container_;
 };
 
-class SelectableSection final : public QWidget {
-	Q_OBJECT
-
-public:
-	SelectableSection(const QString& title, const QString& data, QWidget* parent = nullptr);
-	Header* GetHeader();
-	Paragraph* GetParagraph();
-
-private:
-	Header* header;
-	Paragraph* paragraph;
-};
-
-class OneLineSection final : public QWidget {
-	Q_OBJECT
-
-public:
-	OneLineSection(const QString& title, const QString& data, QWidget* parent = nullptr);
-	Header* GetHeader();
-	Line* GetLine();
-
-private:
-	Header* header;
-	Line* line;
-};
+/* Old aliases used when the sections weren't templateized.
+ *
+ * These are kept to keep old code working and can largely
+ * be ignored for anything new.
+ *
+ * SelectableSection is actually just a generic "long text" */
+using LabelledSection = Section<LabelledParagraph>;
+using SelectableSection = Section<Paragraph>;
+using OneLineSection = Section<Paragraph>;
 
 } // namespace TextWidgets