Mercurial > minori
annotate src/gui/widgets/text.cc @ 291:9a88e1725fd2
*: refactor lots of stuff
I forgot to put this into different commits, oops!
anyway, it doesn't really matter *that* much since this is an
unfinished hobby project anyway. once it starts getting stable
commit history will be more important, but for now it's not
that big of a deal
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 12 May 2024 16:31:07 -0400 |
parents | 862d0d8619f6 |
children | b82841e76e79 |
rev | line source |
---|---|
46 | 1 #include "gui/widgets/text.h" |
2 #include "core/session.h" | |
253 | 3 |
77 | 4 #include <QDebug> |
46 | 5 #include <QFrame> |
6 #include <QLabel> | |
7 #include <QTextBlock> | |
8 #include <QVBoxLayout> | |
291 | 9 #include <QScrollArea> |
10 #include <QDebug> | |
11 | |
12 /* WARNING: GARBAGE CODE FOLLOWS | |
13 * | |
14 * This file is filled with spaghetti to make this | |
15 * stupid text render how I want it to. | |
16 * | |
17 * many cases of hacking with setSizePolicy() are seen | |
18 * all around this file. Edit it only if really | |
19 * necessary, please. | |
20 */ | |
46 | 21 |
22 namespace TextWidgets { | |
23 | |
291 | 24 Header::Header(const QString& title, QWidget* parent) |
25 : QWidget(parent) | |
26 , static_text_title(title) { | |
64 | 27 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 28 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
29 | |
291 | 30 static_text_title.setTextFormat(Qt::PlainText); |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
31 |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
32 { |
291 | 33 QFont font = static_text_title.font(); |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
34 font.setWeight(QFont::Bold); |
291 | 35 static_text_title.setFont(font); |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
36 } |
46 | 37 |
291 | 38 static_text_line.setFrameShape(QFrame::HLine); |
39 static_text_line.setFrameShadow(QFrame::Sunken); | |
40 static_text_line.setFixedHeight(2); | |
46 | 41 |
291 | 42 layout->addWidget(&static_text_title); |
43 layout->addWidget(&static_text_line); | |
64 | 44 layout->setSpacing(0); |
45 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 46 } |
47 | |
83 | 48 void Header::SetText(const QString& text) { |
291 | 49 static_text_title.setText(text); |
83 | 50 updateGeometry(); |
46 | 51 } |
52 | |
291 | 53 Paragraph::Paragraph(const QString& text, QWidget* parent) : QWidget(parent) { |
54 /* meh */ | |
55 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); | |
56 | |
57 QVBoxLayout* layout = new QVBoxLayout(this); | |
58 layout->setSpacing(0); | |
59 layout->setContentsMargins(0, 0, 0, 0); | |
60 | |
61 text_edit.setTextInteractionFlags(Qt::TextBrowserInteraction); | |
62 text_edit.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
63 text_edit.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
64 text_edit.setStyleSheet("background: transparent;"); | |
65 | |
66 text_edit.document()->setDocumentMargin(0); | |
75 | 67 |
291 | 68 SetText(text); |
69 | |
70 layout->addWidget(&text_edit); | |
71 } | |
72 | |
73 void Paragraph::SetText(const QString& text) { | |
74 text_edit.document()->setPlainText(text); | |
75 | |
76 /* return the view to the start */ | |
77 QTextCursor cursor = text_edit.textCursor(); | |
78 cursor.setPosition(0); | |
79 text_edit.setTextCursor(cursor); | |
80 } | |
81 | |
82 QSize Paragraph::minimumSizeHint() const { | |
83 return QSize(0, 0); | |
75 | 84 } |
85 | |
291 | 86 /* highly based upon... some stackoverflow answer for PyQt */ |
87 QSize Paragraph::sizeHint() const { | |
88 QTextDocument* doc = text_edit.document(); | |
89 doc->adjustSize(); | |
90 | |
91 long h = 0; | |
92 for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) | |
93 h += doc->documentLayout()->blockBoundingRect(line).height(); | |
94 | |
95 return QSize(doc->size().width(), h); | |
96 } | |
97 | |
98 QPlainTextEdit* Paragraph::GetLabel() { | |
99 return &text_edit; | |
75 | 100 } |
101 | |
291 | 102 Line::Line(QWidget* parent) : QWidget(parent) { |
103 QVBoxLayout* layout = new QVBoxLayout(this); | |
104 layout->setSpacing(0); | |
105 layout->setContentsMargins(0, 0, 0, 0); | |
106 | |
107 line_edit_.setReadOnly(true); | |
108 line_edit_.setFrame(false); | |
109 line_edit_.setStyleSheet("background: transparent;"); | |
110 | |
111 layout->addWidget(&line_edit_); | |
75 | 112 } |
113 | |
291 | 114 Line::Line(const QString& text, QWidget* parent) : Line(parent) { |
115 SetText(text); | |
83 | 116 } |
117 | |
118 void Line::SetText(const QString& text) { | |
291 | 119 line_edit_.setText(text); |
120 line_edit_.setCursorPosition(0); | |
83 | 121 } |
122 | |
123 Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { | |
291 | 124 QFont fnt(line_edit_.font()); |
75 | 125 fnt.setPixelSize(16); |
291 | 126 line_edit_.setFont(fnt); |
75 | 127 |
291 | 128 line_edit_.setForegroundRole(QPalette::Highlight); |
75 | 129 } |
130 | |
291 | 131 Section::Section(const QString& title, const QString& data, QWidget* parent) |
132 : QWidget(parent) { | |
64 | 133 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 134 |
135 header = new Header(title, this); | |
136 | |
137 QWidget* content = new QWidget(this); | |
64 | 138 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 139 |
140 paragraph = new Paragraph(data, this); | |
291 | 141 paragraph->GetLabel()->setTextInteractionFlags(Qt::NoTextInteraction); |
142 paragraph->GetLabel()->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
143 paragraph->GetLabel()->setWordWrapMode(QTextOption::NoWrap); | |
46 | 144 |
64 | 145 content_layout->addWidget(paragraph); |
146 content_layout->setSpacing(0); | |
147 content_layout->setContentsMargins(0, 0, 0, 0); | |
46 | 148 content->setContentsMargins(12, 0, 0, 0); |
149 | |
64 | 150 layout->addWidget(header); |
151 layout->addWidget(paragraph); | |
152 layout->setSpacing(0); | |
153 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 154 } |
155 | |
75 | 156 Header* Section::GetHeader() { |
46 | 157 return header; |
158 } | |
159 | |
75 | 160 Paragraph* Section::GetParagraph() { |
46 | 161 return paragraph; |
162 } | |
163 | |
291 | 164 /* despite being named a "labelled paragraph" this uses QLabels for simplicity */ |
165 LabelledParagraph::LabelledParagraph(const QString& label, const QString& data, QWidget* parent) | |
166 : QWidget(parent) | |
167 , labels_(label) | |
168 , data_(data) { | |
253 | 169 QHBoxLayout* ly = new QHBoxLayout(this); |
170 | |
291 | 171 labels_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); |
172 data_.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | |
253 | 173 |
291 | 174 ly->addWidget(&labels_, 0, Qt::AlignTop); |
175 ly->addWidget(&data_, 0, Qt::AlignTop); | |
253 | 176 ly->setSpacing(20); |
177 ly->setContentsMargins(0, 0, 0, 0); | |
178 } | |
179 | |
291 | 180 QLabel* LabelledParagraph::GetLabels() { |
181 return &labels_; | |
253 | 182 } |
183 | |
291 | 184 QLabel* LabelledParagraph::GetData() { |
185 return &data_; | |
186 } | |
187 | |
188 QLabel* LabelledParagraph::GetParagraph() { | |
189 return GetData(); | |
253 | 190 } |
191 | |
258 | 192 LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) |
193 : QWidget(parent) { | |
64 | 194 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 195 |
196 header = new Header(title, this); | |
197 | |
198 // this is not accessible from the object because there's really | |
199 // no reason to make it accessible... | |
253 | 200 content = new LabelledParagraph(label, data, this); |
291 | 201 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
46 | 202 content->setContentsMargins(12, 0, 0, 0); |
203 | |
64 | 204 layout->addWidget(header); |
205 layout->addWidget(content); | |
206 layout->setSpacing(0); | |
207 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 208 } |
209 | |
75 | 210 Header* LabelledSection::GetHeader() { |
46 | 211 return header; |
212 } | |
213 | |
291 | 214 QLabel* LabelledSection::GetLabels() { |
253 | 215 return content->GetLabels(); |
46 | 216 } |
217 | |
291 | 218 QLabel* LabelledSection::GetData() { |
219 return content->GetData(); | |
220 } | |
221 | |
222 QLabel* LabelledSection::GetParagraph() { | |
253 | 223 return content->GetParagraph(); |
46 | 224 } |
225 | |
83 | 226 SelectableSection::SelectableSection(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
64 | 227 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 228 |
229 header = new Header(title, this); | |
230 | |
231 QWidget* content = new QWidget(this); | |
64 | 232 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 233 |
234 paragraph = new Paragraph(data, content); | |
235 | |
64 | 236 content_layout->addWidget(paragraph); |
237 content_layout->setSpacing(0); | |
102 | 238 content_layout->setContentsMargins(12, 0, 0, 0); |
239 content->setContentsMargins(0, 0, 0, 0); | |
46 | 240 |
64 | 241 layout->addWidget(header); |
242 layout->addWidget(content); | |
243 layout->setSpacing(0); | |
244 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 245 } |
246 | |
75 | 247 Header* SelectableSection::GetHeader() { |
46 | 248 return header; |
249 } | |
250 | |
75 | 251 Paragraph* SelectableSection::GetParagraph() { |
46 | 252 return paragraph; |
253 } | |
254 | |
83 | 255 OneLineSection::OneLineSection(const QString& title, const QString& text, QWidget* parent) : QWidget(parent) { |
75 | 256 QVBoxLayout* layout = new QVBoxLayout(this); |
257 | |
258 header = new Header(title, this); | |
46 | 259 |
75 | 260 QWidget* content = new QWidget(this); |
261 QHBoxLayout* content_layout = new QHBoxLayout(content); | |
262 | |
263 line = new Line(text, content); | |
46 | 264 |
75 | 265 content_layout->addWidget(line); |
266 content_layout->setSpacing(0); | |
267 content_layout->setContentsMargins(0, 0, 0, 0); | |
268 content->setContentsMargins(12, 0, 0, 0); | |
46 | 269 |
75 | 270 layout->addWidget(header); |
271 layout->addWidget(content); | |
272 layout->setSpacing(0); | |
273 layout->setContentsMargins(0, 0, 0, 0); | |
64 | 274 } |
275 | |
75 | 276 Header* OneLineSection::GetHeader() { |
277 return header; | |
46 | 278 } |
279 | |
75 | 280 Line* OneLineSection::GetLine() { |
281 return line; | |
46 | 282 } |
283 | |
284 } // namespace TextWidgets |