Mercurial > minori
annotate src/gui/widgets/text.cc @ 137:69db40272acd
dep/animia: [WIP] huge refactor
this WILL NOT compile, because lots of code has been changed
and every API in the original codebase has been removed.
note that this api setup is not exactly permanent...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 10 Nov 2023 13:52:47 -0500 |
parents | 6d8da6e64d61 |
children | 01d259b9c89f |
rev | line source |
---|---|
46 | 1 #include "gui/widgets/text.h" |
2 #include "core/session.h" | |
77 | 3 #include <QDebug> |
46 | 4 #include <QFrame> |
5 #include <QLabel> | |
6 #include <QTextBlock> | |
7 #include <QVBoxLayout> | |
8 | |
9 namespace TextWidgets { | |
10 | |
83 | 11 Header::Header(const QString& title, QWidget* parent) : QWidget(parent) { |
64 | 12 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 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 | |
64 | 26 layout->addWidget(static_text_title); |
27 layout->addWidget(static_text_line); | |
28 layout->setSpacing(0); | |
29 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 30 } |
31 | |
83 | 32 void Header::SetText(const QString& text) { |
64 | 33 static_text_title->setText(text); |
83 | 34 updateGeometry(); |
46 | 35 } |
36 | |
75 | 37 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */ |
83 | 38 Paragraph::Paragraph(const QString& text, QWidget* parent) : QPlainTextEdit(text, parent) { |
75 | 39 setTextInteractionFlags(Qt::TextBrowserInteraction); |
40 setFrameShape(QFrame::NoFrame); | |
41 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
42 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
43 | |
44 QPalette pal; | |
86 | 45 pal.setColor(QPalette::Base, Qt::transparent); |
75 | 46 setPalette(pal); |
47 | |
48 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | |
49 } | |
50 | |
83 | 51 void Paragraph::SetText(const QString& text) { |
75 | 52 QTextDocument* document = new QTextDocument(this); |
53 document->setDocumentLayout(new QPlainTextDocumentLayout(document)); | |
54 document->setPlainText(text); | |
55 setDocument(document); | |
83 | 56 updateGeometry(); |
75 | 57 } |
58 | |
59 /* highly based upon... some stackoverflow answer for PyQt */ | |
60 QSize Paragraph::minimumSizeHint() const { | |
61 return QSize(0, 0); | |
62 } | |
63 | |
64 QSize Paragraph::sizeHint() const { | |
65 QTextDocument* doc = document(); | |
66 doc->adjustSize(); | |
67 long h = 0; | |
68 for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) { | |
69 h += doc->documentLayout()->blockBoundingRect(line).height(); | |
70 } | |
71 return QSize(doc->size().width(), h); | |
72 } | |
73 | |
74 /* Equivalent to Paragraph(), but is only capable of showing one line. Only | |
97
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
87
diff
changeset
|
75 exists because with SelectableSection it will let you go |
18979b066284
animia/unix: fix a bunch of stuff that breaks OS X things
Paper <mrpapersonic@gmail.com>
parents:
87
diff
changeset
|
76 out of bounds and that looks really fugly for most things */ |
83 | 77 Line::Line(QWidget* parent) : QLineEdit(parent) { |
75 | 78 setFrame(false); |
79 setReadOnly(true); | |
87
4aef97f4d998
information: use QGridLayout please...
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
80 setCursor(Qt::IBeamCursor); |
75 | 81 |
82 QPalette pal; | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
83 pal.setColor(QPalette::Window, Qt::transparent); |
86 | 84 pal.setColor(QPalette::Base, Qt::transparent); |
75 | 85 setPalette(pal); |
86 | |
87 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
88 } | |
89 | |
83 | 90 Line::Line(const QString& text, QWidget* parent) : Line(parent) { |
91 SetText(text); | |
92 } | |
93 | |
94 void Line::SetText(const QString& text) { | |
95 setText(text); | |
96 setCursorPosition(0); /* displays left text first */ | |
97 } | |
98 | |
99 Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { | |
75 | 100 QFont fnt(font()); |
101 fnt.setPixelSize(16); | |
102 setFont(fnt); | |
103 | |
104 QPalette pal(palette()); | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
105 pal.setColor(QPalette::Text, pal.color(QPalette::Highlight)); |
75 | 106 setPalette(pal); |
107 } | |
108 | |
83 | 109 Section::Section(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
64 | 110 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 111 |
112 header = new Header(title, this); | |
113 | |
114 QWidget* content = new QWidget(this); | |
64 | 115 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 116 |
117 paragraph = new Paragraph(data, this); | |
118 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
119 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
120 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
121 | |
64 | 122 content_layout->addWidget(paragraph); |
123 content_layout->setSpacing(0); | |
124 content_layout->setContentsMargins(0, 0, 0, 0); | |
46 | 125 content->setContentsMargins(12, 0, 0, 0); |
126 | |
64 | 127 layout->addWidget(header); |
128 layout->addWidget(paragraph); | |
129 layout->setSpacing(0); | |
130 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 131 } |
132 | |
75 | 133 Header* Section::GetHeader() { |
46 | 134 return header; |
135 } | |
136 | |
75 | 137 Paragraph* Section::GetParagraph() { |
46 | 138 return paragraph; |
139 } | |
140 | |
83 | 141 LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) : QWidget(parent) { |
64 | 142 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 143 |
144 header = new Header(title, this); | |
145 | |
146 // this is not accessible from the object because there's really | |
147 // no reason to make it accessible... | |
148 QWidget* content = new QWidget(this); | |
83 | 149 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); |
46 | 150 |
151 labels = new Paragraph(label, this); | |
152 labels->setTextInteractionFlags(Qt::NoTextInteraction); | |
153 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
154 labels->setWordWrapMode(QTextOption::NoWrap); | |
83 | 155 labels->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding); |
46 | 156 |
157 paragraph = new Paragraph(data, this); | |
158 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
159 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
160 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
83 | 161 paragraph->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
46 | 162 |
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
163 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 164 content_layout->addWidget(labels, 0, Qt::AlignTop); |
165 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
|
166 content_layout->setSpacing(20); |
62 | 167 content_layout->setContentsMargins(0, 0, 0, 0); |
46 | 168 |
169 content->setContentsMargins(12, 0, 0, 0); | |
170 | |
64 | 171 layout->addWidget(header); |
172 layout->addWidget(content); | |
173 layout->setSpacing(0); | |
174 layout->setContentsMargins(0, 0, 0, 0); | |
46 | 175 } |
176 | |
75 | 177 Header* LabelledSection::GetHeader() { |
46 | 178 return header; |
179 } | |
180 | |
75 | 181 Paragraph* LabelledSection::GetLabels() { |
46 | 182 return labels; |
183 } | |
184 | |
75 | 185 Paragraph* LabelledSection::GetParagraph() { |
46 | 186 return paragraph; |
187 } | |
188 | |
83 | 189 SelectableSection::SelectableSection(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
64 | 190 QVBoxLayout* layout = new QVBoxLayout(this); |
46 | 191 |
192 header = new Header(title, this); | |
193 | |
194 QWidget* content = new QWidget(this); | |
64 | 195 QHBoxLayout* content_layout = new QHBoxLayout(content); |
46 | 196 |
197 paragraph = new Paragraph(data, content); | |
198 | |
64 | 199 content_layout->addWidget(paragraph); |
200 content_layout->setSpacing(0); | |
102 | 201 content_layout->setContentsMargins(12, 0, 0, 0); |
202 content->setContentsMargins(0, 0, 0, 0); | |
46 | 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* SelectableSection::GetHeader() { |
46 | 211 return header; |
212 } | |
213 | |
75 | 214 Paragraph* SelectableSection::GetParagraph() { |
46 | 215 return paragraph; |
216 } | |
217 | |
83 | 218 OneLineSection::OneLineSection(const QString& title, const QString& text, QWidget* parent) : QWidget(parent) { |
75 | 219 QVBoxLayout* layout = new QVBoxLayout(this); |
220 | |
221 header = new Header(title, this); | |
46 | 222 |
75 | 223 QWidget* content = new QWidget(this); |
224 QHBoxLayout* content_layout = new QHBoxLayout(content); | |
225 | |
226 line = new Line(text, content); | |
46 | 227 |
75 | 228 content_layout->addWidget(line); |
229 content_layout->setSpacing(0); | |
230 content_layout->setContentsMargins(0, 0, 0, 0); | |
231 content->setContentsMargins(12, 0, 0, 0); | |
46 | 232 |
75 | 233 layout->addWidget(header); |
234 layout->addWidget(content); | |
235 layout->setSpacing(0); | |
236 layout->setContentsMargins(0, 0, 0, 0); | |
64 | 237 } |
238 | |
75 | 239 Header* OneLineSection::GetHeader() { |
240 return header; | |
46 | 241 } |
242 | |
75 | 243 Line* OneLineSection::GetLine() { |
244 return line; | |
46 | 245 } |
246 | |
247 } // namespace TextWidgets | |
248 | |
249 #include "gui/widgets/moc_text.cpp" |