Mercurial > minori
annotate src/gui/widgets/text.cc @ 86:c912128af0eb
*: various macos fixes
todo: push that bsd thing to upstream
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Tue, 31 Oct 2023 13:52:36 -0400 |
| parents | d02fdf1d6708 |
| children | 4aef97f4d998 |
| 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 | |
| 75 exists because sometimes with SelectableSection it will let you go | |
| 76 out of bounds */ | |
| 83 | 77 Line::Line(QWidget* parent) : QLineEdit(parent) { |
| 75 | 78 setFrame(false); |
| 79 setReadOnly(true); | |
| 80 | |
| 81 QPalette pal; | |
| 86 | 82 pal.setColor(QPalette::Base, Qt::transparent); |
| 75 | 83 setPalette(pal); |
| 84 | |
| 85 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 86 } | |
| 87 | |
| 83 | 88 Line::Line(const QString& text, QWidget* parent) : Line(parent) { |
| 89 SetText(text); | |
| 90 } | |
| 91 | |
| 92 void Line::SetText(const QString& text) { | |
| 93 setText(text); | |
| 94 setCursorPosition(0); /* displays left text first */ | |
| 95 } | |
| 96 | |
| 97 Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { | |
| 75 | 98 QFont fnt(font()); |
| 99 fnt.setPixelSize(16); | |
| 100 setFont(fnt); | |
| 101 | |
| 102 QPalette pal(palette()); | |
| 86 | 103 pal.setColor(QPalette::Text, Qt::blue); |
| 75 | 104 setPalette(pal); |
| 105 } | |
| 106 | |
| 83 | 107 Section::Section(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
| 64 | 108 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 109 |
| 110 header = new Header(title, this); | |
| 111 | |
| 112 QWidget* content = new QWidget(this); | |
| 64 | 113 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 114 |
| 115 paragraph = new Paragraph(data, this); | |
| 116 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 117 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 118 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 119 | |
| 64 | 120 content_layout->addWidget(paragraph); |
| 121 content_layout->setSpacing(0); | |
| 122 content_layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 123 content->setContentsMargins(12, 0, 0, 0); |
| 124 | |
| 64 | 125 layout->addWidget(header); |
| 126 layout->addWidget(paragraph); | |
| 127 layout->setSpacing(0); | |
| 128 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 129 } |
| 130 | |
| 75 | 131 Header* Section::GetHeader() { |
| 46 | 132 return header; |
| 133 } | |
| 134 | |
| 75 | 135 Paragraph* Section::GetParagraph() { |
| 46 | 136 return paragraph; |
| 137 } | |
| 138 | |
| 83 | 139 LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) : QWidget(parent) { |
| 64 | 140 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 141 |
| 142 header = new Header(title, this); | |
| 143 | |
| 144 // this is not accessible from the object because there's really | |
| 145 // no reason to make it accessible... | |
| 146 QWidget* content = new QWidget(this); | |
| 83 | 147 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); |
| 46 | 148 |
| 149 labels = new Paragraph(label, this); | |
| 150 labels->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 151 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 152 labels->setWordWrapMode(QTextOption::NoWrap); | |
| 83 | 153 labels->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding); |
| 46 | 154 |
| 155 paragraph = new Paragraph(data, this); | |
| 156 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 157 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 158 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 83 | 159 paragraph->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 46 | 160 |
|
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
161 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 162 content_layout->addWidget(labels, 0, Qt::AlignTop); |
| 163 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
|
164 content_layout->setSpacing(20); |
| 62 | 165 content_layout->setContentsMargins(0, 0, 0, 0); |
| 46 | 166 |
| 167 content->setContentsMargins(12, 0, 0, 0); | |
| 168 | |
| 64 | 169 layout->addWidget(header); |
| 170 layout->addWidget(content); | |
| 171 layout->setSpacing(0); | |
| 172 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 173 } |
| 174 | |
| 75 | 175 Header* LabelledSection::GetHeader() { |
| 46 | 176 return header; |
| 177 } | |
| 178 | |
| 75 | 179 Paragraph* LabelledSection::GetLabels() { |
| 46 | 180 return labels; |
| 181 } | |
| 182 | |
| 75 | 183 Paragraph* LabelledSection::GetParagraph() { |
| 46 | 184 return paragraph; |
| 185 } | |
| 186 | |
| 83 | 187 SelectableSection::SelectableSection(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { |
| 64 | 188 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 189 |
| 190 header = new Header(title, this); | |
| 191 | |
| 192 QWidget* content = new QWidget(this); | |
| 64 | 193 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 194 |
| 195 paragraph = new Paragraph(data, content); | |
| 196 | |
| 64 | 197 content_layout->addWidget(paragraph); |
| 198 content_layout->setSpacing(0); | |
| 199 content_layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 200 content->setContentsMargins(12, 0, 0, 0); |
| 201 | |
| 64 | 202 layout->addWidget(header); |
| 203 layout->addWidget(content); | |
| 204 layout->setSpacing(0); | |
| 205 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 206 } |
| 207 | |
| 75 | 208 Header* SelectableSection::GetHeader() { |
| 46 | 209 return header; |
| 210 } | |
| 211 | |
| 75 | 212 Paragraph* SelectableSection::GetParagraph() { |
| 46 | 213 return paragraph; |
| 214 } | |
| 215 | |
| 83 | 216 OneLineSection::OneLineSection(const QString& title, const QString& text, QWidget* parent) : QWidget(parent) { |
| 75 | 217 QVBoxLayout* layout = new QVBoxLayout(this); |
| 218 | |
| 219 header = new Header(title, this); | |
| 46 | 220 |
| 75 | 221 QWidget* content = new QWidget(this); |
| 222 QHBoxLayout* content_layout = new QHBoxLayout(content); | |
| 223 | |
| 224 line = new Line(text, content); | |
| 46 | 225 |
| 75 | 226 content_layout->addWidget(line); |
| 227 content_layout->setSpacing(0); | |
| 228 content_layout->setContentsMargins(0, 0, 0, 0); | |
| 229 content->setContentsMargins(12, 0, 0, 0); | |
| 46 | 230 |
| 75 | 231 layout->addWidget(header); |
| 232 layout->addWidget(content); | |
| 233 layout->setSpacing(0); | |
| 234 layout->setContentsMargins(0, 0, 0, 0); | |
| 64 | 235 } |
| 236 | |
| 75 | 237 Header* OneLineSection::GetHeader() { |
| 238 return header; | |
| 46 | 239 } |
| 240 | |
| 75 | 241 Line* OneLineSection::GetLine() { |
| 242 return line; | |
| 46 | 243 } |
| 244 | |
| 245 } // namespace TextWidgets | |
| 246 | |
| 247 #include "gui/widgets/moc_text.cpp" |
