Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 290:9347e2eaf6e5 | 291:9a88e1725fd2 |
|---|---|
| 4 #include <QDebug> | 4 #include <QDebug> |
| 5 #include <QFrame> | 5 #include <QFrame> |
| 6 #include <QLabel> | 6 #include <QLabel> |
| 7 #include <QTextBlock> | 7 #include <QTextBlock> |
| 8 #include <QVBoxLayout> | 8 #include <QVBoxLayout> |
| 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 */ | |
| 9 | 21 |
| 10 namespace TextWidgets { | 22 namespace TextWidgets { |
| 11 | 23 |
| 12 Header::Header(const QString& title, QWidget* parent) : QWidget(parent) { | 24 Header::Header(const QString& title, QWidget* parent) |
| 25 : QWidget(parent) | |
| 26 , static_text_title(title) { | |
| 13 QVBoxLayout* layout = new QVBoxLayout(this); | 27 QVBoxLayout* layout = new QVBoxLayout(this); |
| 14 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | 28 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
| 15 | 29 |
| 16 static_text_title = new QLabel(title, this); | 30 static_text_title.setTextFormat(Qt::PlainText); |
| 17 static_text_title->setTextFormat(Qt::PlainText); | |
| 18 | 31 |
| 19 { | 32 { |
| 20 QFont font = static_text_title->font(); | 33 QFont font = static_text_title.font(); |
| 21 font.setWeight(QFont::Bold); | 34 font.setWeight(QFont::Bold); |
| 22 static_text_title->setFont(font); | 35 static_text_title.setFont(font); |
| 23 } | 36 } |
| 24 | 37 |
| 25 static_text_line = new QFrame(this); | 38 static_text_line.setFrameShape(QFrame::HLine); |
| 26 static_text_line->setFrameShape(QFrame::HLine); | 39 static_text_line.setFrameShadow(QFrame::Sunken); |
| 27 static_text_line->setFrameShadow(QFrame::Sunken); | 40 static_text_line.setFixedHeight(2); |
| 28 static_text_line->setFixedHeight(2); | 41 |
| 29 | 42 layout->addWidget(&static_text_title); |
| 30 layout->addWidget(static_text_title); | 43 layout->addWidget(&static_text_line); |
| 31 layout->addWidget(static_text_line); | |
| 32 layout->setSpacing(0); | 44 layout->setSpacing(0); |
| 33 layout->setContentsMargins(0, 0, 0, 0); | 45 layout->setContentsMargins(0, 0, 0, 0); |
| 34 } | 46 } |
| 35 | 47 |
| 36 void Header::SetText(const QString& text) { | 48 void Header::SetText(const QString& text) { |
| 37 static_text_title->setText(text); | 49 static_text_title.setText(text); |
| 38 updateGeometry(); | 50 updateGeometry(); |
| 39 } | 51 } |
| 40 | 52 |
| 41 /* for now, this is a QLabel with a couple of default settings. | 53 Paragraph::Paragraph(const QString& text, QWidget* parent) : QWidget(parent) { |
| 42 * | 54 /* meh */ |
| 43 * eventually I'll have to implement this as a QScrollArea, just in case | 55 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 44 * some random text decides to overflow or something. | 56 |
| 45 */ | 57 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 Paragraph::Paragraph(const QString& text, QWidget* parent) : QLabel(text, parent) { | 58 layout->setSpacing(0); |
| 47 setTextInteractionFlags(Qt::TextBrowserInteraction); | 59 layout->setContentsMargins(0, 0, 0, 0); |
| 48 setFrameShape(QFrame::NoFrame); | 60 |
| 49 setCursor(Qt::IBeamCursor); /* emulate Taiga */ | 61 text_edit.setTextInteractionFlags(Qt::TextBrowserInteraction); |
| 50 setWordWrap(true); | 62 text_edit.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 51 | 63 text_edit.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 52 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); | 64 text_edit.setStyleSheet("background: transparent;"); |
| 53 } | 65 |
| 54 | 66 text_edit.document()->setDocumentMargin(0); |
| 55 /* kept here for legacy reasons, see explanation above */ | 67 |
| 68 SetText(text); | |
| 69 | |
| 70 layout->addWidget(&text_edit); | |
| 71 } | |
| 72 | |
| 56 void Paragraph::SetText(const QString& text) { | 73 void Paragraph::SetText(const QString& text) { |
| 57 setText(text); | 74 text_edit.document()->setPlainText(text); |
| 58 } | 75 |
| 59 | 76 /* return the view to the start */ |
| 60 /* Equivalent to Paragraph(), but disables word wrap. */ | 77 QTextCursor cursor = text_edit.textCursor(); |
| 61 Line::Line(QWidget* parent) : Paragraph("", parent) { | 78 cursor.setPosition(0); |
| 62 setWordWrap(false); | 79 text_edit.setTextCursor(cursor); |
| 63 } | 80 } |
| 64 | 81 |
| 65 Line::Line(const QString& text, QWidget* parent) : Paragraph(text, parent) { | 82 QSize Paragraph::minimumSizeHint() const { |
| 66 setWordWrap(false); | 83 return QSize(0, 0); |
| 67 } | 84 } |
| 68 | 85 |
| 69 /* legacy function, don't use in new code */ | 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; | |
| 100 } | |
| 101 | |
| 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_); | |
| 112 } | |
| 113 | |
| 114 Line::Line(const QString& text, QWidget* parent) : Line(parent) { | |
| 115 SetText(text); | |
| 116 } | |
| 117 | |
| 70 void Line::SetText(const QString& text) { | 118 void Line::SetText(const QString& text) { |
| 71 setText(text); | 119 line_edit_.setText(text); |
| 120 line_edit_.setCursorPosition(0); | |
| 72 } | 121 } |
| 73 | 122 |
| 74 Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { | 123 Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { |
| 75 QFont fnt(font()); | 124 QFont fnt(line_edit_.font()); |
| 76 fnt.setPixelSize(16); | 125 fnt.setPixelSize(16); |
| 77 setFont(fnt); | 126 line_edit_.setFont(fnt); |
| 78 | 127 |
| 79 QPalette pal(palette()); | 128 line_edit_.setForegroundRole(QPalette::Highlight); |
| 80 pal.setColor(QPalette::Text, pal.color(QPalette::Highlight)); | 129 } |
| 81 setPalette(pal); | 130 |
| 82 } | 131 Section::Section(const QString& title, const QString& data, QWidget* parent) |
| 83 | 132 : QWidget(parent) { |
| 84 Section::Section(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { | |
| 85 QVBoxLayout* layout = new QVBoxLayout(this); | 133 QVBoxLayout* layout = new QVBoxLayout(this); |
| 86 | 134 |
| 87 header = new Header(title, this); | 135 header = new Header(title, this); |
| 88 | 136 |
| 89 QWidget* content = new QWidget(this); | 137 QWidget* content = new QWidget(this); |
| 90 QHBoxLayout* content_layout = new QHBoxLayout(content); | 138 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 91 | 139 |
| 92 paragraph = new Paragraph(data, this); | 140 paragraph = new Paragraph(data, this); |
| 93 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | 141 paragraph->GetLabel()->setTextInteractionFlags(Qt::NoTextInteraction); |
| 94 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | 142 paragraph->GetLabel()->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); |
| 95 paragraph->setWordWrap(QTextOption::NoWrap); | 143 paragraph->GetLabel()->setWordWrapMode(QTextOption::NoWrap); |
| 96 | 144 |
| 97 content_layout->addWidget(paragraph); | 145 content_layout->addWidget(paragraph); |
| 98 content_layout->setSpacing(0); | 146 content_layout->setSpacing(0); |
| 99 content_layout->setContentsMargins(0, 0, 0, 0); | 147 content_layout->setContentsMargins(0, 0, 0, 0); |
| 100 content->setContentsMargins(12, 0, 0, 0); | 148 content->setContentsMargins(12, 0, 0, 0); |
| 111 | 159 |
| 112 Paragraph* Section::GetParagraph() { | 160 Paragraph* Section::GetParagraph() { |
| 113 return paragraph; | 161 return paragraph; |
| 114 } | 162 } |
| 115 | 163 |
| 116 LabelledParagraph::LabelledParagraph(const QString& label, const QString& data, QWidget* parent) : QWidget(parent) { | 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) { | |
| 117 QHBoxLayout* ly = new QHBoxLayout(this); | 169 QHBoxLayout* ly = new QHBoxLayout(this); |
| 118 | 170 |
| 119 labels = new Paragraph(label, this); | 171 labels_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); |
| 120 labels->setTextInteractionFlags(Qt::NoTextInteraction); | 172 data_.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
| 121 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | 173 |
| 122 labels->setWordWrap(QTextOption::NoWrap); | 174 ly->addWidget(&labels_, 0, Qt::AlignTop); |
| 123 labels->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding); | 175 ly->addWidget(&data_, 0, Qt::AlignTop); |
| 124 | |
| 125 paragraph = new Paragraph(data, this); | |
| 126 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 127 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 128 paragraph->setWordWrap(QTextOption::NoWrap); | |
| 129 paragraph->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); | |
| 130 | |
| 131 ly->addWidget(labels, 0, Qt::AlignTop); | |
| 132 ly->addWidget(paragraph, 0, Qt::AlignTop); | |
| 133 ly->setSpacing(20); | 176 ly->setSpacing(20); |
| 134 ly->setContentsMargins(0, 0, 0, 0); | 177 ly->setContentsMargins(0, 0, 0, 0); |
| 135 } | 178 } |
| 136 | 179 |
| 137 Paragraph* LabelledParagraph::GetLabels() { | 180 QLabel* LabelledParagraph::GetLabels() { |
| 138 return labels; | 181 return &labels_; |
| 139 } | 182 } |
| 140 | 183 |
| 141 Paragraph* LabelledParagraph::GetParagraph() { | 184 QLabel* LabelledParagraph::GetData() { |
| 142 return paragraph; | 185 return &data_; |
| 186 } | |
| 187 | |
| 188 QLabel* LabelledParagraph::GetParagraph() { | |
| 189 return GetData(); | |
| 143 } | 190 } |
| 144 | 191 |
| 145 LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) | 192 LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) |
| 146 : QWidget(parent) { | 193 : QWidget(parent) { |
| 147 QVBoxLayout* layout = new QVBoxLayout(this); | 194 QVBoxLayout* layout = new QVBoxLayout(this); |
| 149 header = new Header(title, this); | 196 header = new Header(title, this); |
| 150 | 197 |
| 151 // this is not accessible from the object because there's really | 198 // this is not accessible from the object because there's really |
| 152 // no reason to make it accessible... | 199 // no reason to make it accessible... |
| 153 content = new LabelledParagraph(label, data, this); | 200 content = new LabelledParagraph(label, data, this); |
| 154 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); | 201 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
| 155 content->setContentsMargins(12, 0, 0, 0); | 202 content->setContentsMargins(12, 0, 0, 0); |
| 156 | 203 |
| 157 layout->addWidget(header); | 204 layout->addWidget(header); |
| 158 layout->addWidget(content); | 205 layout->addWidget(content); |
| 159 layout->setSpacing(0); | 206 layout->setSpacing(0); |
| 162 | 209 |
| 163 Header* LabelledSection::GetHeader() { | 210 Header* LabelledSection::GetHeader() { |
| 164 return header; | 211 return header; |
| 165 } | 212 } |
| 166 | 213 |
| 167 Paragraph* LabelledSection::GetLabels() { | 214 QLabel* LabelledSection::GetLabels() { |
| 168 return content->GetLabels(); | 215 return content->GetLabels(); |
| 169 } | 216 } |
| 170 | 217 |
| 171 Paragraph* LabelledSection::GetParagraph() { | 218 QLabel* LabelledSection::GetData() { |
| 219 return content->GetData(); | |
| 220 } | |
| 221 | |
| 222 QLabel* LabelledSection::GetParagraph() { | |
| 172 return content->GetParagraph(); | 223 return content->GetParagraph(); |
| 173 } | 224 } |
| 174 | 225 |
| 175 SelectableSection::SelectableSection(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { | 226 SelectableSection::SelectableSection(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
| 176 QVBoxLayout* layout = new QVBoxLayout(this); | 227 QVBoxLayout* layout = new QVBoxLayout(this); |
