comparison 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
comparison
equal deleted inserted replaced
347:a0aa8c8c4307 348:6b0768158dcd
2 #define MINORI_GUI_WIDGETS_TEXT_H_ 2 #define MINORI_GUI_WIDGETS_TEXT_H_
3 3
4 #include <QLineEdit> 4 #include <QLineEdit>
5 #include <QPlainTextEdit> 5 #include <QPlainTextEdit>
6 #include <QSize> 6 #include <QSize>
7 #include <QString>
8 #include <QWidget> 7 #include <QWidget>
8 #include <QLabel>
9 #include <QVBoxLayout>
10 #include <QPointer>
9 11
10 class QFrame; 12 #include <memory>
11 13 #include <unordered_map>
12 #include <QLabel>
13 14
14 namespace TextWidgets { 15 namespace TextWidgets {
16
17 /* These used to have getter methods to get the real Qt widgets;
18 * don't make those anymore. Instead, add new methods that are
19 * wrappers around the Qt methods instead. */
15 20
16 class Header : public QWidget { 21 class Header : public QWidget {
17 Q_OBJECT 22 Q_OBJECT
18 23
19 public: 24 public:
20 Header(const QString& title, QWidget* parent = nullptr); 25 Header(QWidget* parent = nullptr);
21 void SetText(const QString& title); 26 void SetText(const std::string& title);
22 27
23 private: 28 protected:
24 QLabel static_text_title; 29 QPointer<QLabel> title_;
25 QFrame static_text_line; 30 QPointer<QFrame> separator_;
26 }; 31 };
27 32
33 /* This is a nice clean wrapper around Label suitable for our needs. */
28 class Paragraph : public QWidget { 34 class Paragraph : public QWidget {
29 Q_OBJECT 35 Q_OBJECT
30 36
31 public: 37 public:
32 Paragraph(const QString& text, QWidget* parent = nullptr); 38 Paragraph(QWidget* parent = nullptr);
33 void SetText(const QString& text); 39 void SetText(const std::string& text);
34 QPlainTextEdit* GetLabel(); 40 void SetSelectable(bool enable);
41 void SetWordWrap(bool enable);
42 void SetElidingMode(bool enable);
35 43
36 protected: 44 protected:
37 bool hasHeightForWidth() const override; 45 QPointer<QLabel> label_;
38 int heightForWidth(int w) const override;
39
40 private:
41 QPlainTextEdit text_edit;
42 }; 46 };
43 47
48 /* This aligns data with labels */
44 class LabelledParagraph final : public QWidget { 49 class LabelledParagraph final : public QWidget {
45 Q_OBJECT 50 Q_OBJECT
46 51
47 public: 52 public:
48 LabelledParagraph(const QString& label, const QString& data, QWidget* parent = nullptr); 53 enum Style {
49 QLabel* GetLabels(); 54 BoldedLabels = (1 << 1),
50 QLabel* GetData(); 55 ElidedData = (1 << 2), /* does nothing for now */
56 };
51 57
52 /* synonymous with GetData(), kept for compatibility. don't use in new code!!! */ 58 LabelledParagraph(QWidget* parent = nullptr);
53 QLabel* GetParagraph(); 59 ~LabelledParagraph();
60 void SetData(const std::vector<std::pair<std::string, std::string>>& data);
61 void SetStyle(int style); /* bit-flags from Style enum */
62 void Clear(void);
63
64 protected:
65 QPointer<QWidget> contents_;
66 QPointer<QGridLayout> contents_layout_;
67
68 std::vector<std::pair<QSharedPointer<QLabel>, QSharedPointer<QLabel>>> data_;
69 };
70
71 /* this is just a generic QLabel with a specific font and foreground role,
72 * which is why it's defined inline */
73 class Title final : public Paragraph {
74 public:
75 Title(QWidget* parent = nullptr) : Paragraph(parent) {
76 QFont fnt(label_->font());
77 fnt.setPixelSize(16);
78 label_->setFont(fnt);
79
80 label_->setForegroundRole(QPalette::Highlight);
81 }
82 };
83
84 /* ----------------------------------------------------------------------- */
85 /* Generic "Section" widget */
86
87 template<typename T>
88 class Section final : public QWidget {
89 public:
90 Section(QWidget* parent = nullptr) : QWidget(parent) {
91 header_ = new Header(this);
92
93 content_container_ = new QWidget(this);
94 content_ = new T(content_container_);
95
96 QVBoxLayout* content_layout = new QVBoxLayout(content_container_);
97
98 content_layout->addWidget(content_);
99 content_layout->setSpacing(0);
100 content_layout->setContentsMargins(12, 0, 0, 0);
101
102 content_container_->setContentsMargins(0, 0, 0, 0);
103
104 content_container_->setLayout(content_layout);
105
106 QVBoxLayout* layout = new QVBoxLayout(this);
107
108 layout->addWidget(header_);
109 layout->addWidget(content_container_);
110 layout->setSpacing(0);
111 layout->setContentsMargins(0, 0, 0, 0);
112
113 setLayout(layout);
114 }
115
116 Header& GetHeader() { return *header_; }
117 T& GetContent() { return *content_; }
54 118
55 private: 119 private:
56 QLabel labels_; 120 Header* header_;
57 QLabel data_; 121 T* content_;
122
123 /* I don't think making a separate container
124 * widget is necessary anymore, but I'm paranoid */
125 QWidget* content_container_;
58 }; 126 };
59 127
60 class Line : public QWidget { 128 /* Old aliases used when the sections weren't templateized.
61 Q_OBJECT 129 *
62 130 * These are kept to keep old code working and can largely
63 public: 131 * be ignored for anything new.
64 Line(QWidget* parent = nullptr); 132 *
65 Line(const QString& text, QWidget* parent = nullptr); 133 * SelectableSection is actually just a generic "long text" */
66 void SetText(const QString& text); 134 using LabelledSection = Section<LabelledParagraph>;
67 135 using SelectableSection = Section<Paragraph>;
68 protected: 136 using OneLineSection = Section<Paragraph>;
69 QLineEdit line_edit_;
70 };
71
72 class Title final : public Line {
73 Q_OBJECT
74
75 public:
76 Title(const QString& title, QWidget* parent = nullptr);
77 };
78
79 class Section final : public QWidget {
80 Q_OBJECT
81
82 public:
83 Section(const QString& title, const QString& data, QWidget* parent = nullptr);
84 Header* GetHeader();
85 Paragraph* GetParagraph();
86
87 private:
88 Header* header;
89 Paragraph* paragraph;
90 };
91
92 class LabelledSection final : public QWidget {
93 Q_OBJECT
94
95 public:
96 LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent = nullptr);
97 Header* GetHeader();
98 QLabel* GetLabels();
99 QLabel* GetData();
100 QLabel* GetParagraph();
101
102 private:
103 Header* header;
104 LabelledParagraph* content;
105 };
106
107 class SelectableSection final : public QWidget {
108 Q_OBJECT
109
110 public:
111 SelectableSection(const QString& title, const QString& data, QWidget* parent = nullptr);
112 Header* GetHeader();
113 Paragraph* GetParagraph();
114
115 private:
116 Header* header;
117 Paragraph* paragraph;
118 };
119
120 class OneLineSection final : public QWidget {
121 Q_OBJECT
122
123 public:
124 OneLineSection(const QString& title, const QString& data, QWidget* parent = nullptr);
125 Header* GetHeader();
126 Line* GetLine();
127
128 private:
129 Header* header;
130 Line* line;
131 };
132 137
133 } // namespace TextWidgets 138 } // namespace TextWidgets
134 139
135 #endif // MINORI_GUI_WIDGETS_TEXT_H_ 140 #endif // MINORI_GUI_WIDGETS_TEXT_H_