Mercurial > minori
annotate src/gui/widgets/text.cpp @ 68:2417121d894e
*: normalize usage of layouts
before, I used them two ways, once was by setting the layout later
by using setLayout(QWidget), and the other was just using the constructor.
I find the constructor to be easier to read, so I chose that one.
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Mon, 02 Oct 2023 21:33:25 -0400 |
| parents | fe719c109dbc |
| children | 5ccb99bfa605 |
| rev | line source |
|---|---|
| 46 | 1 #include "gui/widgets/text.h" |
| 2 #include "core/session.h" | |
| 63 | 3 #include <QDebug> |
| 46 | 4 #include <QFrame> |
| 5 #include <QLabel> | |
| 6 #include <QPixmap> | |
| 7 #include <QTextBlock> | |
| 8 #include <QVBoxLayout> | |
| 9 | |
| 10 namespace TextWidgets { | |
| 11 | |
| 12 Header::Header(QString title, QWidget* parent) : QWidget(parent) { | |
| 64 | 13 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 14 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
| 15 | |
| 16 static_text_title = new QLabel(title, this); | |
| 17 static_text_title->setTextFormat(Qt::PlainText); | |
| 18 QFont font = static_text_title->font(); | |
| 19 font.setWeight(QFont::Bold); | |
| 20 static_text_title->setFont(font); | |
| 64 | 21 /* FIXME: is this needed? */ |
| 46 | 22 static_text_title->setFixedHeight(16); |
| 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 | |
| 64 | 35 void Header::SetText(QString text) { |
| 36 static_text_title->setText(text); | |
| 46 | 37 } |
| 38 | |
| 39 TextParagraph::TextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) { | |
| 64 | 40 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 41 |
| 42 header = new Header(title, this); | |
| 43 | |
| 44 QWidget* content = new QWidget(this); | |
| 64 | 45 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 46 |
| 47 paragraph = new Paragraph(data, this); | |
| 48 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 49 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 50 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 51 | |
| 64 | 52 content_layout->addWidget(paragraph); |
| 53 content_layout->setSpacing(0); | |
| 54 content_layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 55 content->setContentsMargins(12, 0, 0, 0); |
| 56 | |
| 64 | 57 layout->addWidget(header); |
| 58 layout->addWidget(paragraph); | |
| 59 layout->setSpacing(0); | |
| 60 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 61 } |
| 62 | |
| 63 Header* TextParagraph::GetHeader() { | |
| 64 return header; | |
| 65 } | |
| 66 | |
| 67 Paragraph* TextParagraph::GetParagraph() { | |
| 68 return paragraph; | |
| 69 } | |
| 70 | |
| 71 LabelledTextParagraph::LabelledTextParagraph(QString title, QString label, QString data, QWidget* parent) | |
| 72 : QWidget(parent) { | |
| 64 | 73 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 74 |
| 75 header = new Header(title, this); | |
| 76 | |
| 77 // this is not accessible from the object because there's really | |
| 78 // no reason to make it accessible... | |
| 79 QWidget* content = new QWidget(this); | |
| 80 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); | |
| 81 | |
| 82 labels = new Paragraph(label, this); | |
| 83 labels->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 84 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 85 labels->setWordWrapMode(QTextOption::NoWrap); | |
| 86 labels->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); | |
| 87 | |
| 88 paragraph = new Paragraph(data, this); | |
| 89 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 90 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 91 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 92 | |
|
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
93 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 94 content_layout->addWidget(labels, 0, Qt::AlignTop); |
| 95 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
|
96 content_layout->setSpacing(20); |
| 62 | 97 content_layout->setContentsMargins(0, 0, 0, 0); |
| 46 | 98 |
| 99 content->setContentsMargins(12, 0, 0, 0); | |
| 100 | |
| 64 | 101 layout->addWidget(header); |
| 102 layout->addWidget(content); | |
| 103 layout->setSpacing(0); | |
| 104 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 105 } |
| 106 | |
| 107 Header* LabelledTextParagraph::GetHeader() { | |
| 108 return header; | |
| 109 } | |
| 110 | |
| 111 Paragraph* LabelledTextParagraph::GetLabels() { | |
| 112 return labels; | |
| 113 } | |
| 114 | |
| 115 Paragraph* LabelledTextParagraph::GetParagraph() { | |
| 116 return paragraph; | |
| 117 } | |
| 118 | |
| 119 SelectableTextParagraph::SelectableTextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) { | |
| 64 | 120 QVBoxLayout* layout = new QVBoxLayout(this); |
| 46 | 121 |
| 122 header = new Header(title, this); | |
| 123 | |
| 124 QWidget* content = new QWidget(this); | |
| 64 | 125 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 126 |
| 127 paragraph = new Paragraph(data, content); | |
| 128 | |
| 64 | 129 content_layout->addWidget(paragraph); |
| 130 content_layout->setSpacing(0); | |
| 131 content_layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 132 content->setContentsMargins(12, 0, 0, 0); |
| 133 | |
| 64 | 134 layout->addWidget(header); |
| 135 layout->addWidget(content); | |
| 136 layout->setSpacing(0); | |
| 137 layout->setContentsMargins(0, 0, 0, 0); | |
| 46 | 138 } |
| 139 | |
| 140 Header* SelectableTextParagraph::GetHeader() { | |
| 141 return header; | |
| 142 } | |
| 143 | |
| 144 Paragraph* SelectableTextParagraph::GetParagraph() { | |
| 145 return paragraph; | |
| 146 } | |
| 147 | |
| 148 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */ | |
| 149 Paragraph::Paragraph(QString text, QWidget* parent) : QPlainTextEdit(text, parent) { | |
| 150 setTextInteractionFlags(Qt::TextBrowserInteraction); | |
| 151 setFrameShape(QFrame::NoFrame); | |
| 152 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 153 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 154 | |
| 155 QPalette pal; | |
| 156 pal.setColor(QPalette::Window, QColor(0, 0, 0, 0)); | |
| 157 setPalette(pal); | |
| 158 | |
| 159 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | |
| 160 } | |
| 161 | |
| 64 | 162 void Paragraph::SetText(QString text) { |
| 163 QTextDocument* document = new QTextDocument(this); | |
| 164 document->setDocumentLayout(new QPlainTextDocumentLayout(document)); | |
| 165 document->setPlainText(text); | |
| 166 setDocument(document); | |
| 167 } | |
| 168 | |
| 46 | 169 /* highly based upon... some stackoverflow answer for PyQt */ |
| 170 QSize Paragraph::minimumSizeHint() const { | |
| 64 | 171 return QSize(0, 0); |
| 172 } | |
| 173 | |
| 174 QSize Paragraph::sizeHint() const { | |
| 46 | 175 QTextDocument* doc = document(); |
| 49 | 176 doc->adjustSize(); |
| 177 long h = 0; | |
| 178 for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) { | |
| 179 h += doc->documentLayout()->blockBoundingRect(line).height(); | |
| 180 } | |
|
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
181 return QSize(doc->size().width(), h); |
| 46 | 182 } |
| 183 | |
| 64 | 184 Title::Title(QString title, QWidget* parent) : Paragraph(title, parent) { |
| 185 setReadOnly(true); | |
| 186 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 187 setWordWrapMode(QTextOption::NoWrap); | |
| 188 setFrameShape(QFrame::NoFrame); | |
| 189 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); | |
| 190 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 191 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 46 | 192 |
| 64 | 193 QFont fnt(font()); |
| 194 fnt.setPointSize(12); | |
| 195 setFont(fnt); | |
| 196 | |
| 197 QPalette pal(palette()); | |
| 198 pal.setColor(QPalette::Window, Qt::transparent); | |
| 199 pal.setColor(QPalette::Text, QColor(0x00, 0x33, 0x99)); | |
| 200 setPalette(pal); | |
| 46 | 201 } |
| 202 | |
| 203 } // namespace TextWidgets | |
| 204 | |
| 205 #include "gui/widgets/moc_text.cpp" |
