Mercurial > minori
annotate src/gui/widgets/text.cpp @ 75:d3e9310598b1
*: refactor some stuff
text: "TextParagraph"s are now called sections, because that's the
actual word for it :P
text: new classes: Line and OneLineSection, solves many problems with
paragraphs that are only one line long (ex. going out of bounds)
http: reworked http stuff to allow threaded get requests, also moved it
to its own file to (hopefully) remove clutter
eventually I'll make a threaded post request method and use that in
the "basic" function
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 04 Oct 2023 01:42:30 -0400 |
parents | 5ccb99bfa605 |
children | 3364fadc8a36 |
rev | line source |
---|---|
46 | 1 #include "gui/widgets/text.h" |
2 #include "core/session.h" | |
3 #include <QFrame> | |
4 #include <QLabel> | |
5 #include <QTextBlock> | |
6 #include <QVBoxLayout> | |
7 | |
8 namespace TextWidgets { | |
9 | |
10 Header::Header(QString title, QWidget* parent) : QWidget(parent) { | |
64 | 11 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 12 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
13 | |
14 static_text_title = new QLabel(title, this); | |
15 static_text_title->setTextFormat(Qt::PlainText); | |
16 QFont font = static_text_title->font(); | |
17 font.setWeight(QFont::Bold); | |
18 static_text_title->setFont(font); | |
64 | 19 /* FIXME: is this needed? */ |
46 | 20 static_text_title->setFixedHeight(16); |
21 | |
22 static_text_line = new QFrame(this); | |
23 static_text_line->setFrameShape(QFrame::HLine); | |
24 static_text_line->setFrameShadow(QFrame::Sunken); | |
25 static_text_line->setFixedHeight(2); | |
26 | |
64 | 27 layout->addWidget(static_text_title); |
28 layout->addWidget(static_text_line); | |
29 layout->setSpacing(0); | |
30 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 31 } |
32 | |
64 | 33 void Header::SetText(QString text) { |
34 static_text_title->setText(text); | |
46 | 35 } |
36 | |
75 | 37 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */ |
38 Paragraph::Paragraph(QString text, QWidget* parent) : QPlainTextEdit(text, parent) { | |
39 setTextInteractionFlags(Qt::TextBrowserInteraction); | |
40 setFrameShape(QFrame::NoFrame); | |
41 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
42 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
43 | |
44 QPalette pal; | |
45 pal.setColor(QPalette::Window, Qt::transparent); | |
46 setPalette(pal); | |
47 | |
48 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | |
49 } | |
50 | |
51 void Paragraph::SetText(QString text) { | |
52 QTextDocument* document = new QTextDocument(this); | |
53 document->setDocumentLayout(new QPlainTextDocumentLayout(document)); | |
54 document->setPlainText(text); | |
55 setDocument(document); | |
56 } | |
57 | |
58 /* highly based upon... some stackoverflow answer for PyQt */ | |
59 QSize Paragraph::minimumSizeHint() const { | |
60 return QSize(0, 0); | |
61 } | |
62 | |
63 QSize Paragraph::sizeHint() const { | |
64 QTextDocument* doc = document(); | |
65 doc->adjustSize(); | |
66 long h = 0; | |
67 for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) { | |
68 h += doc->documentLayout()->blockBoundingRect(line).height(); | |
69 } | |
70 return QSize(doc->size().width(), h); | |
71 } | |
72 | |
73 /* Equivalent to Paragraph(), but is only capable of showing one line. Only | |
74 exists because sometimes with SelectableSection it will let you go | |
75 out of bounds */ | |
76 Line::Line(QString text, QWidget* parent) : QLineEdit(text, parent) { | |
77 setFrame(false); | |
78 setReadOnly(true); | |
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) { | |
64 | 99 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 100 |
101 header = new Header(title, this); | |
102 | |
103 QWidget* content = new QWidget(this); | |
64 | 104 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 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 | |
64 | 111 content_layout->addWidget(paragraph); |
112 content_layout->setSpacing(0); | |
113 content_layout->setContentsMargins(0, 0, 0, 0); | |
46 | 114 content->setContentsMargins(12, 0, 0, 0); |
115 | |
64 | 116 layout->addWidget(header); |
117 layout->addWidget(paragraph); | |
118 layout->setSpacing(0); | |
119 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 120 } |
121 | |
75 | 122 Header* Section::GetHeader() { |
46 | 123 return header; |
124 } | |
125 | |
75 | 126 Paragraph* Section::GetParagraph() { |
46 | 127 return paragraph; |
128 } | |
129 | |
75 | 130 LabelledSection::LabelledSection(QString title, QString label, QString data, QWidget* parent) |
46 | 131 : QWidget(parent) { |
64 | 132 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 133 |
134 header = new Header(title, this); | |
135 | |
136 // this is not accessible from the object because there's really | |
137 // no reason to make it accessible... | |
138 QWidget* content = new QWidget(this); | |
139 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); | |
140 | |
141 labels = new Paragraph(label, this); | |
142 labels->setTextInteractionFlags(Qt::NoTextInteraction); | |
143 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
144 labels->setWordWrapMode(QTextOption::NoWrap); | |
145 labels->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); | |
146 | |
147 paragraph = new Paragraph(data, this); | |
148 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
149 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
150 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
151 | |
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
152 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 153 content_layout->addWidget(labels, 0, Qt::AlignTop); |
154 content_layout->addWidget(paragraph, 0, Qt::AlignTop); | |
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
155 content_layout->setSpacing(20); |
62 | 156 content_layout->setContentsMargins(0, 0, 0, 0); |
46 | 157 |
158 content->setContentsMargins(12, 0, 0, 0); | |
159 | |
64 | 160 layout->addWidget(header); |
161 layout->addWidget(content); | |
162 layout->setSpacing(0); | |
163 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 164 } |
165 | |
75 | 166 Header* LabelledSection::GetHeader() { |
46 | 167 return header; |
168 } | |
169 | |
75 | 170 Paragraph* LabelledSection::GetLabels() { |
46 | 171 return labels; |
172 } | |
173 | |
75 | 174 Paragraph* LabelledSection::GetParagraph() { |
46 | 175 return paragraph; |
176 } | |
177 | |
75 | 178 SelectableSection::SelectableSection(QString title, QString data, QWidget* parent) : QWidget(parent) { |
64 | 179 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 180 |
181 header = new Header(title, this); | |
182 | |
183 QWidget* content = new QWidget(this); | |
64 | 184 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 185 |
186 paragraph = new Paragraph(data, content); | |
187 | |
64 | 188 content_layout->addWidget(paragraph); |
189 content_layout->setSpacing(0); | |
190 content_layout->setContentsMargins(0, 0, 0, 0); | |
46 | 191 content->setContentsMargins(12, 0, 0, 0); |
192 | |
64 | 193 layout->addWidget(header); |
194 layout->addWidget(content); | |
195 layout->setSpacing(0); | |
196 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 197 } |
198 | |
75 | 199 Header* SelectableSection::GetHeader() { |
46 | 200 return header; |
201 } | |
202 | |
75 | 203 Paragraph* SelectableSection::GetParagraph() { |
46 | 204 return paragraph; |
205 } | |
206 | |
75 | 207 OneLineSection::OneLineSection(QString title, QString text, QWidget* parent) : QWidget(parent) { |
208 QVBoxLayout* layout = new QVBoxLayout(this); | |
209 | |
210 header = new Header(title, this); | |
46 | 211 |
75 | 212 QWidget* content = new QWidget(this); |
213 QHBoxLayout* content_layout = new QHBoxLayout(content); | |
214 | |
215 line = new Line(text, content); | |
46 | 216 |
75 | 217 content_layout->addWidget(line); |
218 content_layout->setSpacing(0); | |
219 content_layout->setContentsMargins(0, 0, 0, 0); | |
220 content->setContentsMargins(12, 0, 0, 0); | |
46 | 221 |
75 | 222 layout->addWidget(header); |
223 layout->addWidget(content); | |
224 layout->setSpacing(0); | |
225 layout->setContentsMargins(0, 0, 0, 0); | |
64 | 226 } |
227 | |
75 | 228 Header* OneLineSection::GetHeader() { |
229 return header; | |
46 | 230 } |
231 | |
75 | 232 Line* OneLineSection::GetLine() { |
233 return line; | |
46 | 234 } |
235 | |
236 } // namespace TextWidgets | |
237 | |
238 #include "gui/widgets/moc_text.cpp" |