Mercurial > minori
annotate src/gui/widgets/text.cc @ 339:eac06513db86
dialog/about: add licenses button
otherwise we wouldn't be complying with most of the libraries
and external software we use
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 19 Jun 2024 14:02:11 -0400 |
parents | b82841e76e79 |
children | 6b0768158dcd |
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;"); | |
295 | 65 text_edit.setFrameStyle(QFrame::NoFrame); |
291 | 66 |
67 text_edit.document()->setDocumentMargin(0); | |
75 | 68 |
291 | 69 SetText(text); |
70 | |
71 layout->addWidget(&text_edit); | |
72 } | |
73 | |
74 void Paragraph::SetText(const QString& text) { | |
75 text_edit.document()->setPlainText(text); | |
76 | |
77 /* return the view to the start */ | |
78 QTextCursor cursor = text_edit.textCursor(); | |
79 cursor.setPosition(0); | |
80 text_edit.setTextCursor(cursor); | |
81 } | |
82 | |
295 | 83 bool Paragraph::hasHeightForWidth() const { |
84 return true; | |
75 | 85 } |
86 | |
295 | 87 int Paragraph::heightForWidth(int w) const { |
291 | 88 QTextDocument* doc = text_edit.document(); |
295 | 89 doc->setTextWidth(w); |
291 | 90 |
295 | 91 return doc->size().toSize().height(); |
291 | 92 } |
93 | |
94 QPlainTextEdit* Paragraph::GetLabel() { | |
95 return &text_edit; | |
75 | 96 } |
97 | |
291 | 98 Line::Line(QWidget* parent) : QWidget(parent) { |
99 QVBoxLayout* layout = new QVBoxLayout(this); | |
100 layout->setSpacing(0); | |
101 layout->setContentsMargins(0, 0, 0, 0); | |
102 | |
103 line_edit_.setReadOnly(true); | |
104 line_edit_.setFrame(false); | |
105 line_edit_.setStyleSheet("background: transparent;"); | |
106 | |
107 layout->addWidget(&line_edit_); | |
75 | 108 } |
109 | |
291 | 110 Line::Line(const QString& text, QWidget* parent) : Line(parent) { |
111 SetText(text); | |
83 | 112 } |
113 | |
114 void Line::SetText(const QString& text) { | |
291 | 115 line_edit_.setText(text); |
116 line_edit_.setCursorPosition(0); | |
83 | 117 } |
118 | |
119 Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { | |
291 | 120 QFont fnt(line_edit_.font()); |
75 | 121 fnt.setPixelSize(16); |
291 | 122 line_edit_.setFont(fnt); |
75 | 123 |
291 | 124 line_edit_.setForegroundRole(QPalette::Highlight); |
75 | 125 } |
126 | |
291 | 127 Section::Section(const QString& title, const QString& data, QWidget* parent) |
128 : QWidget(parent) { | |
64 | 129 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 130 |
131 header = new Header(title, this); | |
132 | |
133 QWidget* content = new QWidget(this); | |
64 | 134 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 135 |
136 paragraph = new Paragraph(data, this); | |
291 | 137 paragraph->GetLabel()->setTextInteractionFlags(Qt::NoTextInteraction); |
138 paragraph->GetLabel()->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
139 paragraph->GetLabel()->setWordWrapMode(QTextOption::NoWrap); | |
46 | 140 |
64 | 141 content_layout->addWidget(paragraph); |
142 content_layout->setSpacing(0); | |
143 content_layout->setContentsMargins(0, 0, 0, 0); | |
46 | 144 content->setContentsMargins(12, 0, 0, 0); |
145 | |
64 | 146 layout->addWidget(header); |
147 layout->addWidget(paragraph); | |
148 layout->setSpacing(0); | |
149 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 150 } |
151 | |
75 | 152 Header* Section::GetHeader() { |
46 | 153 return header; |
154 } | |
155 | |
75 | 156 Paragraph* Section::GetParagraph() { |
46 | 157 return paragraph; |
158 } | |
159 | |
291 | 160 /* despite being named a "labelled paragraph" this uses QLabels for simplicity */ |
161 LabelledParagraph::LabelledParagraph(const QString& label, const QString& data, QWidget* parent) | |
162 : QWidget(parent) | |
163 , labels_(label) | |
164 , data_(data) { | |
253 | 165 QHBoxLayout* ly = new QHBoxLayout(this); |
166 | |
291 | 167 labels_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); |
168 data_.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | |
253 | 169 |
291 | 170 ly->addWidget(&labels_, 0, Qt::AlignTop); |
171 ly->addWidget(&data_, 0, Qt::AlignTop); | |
253 | 172 ly->setSpacing(20); |
173 ly->setContentsMargins(0, 0, 0, 0); | |
174 } | |
175 | |
291 | 176 QLabel* LabelledParagraph::GetLabels() { |
177 return &labels_; | |
253 | 178 } |
179 | |
291 | 180 QLabel* LabelledParagraph::GetData() { |
181 return &data_; | |
182 } | |
183 | |
184 QLabel* LabelledParagraph::GetParagraph() { | |
185 return GetData(); | |
253 | 186 } |
187 | |
258 | 188 LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) |
189 : QWidget(parent) { | |
64 | 190 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 191 |
192 header = new Header(title, this); | |
193 | |
194 // this is not accessible from the object because there's really | |
195 // no reason to make it accessible... | |
253 | 196 content = new LabelledParagraph(label, data, this); |
291 | 197 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
46 | 198 content->setContentsMargins(12, 0, 0, 0); |
199 | |
64 | 200 layout->addWidget(header); |
201 layout->addWidget(content); | |
202 layout->setSpacing(0); | |
203 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 204 } |
205 | |
75 | 206 Header* LabelledSection::GetHeader() { |
46 | 207 return header; |
208 } | |
209 | |
291 | 210 QLabel* LabelledSection::GetLabels() { |
253 | 211 return content->GetLabels(); |
46 | 212 } |
213 | |
291 | 214 QLabel* LabelledSection::GetData() { |
215 return content->GetData(); | |
216 } | |
217 | |
218 QLabel* LabelledSection::GetParagraph() { | |
253 | 219 return content->GetParagraph(); |
46 | 220 } |
221 | |
83 | 222 SelectableSection::SelectableSection(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
64 | 223 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 224 |
225 header = new Header(title, this); | |
226 | |
227 QWidget* content = new QWidget(this); | |
64 | 228 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 229 |
230 paragraph = new Paragraph(data, content); | |
231 | |
64 | 232 content_layout->addWidget(paragraph); |
233 content_layout->setSpacing(0); | |
102 | 234 content_layout->setContentsMargins(12, 0, 0, 0); |
235 content->setContentsMargins(0, 0, 0, 0); | |
46 | 236 |
64 | 237 layout->addWidget(header); |
238 layout->addWidget(content); | |
239 layout->setSpacing(0); | |
240 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 241 } |
242 | |
75 | 243 Header* SelectableSection::GetHeader() { |
46 | 244 return header; |
245 } | |
246 | |
75 | 247 Paragraph* SelectableSection::GetParagraph() { |
46 | 248 return paragraph; |
249 } | |
250 | |
83 | 251 OneLineSection::OneLineSection(const QString& title, const QString& text, QWidget* parent) : QWidget(parent) { |
75 | 252 QVBoxLayout* layout = new QVBoxLayout(this); |
253 | |
254 header = new Header(title, this); | |
46 | 255 |
75 | 256 QWidget* content = new QWidget(this); |
257 QHBoxLayout* content_layout = new QHBoxLayout(content); | |
258 | |
259 line = new Line(text, content); | |
46 | 260 |
75 | 261 content_layout->addWidget(line); |
262 content_layout->setSpacing(0); | |
263 content_layout->setContentsMargins(0, 0, 0, 0); | |
264 content->setContentsMargins(12, 0, 0, 0); | |
46 | 265 |
75 | 266 layout->addWidget(header); |
267 layout->addWidget(content); | |
268 layout->setSpacing(0); | |
269 layout->setContentsMargins(0, 0, 0, 0); | |
64 | 270 } |
271 | |
75 | 272 Header* OneLineSection::GetHeader() { |
273 return header; | |
46 | 274 } |
275 | |
75 | 276 Line* OneLineSection::GetLine() { |
277 return line; | |
46 | 278 } |
279 | |
280 } // namespace TextWidgets |