Mercurial > minori
annotate src/gui/widgets/text.cc @ 187:9613d72b097e
*: multiple performance improvements
like marking `static const` when it makes sense...
date: change old stupid heap-based method to a structure which should
make copying the thing actually make a copy.
also many performance-based changes, like removing the std::tie
dependency and forward-declaring nlohmann json
*: replace every instance of QString::fromUtf8 to Strings::ToQString.
the main difference is that our function will always convert exactly
what is in the string, while some other times it would only convert
up to the nearest NUL byte
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Wed, 06 Dec 2023 13:43:54 -0500 |
| parents | 01d259b9c89f |
| children | 4d461ef7d424 |
| 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); | |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
17 |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
18 { |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
19 QFont font = static_text_title->font(); |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
20 font.setWeight(QFont::Bold); |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
21 static_text_title->setFont(font); |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
22 } |
| 46 | 23 |
| 24 static_text_line = new QFrame(this); | |
| 25 static_text_line->setFrameShape(QFrame::HLine); | |
| 26 static_text_line->setFrameShadow(QFrame::Sunken); | |
| 27 static_text_line->setFixedHeight(2); | |
| 28 | |
| 64 | 29 layout->addWidget(static_text_title); |
| 30 layout->addWidget(static_text_line); | |
| 31 layout->setSpacing(0); | |
| 32 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 33 } |
| 34 | |
| 83 | 35 void Header::SetText(const QString& text) { |
| 64 | 36 static_text_title->setText(text); |
| 83 | 37 updateGeometry(); |
| 46 | 38 } |
| 39 | |
| 75 | 40 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */ |
| 83 | 41 Paragraph::Paragraph(const QString& text, QWidget* parent) : QPlainTextEdit(text, parent) { |
| 75 | 42 setTextInteractionFlags(Qt::TextBrowserInteraction); |
| 43 setFrameShape(QFrame::NoFrame); | |
| 44 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 45 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 46 | |
| 47 QPalette pal; | |
| 86 | 48 pal.setColor(QPalette::Base, Qt::transparent); |
| 75 | 49 setPalette(pal); |
| 50 | |
| 51 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | |
| 52 } | |
| 53 | |
| 83 | 54 void Paragraph::SetText(const QString& text) { |
| 75 | 55 QTextDocument* document = new QTextDocument(this); |
| 56 document->setDocumentLayout(new QPlainTextDocumentLayout(document)); | |
| 57 document->setPlainText(text); | |
| 58 setDocument(document); | |
| 83 | 59 updateGeometry(); |
| 75 | 60 } |
| 61 | |
| 62 /* highly based upon... some stackoverflow answer for PyQt */ | |
| 63 QSize Paragraph::minimumSizeHint() const { | |
| 64 return QSize(0, 0); | |
| 65 } | |
| 66 | |
| 67 QSize Paragraph::sizeHint() const { | |
| 68 QTextDocument* doc = document(); | |
| 69 doc->adjustSize(); | |
| 70 long h = 0; | |
| 71 for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) { | |
| 72 h += doc->documentLayout()->blockBoundingRect(line).height(); | |
| 73 } | |
| 74 return QSize(doc->size().width(), h); | |
| 75 } | |
| 76 | |
| 77 /* 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
|
78 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
|
79 out of bounds and that looks really fugly for most things */ |
| 83 | 80 Line::Line(QWidget* parent) : QLineEdit(parent) { |
| 75 | 81 setFrame(false); |
| 82 setReadOnly(true); | |
|
87
4aef97f4d998
information: use QGridLayout please...
Paper <mrpapersonic@gmail.com>
parents:
86
diff
changeset
|
83 setCursor(Qt::IBeamCursor); |
| 75 | 84 |
| 85 QPalette pal; | |
|
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
86 pal.setColor(QPalette::Window, Qt::transparent); |
| 86 | 87 pal.setColor(QPalette::Base, Qt::transparent); |
| 75 | 88 setPalette(pal); |
| 89 | |
| 90 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 91 } | |
| 92 | |
| 83 | 93 Line::Line(const QString& text, QWidget* parent) : Line(parent) { |
| 94 SetText(text); | |
| 95 } | |
| 96 | |
| 97 void Line::SetText(const QString& text) { | |
| 98 setText(text); | |
| 99 setCursorPosition(0); /* displays left text first */ | |
| 100 } | |
| 101 | |
| 102 Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { | |
| 75 | 103 QFont fnt(font()); |
| 104 fnt.setPixelSize(16); | |
| 105 setFont(fnt); | |
| 106 | |
| 107 QPalette pal(palette()); | |
|
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
108 pal.setColor(QPalette::Text, pal.color(QPalette::Highlight)); |
| 75 | 109 setPalette(pal); |
| 110 } | |
| 111 | |
| 83 | 112 Section::Section(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
| 64 | 113 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 114 |
| 115 header = new Header(title, this); | |
| 116 | |
| 117 QWidget* content = new QWidget(this); | |
| 64 | 118 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 119 |
| 120 paragraph = new Paragraph(data, this); | |
| 121 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 122 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 123 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 124 | |
| 64 | 125 content_layout->addWidget(paragraph); |
| 126 content_layout->setSpacing(0); | |
| 127 content_layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 128 content->setContentsMargins(12, 0, 0, 0); |
| 129 | |
| 64 | 130 layout->addWidget(header); |
| 131 layout->addWidget(paragraph); | |
| 132 layout->setSpacing(0); | |
| 133 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 134 } |
| 135 | |
| 75 | 136 Header* Section::GetHeader() { |
| 46 | 137 return header; |
| 138 } | |
| 139 | |
| 75 | 140 Paragraph* Section::GetParagraph() { |
| 46 | 141 return paragraph; |
| 142 } | |
| 143 | |
| 83 | 144 LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) : QWidget(parent) { |
| 64 | 145 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 146 |
| 147 header = new Header(title, this); | |
| 148 | |
| 149 // this is not accessible from the object because there's really | |
| 150 // no reason to make it accessible... | |
| 151 QWidget* content = new QWidget(this); | |
| 83 | 152 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); |
| 46 | 153 |
| 154 labels = new Paragraph(label, this); | |
| 155 labels->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 156 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 157 labels->setWordWrapMode(QTextOption::NoWrap); | |
| 83 | 158 labels->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding); |
| 46 | 159 |
| 160 paragraph = new Paragraph(data, this); | |
| 161 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 162 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 163 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 83 | 164 paragraph->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 46 | 165 |
|
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
166 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 167 content_layout->addWidget(labels, 0, Qt::AlignTop); |
| 168 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
|
169 content_layout->setSpacing(20); |
| 62 | 170 content_layout->setContentsMargins(0, 0, 0, 0); |
| 46 | 171 |
| 172 content->setContentsMargins(12, 0, 0, 0); | |
| 173 | |
| 64 | 174 layout->addWidget(header); |
| 175 layout->addWidget(content); | |
| 176 layout->setSpacing(0); | |
| 177 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 178 } |
| 179 | |
| 75 | 180 Header* LabelledSection::GetHeader() { |
| 46 | 181 return header; |
| 182 } | |
| 183 | |
| 75 | 184 Paragraph* LabelledSection::GetLabels() { |
| 46 | 185 return labels; |
| 186 } | |
| 187 | |
| 75 | 188 Paragraph* LabelledSection::GetParagraph() { |
| 46 | 189 return paragraph; |
| 190 } | |
| 191 | |
| 83 | 192 SelectableSection::SelectableSection(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
| 64 | 193 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 194 |
| 195 header = new Header(title, this); | |
| 196 | |
| 197 QWidget* content = new QWidget(this); | |
| 64 | 198 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 199 |
| 200 paragraph = new Paragraph(data, content); | |
| 201 | |
| 64 | 202 content_layout->addWidget(paragraph); |
| 203 content_layout->setSpacing(0); | |
| 102 | 204 content_layout->setContentsMargins(12, 0, 0, 0); |
| 205 content->setContentsMargins(0, 0, 0, 0); | |
| 46 | 206 |
| 64 | 207 layout->addWidget(header); |
| 208 layout->addWidget(content); | |
| 209 layout->setSpacing(0); | |
| 210 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 211 } |
| 212 | |
| 75 | 213 Header* SelectableSection::GetHeader() { |
| 46 | 214 return header; |
| 215 } | |
| 216 | |
| 75 | 217 Paragraph* SelectableSection::GetParagraph() { |
| 46 | 218 return paragraph; |
| 219 } | |
| 220 | |
| 83 | 221 OneLineSection::OneLineSection(const QString& title, const QString& text, QWidget* parent) : QWidget(parent) { |
| 75 | 222 QVBoxLayout* layout = new QVBoxLayout(this); |
| 223 | |
| 224 header = new Header(title, this); | |
| 46 | 225 |
| 75 | 226 QWidget* content = new QWidget(this); |
| 227 QHBoxLayout* content_layout = new QHBoxLayout(content); | |
| 228 | |
| 229 line = new Line(text, content); | |
| 46 | 230 |
| 75 | 231 content_layout->addWidget(line); |
| 232 content_layout->setSpacing(0); | |
| 233 content_layout->setContentsMargins(0, 0, 0, 0); | |
| 234 content->setContentsMargins(12, 0, 0, 0); | |
| 46 | 235 |
| 75 | 236 layout->addWidget(header); |
| 237 layout->addWidget(content); | |
| 238 layout->setSpacing(0); | |
| 239 layout->setContentsMargins(0, 0, 0, 0); | |
| 64 | 240 } |
| 241 | |
| 75 | 242 Header* OneLineSection::GetHeader() { |
| 243 return header; | |
| 46 | 244 } |
| 245 | |
| 75 | 246 Line* OneLineSection::GetLine() { |
| 247 return line; | |
| 46 | 248 } |
| 249 | |
| 250 } // namespace TextWidgets | |
| 251 | |
| 252 #include "gui/widgets/moc_text.cpp" |
