comparison src/gui/widgets/text.cc @ 81:9b2b41f83a5e

boring: mass rename to cc because this is a very unix-y project, it makes more sense to use the 'cc' extension
author Paper <mrpapersonic@gmail.com>
date Mon, 23 Oct 2023 12:07:27 -0400
parents src/gui/widgets/text.cpp@6f7385bd334c
children d02fdf1d6708
comparison
equal deleted inserted replaced
80:825506f0e221 81:9b2b41f83a5e
1 #include "gui/widgets/text.h"
2 #include "core/session.h"
3 #include <QDebug>
4 #include <QFrame>
5 #include <QLabel>
6 #include <QTextBlock>
7 #include <QVBoxLayout>
8
9 namespace TextWidgets {
10
11 Header::Header(QString title, QWidget* parent) : QWidget(parent) {
12 QVBoxLayout* layout = new QVBoxLayout(this);
13 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
14
15 static_text_title = new QLabel(title, this);
16 static_text_title->setTextFormat(Qt::PlainText);
17 QFont font = static_text_title->font();
18 font.setWeight(QFont::Bold);
19 static_text_title->setFont(font);
20
21 static_text_line = new QFrame(this);
22 static_text_line->setFrameShape(QFrame::HLine);
23 static_text_line->setFrameShadow(QFrame::Sunken);
24 static_text_line->setFixedHeight(2);
25
26 layout->addWidget(static_text_title);
27 layout->addWidget(static_text_line);
28 layout->setSpacing(0);
29 layout->setContentsMargins(0, 0, 0, 0);
30 }
31
32 void Header::SetText(QString text) {
33 static_text_title->setText(text);
34 }
35
36 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */
37 Paragraph::Paragraph(QString text, QWidget* parent) : QPlainTextEdit(text, parent) {
38 setTextInteractionFlags(Qt::TextBrowserInteraction);
39 setFrameShape(QFrame::NoFrame);
40 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
42
43 QPalette pal;
44 pal.setColor(QPalette::Window, Qt::transparent);
45 setPalette(pal);
46
47 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
48 }
49
50 void Paragraph::SetText(QString text) {
51 QTextDocument* document = new QTextDocument(this);
52 document->setDocumentLayout(new QPlainTextDocumentLayout(document));
53 document->setPlainText(text);
54 setDocument(document);
55 }
56
57 /* highly based upon... some stackoverflow answer for PyQt */
58 QSize Paragraph::minimumSizeHint() const {
59 return QSize(0, 0);
60 }
61
62 QSize Paragraph::sizeHint() const {
63 QTextDocument* doc = document();
64 doc->adjustSize();
65 long h = 0;
66 for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) {
67 h += doc->documentLayout()->blockBoundingRect(line).height();
68 }
69 return QSize(doc->size().width(), h);
70 }
71
72 /* Equivalent to Paragraph(), but is only capable of showing one line. Only
73 exists because sometimes with SelectableSection it will let you go
74 out of bounds */
75 Line::Line(QString text, QWidget* parent) : QLineEdit(text, parent) {
76 setFrame(false);
77 setReadOnly(true);
78 setCursorPosition(0); /* displays left text first */
79
80 QPalette pal;
81 pal.setColor(QPalette::Window, Qt::transparent);
82 setPalette(pal);
83
84 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
85 }
86
87 Title::Title(QString title, QWidget* parent) : Line(title, parent) {
88 QFont fnt(font());
89 fnt.setPixelSize(16);
90 setFont(fnt);
91
92 QPalette pal(palette());
93 pal.setColor(QPalette::Window, Qt::transparent);
94 pal.setColor(QPalette::Text, QColor(0x00, 0x33, 0x99));
95 setPalette(pal);
96 }
97
98 Section::Section(QString title, QString data, QWidget* parent) : QWidget(parent) {
99 QVBoxLayout* layout = new QVBoxLayout(this);
100
101 header = new Header(title, this);
102
103 QWidget* content = new QWidget(this);
104 QHBoxLayout* content_layout = new QHBoxLayout(content);
105
106 paragraph = new Paragraph(data, this);
107 paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
108 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
109 paragraph->setWordWrapMode(QTextOption::NoWrap);
110
111 content_layout->addWidget(paragraph);
112 content_layout->setSpacing(0);
113 content_layout->setContentsMargins(0, 0, 0, 0);
114 content->setContentsMargins(12, 0, 0, 0);
115
116 layout->addWidget(header);
117 layout->addWidget(paragraph);
118 layout->setSpacing(0);
119 layout->setContentsMargins(0, 0, 0, 0);
120 }
121
122 Header* Section::GetHeader() {
123 return header;
124 }
125
126 Paragraph* Section::GetParagraph() {
127 return paragraph;
128 }
129
130 LabelledSection::LabelledSection(QString title, QString label, QString data, QWidget* parent) : QWidget(parent) {
131 QVBoxLayout* layout = new QVBoxLayout(this);
132
133 header = new Header(title, this);
134
135 // this is not accessible from the object because there's really
136 // no reason to make it accessible...
137 QWidget* content = new QWidget(this);
138 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
139
140 labels = new Paragraph(label, this);
141 labels->setTextInteractionFlags(Qt::NoTextInteraction);
142 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
143 labels->setWordWrapMode(QTextOption::NoWrap);
144 labels->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
145
146 paragraph = new Paragraph(data, this);
147 paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
148 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
149 paragraph->setWordWrapMode(QTextOption::NoWrap);
150
151 QHBoxLayout* content_layout = new QHBoxLayout(content);
152 content_layout->addWidget(labels, 0, Qt::AlignTop);
153 content_layout->addWidget(paragraph, 0, Qt::AlignTop);
154 content_layout->setSpacing(20);
155 content_layout->setContentsMargins(0, 0, 0, 0);
156
157 content->setContentsMargins(12, 0, 0, 0);
158
159 layout->addWidget(header);
160 layout->addWidget(content);
161 layout->setSpacing(0);
162 layout->setContentsMargins(0, 0, 0, 0);
163 }
164
165 Header* LabelledSection::GetHeader() {
166 return header;
167 }
168
169 Paragraph* LabelledSection::GetLabels() {
170 return labels;
171 }
172
173 Paragraph* LabelledSection::GetParagraph() {
174 return paragraph;
175 }
176
177 SelectableSection::SelectableSection(QString title, QString data, QWidget* parent) : QWidget(parent) {
178 QVBoxLayout* layout = new QVBoxLayout(this);
179
180 header = new Header(title, this);
181
182 QWidget* content = new QWidget(this);
183 QHBoxLayout* content_layout = new QHBoxLayout(content);
184
185 paragraph = new Paragraph(data, content);
186
187 content_layout->addWidget(paragraph);
188 content_layout->setSpacing(0);
189 content_layout->setContentsMargins(0, 0, 0, 0);
190 content->setContentsMargins(12, 0, 0, 0);
191
192 layout->addWidget(header);
193 layout->addWidget(content);
194 layout->setSpacing(0);
195 layout->setContentsMargins(0, 0, 0, 0);
196 }
197
198 Header* SelectableSection::GetHeader() {
199 return header;
200 }
201
202 Paragraph* SelectableSection::GetParagraph() {
203 return paragraph;
204 }
205
206 OneLineSection::OneLineSection(QString title, QString text, QWidget* parent) : QWidget(parent) {
207 QVBoxLayout* layout = new QVBoxLayout(this);
208
209 header = new Header(title, this);
210
211 QWidget* content = new QWidget(this);
212 QHBoxLayout* content_layout = new QHBoxLayout(content);
213
214 line = new Line(text, content);
215
216 content_layout->addWidget(line);
217 content_layout->setSpacing(0);
218 content_layout->setContentsMargins(0, 0, 0, 0);
219 content->setContentsMargins(12, 0, 0, 0);
220
221 layout->addWidget(header);
222 layout->addWidget(content);
223 layout->setSpacing(0);
224 layout->setContentsMargins(0, 0, 0, 0);
225 }
226
227 Header* OneLineSection::GetHeader() {
228 return header;
229 }
230
231 Line* OneLineSection::GetLine() {
232 return line;
233 }
234
235 } // namespace TextWidgets
236
237 #include "gui/widgets/moc_text.cpp"