Mercurial > minori
annotate include/gui/widgets/text.h @ 367:8d45d892be88 default tip
*: instead of pugixml, use Qt XML features
this means we have one extra Qt dependency though...
author | Paper <paper@tflc.us> |
---|---|
date | Sun, 17 Nov 2024 22:55:47 -0500 |
parents | f81bed4e04ac |
children |
rev | line source |
---|---|
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
1 #ifndef MINORI_GUI_WIDGETS_TEXT_H_ |
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
2 #define MINORI_GUI_WIDGETS_TEXT_H_ |
64 | 3 |
76 | 4 #include <QLineEdit> |
5 #include <QPlainTextEdit> | |
69 | 6 #include <QSize> |
76 | 7 #include <QWidget> |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
8 #include <QLabel> |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
9 #include <QVBoxLayout> |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
10 #include <QPointer> |
69 | 11 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
12 #include <memory> |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
13 #include <unordered_map> |
64 | 14 |
46 | 15 namespace TextWidgets { |
16 | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
17 /* These used to have getter methods to get the real Qt widgets; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
18 * don't make those anymore. Instead, add new methods that are |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
19 * wrappers around the Qt methods instead. */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
20 |
46 | 21 class Header : public QWidget { |
258 | 22 Q_OBJECT |
46 | 23 |
258 | 24 public: |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
25 Header(QWidget* parent = nullptr); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
26 void SetText(const std::string& title); |
46 | 27 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
28 protected: |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
29 QPointer<QLabel> title_; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
30 QPointer<QFrame> separator_; |
46 | 31 }; |
32 | |
365
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
33 class Label final : public QLabel { |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
34 Q_OBJECT |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
35 |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
36 public: |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
37 Label(QWidget *parent = nullptr); |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
38 Label(const QString &string, QWidget *parent = nullptr); |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
39 void SetElidingMode(bool elide); |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
40 |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
41 protected: |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
42 void paintEvent(QPaintEvent *event) override; |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
43 |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
44 private: |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
45 bool elide_; |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
46 }; |
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
47 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
48 /* This is a nice clean wrapper around Label suitable for our needs. */ |
291 | 49 class Paragraph : public QWidget { |
253 | 50 Q_OBJECT |
46 | 51 |
253 | 52 public: |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
53 Paragraph(QWidget* parent = nullptr); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
54 void SetText(const std::string& text); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
55 void SetSelectable(bool enable); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
56 void SetWordWrap(bool enable); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
57 void SetElidingMode(bool enable); |
291 | 58 |
59 protected: | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
60 QPointer<QLabel> label_; |
46 | 61 }; |
62 | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
63 /* This aligns data with labels */ |
253 | 64 class LabelledParagraph final : public QWidget { |
65 Q_OBJECT | |
66 | |
67 public: | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
68 enum Style { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
69 BoldedLabels = (1 << 1), |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
70 ElidedData = (1 << 2), /* does nothing for now */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
71 }; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
72 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
73 LabelledParagraph(QWidget* parent = nullptr); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
74 ~LabelledParagraph(); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
75 void SetData(const std::vector<std::pair<std::string, std::string>>& data); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
76 void SetStyle(int style); /* bit-flags from Style enum */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
77 void Clear(void); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
78 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
79 protected: |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
80 QPointer<QWidget> contents_; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
81 QPointer<QGridLayout> contents_layout_; |
291 | 82 |
365
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
348
diff
changeset
|
83 std::vector<std::pair<QSharedPointer<Label>, QSharedPointer<Label>>> data_; |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
84 }; |
46 | 85 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
86 /* this is just a generic QLabel with a specific font and foreground role, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
87 * which is why it's defined inline */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
88 class Title final : public Paragraph { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
89 public: |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
90 Title(QWidget* parent = nullptr) : Paragraph(parent) { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
91 QFont fnt(label_->font()); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
92 fnt.setPixelSize(16); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
93 label_->setFont(fnt); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
94 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
95 label_->setForegroundRole(QPalette::Highlight); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
96 } |
253 | 97 }; |
98 | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
99 /* ----------------------------------------------------------------------- */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
100 /* Generic "Section" widget */ |
253 | 101 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
102 template<typename T> |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
103 class Section final : public QWidget { |
253 | 104 public: |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
105 Section(QWidget* parent = nullptr) : QWidget(parent) { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
106 header_ = new Header(this); |
291 | 107 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
108 content_container_ = new QWidget(this); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
109 content_ = new T(content_container_); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
110 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
111 QVBoxLayout* content_layout = new QVBoxLayout(content_container_); |
75 | 112 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
113 content_layout->addWidget(content_); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
114 content_layout->setSpacing(0); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
115 content_layout->setContentsMargins(12, 0, 0, 0); |
75 | 116 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
117 content_container_->setContentsMargins(0, 0, 0, 0); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
118 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
119 content_container_->setLayout(content_layout); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
120 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
121 QVBoxLayout* layout = new QVBoxLayout(this); |
75 | 122 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
123 layout->addWidget(header_); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
124 layout->addWidget(content_container_); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
125 layout->setSpacing(0); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
126 layout->setContentsMargins(0, 0, 0, 0); |
75 | 127 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
128 setLayout(layout); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
129 } |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
130 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
131 Header& GetHeader() { return *header_; } |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
132 T& GetContent() { return *content_; } |
46 | 133 |
258 | 134 private: |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
135 Header* header_; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
136 T* content_; |
46 | 137 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
138 /* I don't think making a separate container |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
139 * widget is necessary anymore, but I'm paranoid */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
140 QWidget* content_container_; |
46 | 141 }; |
142 | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
143 /* Old aliases used when the sections weren't templateized. |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
144 * |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
145 * These are kept to keep old code working and can largely |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
146 * be ignored for anything new. |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
147 * |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
148 * SelectableSection is actually just a generic "long text" */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
149 using LabelledSection = Section<LabelledParagraph>; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
150 using SelectableSection = Section<Paragraph>; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
151 using OneLineSection = Section<Paragraph>; |
64 | 152 |
153 } // namespace TextWidgets | |
154 | |
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
155 #endif // MINORI_GUI_WIDGETS_TEXT_H_ |