Mercurial > minori
annotate include/gui/widgets/text.h @ 359:4e0e17d3c67a
CI/linux: ignore linuxdeployqt silently failing
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 15 Jul 2024 01:06:39 -0400 |
parents | 6b0768158dcd |
children | f81bed4e04ac |
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 | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
33 /* This is a nice clean wrapper around Label suitable for our needs. */ |
291 | 34 class Paragraph : public QWidget { |
253 | 35 Q_OBJECT |
46 | 36 |
253 | 37 public: |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
38 Paragraph(QWidget* parent = nullptr); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
39 void SetText(const std::string& text); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
40 void SetSelectable(bool enable); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
41 void SetWordWrap(bool enable); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
42 void SetElidingMode(bool enable); |
291 | 43 |
44 protected: | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
45 QPointer<QLabel> label_; |
46 | 46 }; |
47 | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
48 /* This aligns data with labels */ |
253 | 49 class LabelledParagraph final : public QWidget { |
50 Q_OBJECT | |
51 | |
52 public: | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
53 enum Style { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
54 BoldedLabels = (1 << 1), |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
55 ElidedData = (1 << 2), /* does nothing for now */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
56 }; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
57 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
58 LabelledParagraph(QWidget* parent = nullptr); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
59 ~LabelledParagraph(); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
60 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
|
61 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
|
62 void Clear(void); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
63 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
64 protected: |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
65 QPointer<QWidget> contents_; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
66 QPointer<QGridLayout> contents_layout_; |
291 | 67 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
68 std::vector<std::pair<QSharedPointer<QLabel>, QSharedPointer<QLabel>>> data_; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
69 }; |
46 | 70 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
71 /* 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
|
72 * which is why it's defined inline */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
73 class Title final : public Paragraph { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
74 public: |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
75 Title(QWidget* parent = nullptr) : Paragraph(parent) { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
76 QFont fnt(label_->font()); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
77 fnt.setPixelSize(16); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
78 label_->setFont(fnt); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
79 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
80 label_->setForegroundRole(QPalette::Highlight); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
81 } |
253 | 82 }; |
83 | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
84 /* ----------------------------------------------------------------------- */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
85 /* Generic "Section" widget */ |
253 | 86 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
87 template<typename T> |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
88 class Section final : public QWidget { |
253 | 89 public: |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
90 Section(QWidget* parent = nullptr) : QWidget(parent) { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
91 header_ = new Header(this); |
291 | 92 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
93 content_container_ = new QWidget(this); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
94 content_ = new T(content_container_); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
95 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
96 QVBoxLayout* content_layout = new QVBoxLayout(content_container_); |
75 | 97 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
98 content_layout->addWidget(content_); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
99 content_layout->setSpacing(0); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
100 content_layout->setContentsMargins(12, 0, 0, 0); |
75 | 101 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
102 content_container_->setContentsMargins(0, 0, 0, 0); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
103 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
104 content_container_->setLayout(content_layout); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
105 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
106 QVBoxLayout* layout = new QVBoxLayout(this); |
75 | 107 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
108 layout->addWidget(header_); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
109 layout->addWidget(content_container_); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
110 layout->setSpacing(0); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
111 layout->setContentsMargins(0, 0, 0, 0); |
75 | 112 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
113 setLayout(layout); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
114 } |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
115 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
116 Header& GetHeader() { return *header_; } |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
117 T& GetContent() { return *content_; } |
46 | 118 |
258 | 119 private: |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
120 Header* header_; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
121 T* content_; |
46 | 122 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
123 /* I don't think making a separate container |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
124 * widget is necessary anymore, but I'm paranoid */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
125 QWidget* content_container_; |
46 | 126 }; |
127 | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
128 /* 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
|
129 * |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
130 * 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
|
131 * be ignored for anything new. |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
132 * |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
133 * SelectableSection is actually just a generic "long text" */ |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
134 using LabelledSection = Section<LabelledParagraph>; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
135 using SelectableSection = Section<Paragraph>; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
136 using OneLineSection = Section<Paragraph>; |
64 | 137 |
138 } // namespace TextWidgets | |
139 | |
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
140 #endif // MINORI_GUI_WIDGETS_TEXT_H_ |