Mercurial > minori
annotate src/gui/widgets/text.cpp @ 55:d10b6c6b432e
add xml lib, we will need to use it eventually
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Tue, 26 Sep 2023 12:37:08 -0400 |
| parents | 75c804f713b2 |
| children | 4c6dd5999b39 |
| rev | line source |
|---|---|
| 46 | 1 #include "gui/widgets/text.h" |
| 2 #include "core/session.h" | |
| 3 #include <QFrame> | |
| 4 #include <QLabel> | |
| 5 #include <QPixmap> | |
| 6 #include <QTextBlock> | |
| 7 #include <QVBoxLayout> | |
| 49 | 8 #include <QDebug> |
| 46 | 9 |
| 10 namespace TextWidgets { | |
| 11 | |
| 12 Header::Header(QString title, QWidget* parent) : QWidget(parent) { | |
| 13 setLayout(new QVBoxLayout); | |
| 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); | |
| 21 static_text_title->setFixedHeight(16); | |
| 22 | |
| 23 static_text_line = new QFrame(this); | |
| 24 static_text_line->setFrameShape(QFrame::HLine); | |
| 25 static_text_line->setFrameShadow(QFrame::Sunken); | |
| 26 static_text_line->setFixedHeight(2); | |
| 27 | |
| 28 layout()->addWidget(static_text_title); | |
| 29 layout()->addWidget(static_text_line); | |
| 30 layout()->setSpacing(0); | |
| 31 layout()->setMargin(0); | |
| 32 } | |
| 33 | |
| 34 void Header::SetTitle(QString title) { | |
| 35 static_text_title->setText(title); | |
| 36 } | |
| 37 | |
| 38 TextParagraph::TextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) { | |
| 39 setLayout(new QVBoxLayout); | |
| 40 | |
| 41 header = new Header(title, this); | |
| 42 | |
| 43 QWidget* content = new QWidget(this); | |
| 44 content->setLayout(new QHBoxLayout); | |
| 45 | |
| 46 paragraph = new Paragraph(data, this); | |
| 47 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 48 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 49 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 50 | |
| 51 content->layout()->addWidget(paragraph); | |
| 52 content->layout()->setSpacing(0); | |
| 53 content->layout()->setMargin(0); | |
| 54 content->setContentsMargins(12, 0, 0, 0); | |
| 55 | |
| 56 layout()->addWidget(header); | |
| 57 layout()->addWidget(paragraph); | |
| 58 layout()->setSpacing(0); | |
| 59 layout()->setMargin(0); | |
| 60 } | |
| 61 | |
| 62 Header* TextParagraph::GetHeader() { | |
| 63 return header; | |
| 64 } | |
| 65 | |
| 66 Paragraph* TextParagraph::GetParagraph() { | |
| 67 return paragraph; | |
| 68 } | |
| 69 | |
| 70 LabelledTextParagraph::LabelledTextParagraph(QString title, QString label, QString data, QWidget* parent) | |
| 71 : QWidget(parent) { | |
| 72 setLayout(new QVBoxLayout); | |
| 73 | |
| 74 header = new Header(title, this); | |
| 75 | |
| 76 // this is not accessible from the object because there's really | |
| 77 // no reason to make it accessible... | |
| 78 QWidget* content = new QWidget(this); | |
| 79 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); | |
| 80 | |
| 81 labels = new Paragraph(label, this); | |
| 82 labels->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 83 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 84 labels->setWordWrapMode(QTextOption::NoWrap); | |
| 85 labels->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); | |
| 86 | |
| 87 paragraph = new Paragraph(data, this); | |
| 88 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 89 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 90 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 91 | |
|
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
92 QHBoxLayout* content_layout = new QHBoxLayout(content); |
| 46 | 93 content_layout->addWidget(labels, 0, Qt::AlignTop); |
| 94 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
|
95 content_layout->setSpacing(20); |
| 46 | 96 content_layout->setMargin(0); |
| 97 | |
| 98 content->setContentsMargins(12, 0, 0, 0); | |
| 99 | |
| 100 layout()->addWidget(header); | |
| 101 layout()->addWidget(content); | |
| 102 layout()->setSpacing(0); | |
| 103 layout()->setMargin(0); | |
| 104 } | |
| 105 | |
| 106 Header* LabelledTextParagraph::GetHeader() { | |
| 107 return header; | |
| 108 } | |
| 109 | |
| 110 Paragraph* LabelledTextParagraph::GetLabels() { | |
| 111 return labels; | |
| 112 } | |
| 113 | |
| 114 Paragraph* LabelledTextParagraph::GetParagraph() { | |
| 115 return paragraph; | |
| 116 } | |
| 117 | |
| 118 SelectableTextParagraph::SelectableTextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) { | |
| 119 setLayout(new QVBoxLayout); | |
| 120 | |
| 121 header = new Header(title, this); | |
| 122 | |
| 123 QWidget* content = new QWidget(this); | |
| 124 content->setLayout(new QHBoxLayout); | |
| 125 | |
| 126 paragraph = new Paragraph(data, content); | |
| 127 | |
| 128 content->layout()->addWidget(paragraph); | |
| 129 content->layout()->setSpacing(0); | |
| 130 content->layout()->setMargin(0); | |
| 131 content->setContentsMargins(12, 0, 0, 0); | |
| 132 | |
| 133 layout()->addWidget(header); | |
| 134 layout()->addWidget(content); | |
| 135 layout()->setSpacing(0); | |
| 136 layout()->setMargin(0); | |
| 137 } | |
| 138 | |
| 139 Header* SelectableTextParagraph::GetHeader() { | |
| 140 return header; | |
| 141 } | |
| 142 | |
| 143 Paragraph* SelectableTextParagraph::GetParagraph() { | |
| 144 return paragraph; | |
| 145 } | |
| 146 | |
| 147 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */ | |
| 148 Paragraph::Paragraph(QString text, QWidget* parent) : QPlainTextEdit(text, parent) { | |
| 149 setTextInteractionFlags(Qt::TextBrowserInteraction); | |
| 150 setFrameShape(QFrame::NoFrame); | |
| 151 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 152 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 153 | |
| 154 QPalette pal; | |
| 155 pal.setColor(QPalette::Window, QColor(0, 0, 0, 0)); | |
| 156 setPalette(pal); | |
| 157 | |
| 158 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | |
| 159 } | |
| 160 | |
| 161 /* highly based upon... some stackoverflow answer for PyQt */ | |
| 162 QSize Paragraph::minimumSizeHint() const { | |
| 163 QTextDocument* doc = document(); | |
| 49 | 164 doc->adjustSize(); |
| 165 long h = 0; | |
| 166 for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) { | |
| 167 h += doc->documentLayout()->blockBoundingRect(line).height(); | |
| 168 } | |
|
50
10868c3fb2be
text.cpp: set paragraph width from document, labelled texts are now...
Paper <mrpapersonic@gmail.com>
parents:
49
diff
changeset
|
169 return QSize(doc->size().width(), h); |
| 46 | 170 } |
| 171 | |
| 172 QSize Paragraph::sizeHint() const { | |
| 173 return minimumSizeHint(); | |
| 174 } | |
| 175 | |
| 176 /* this is still actually useful, so we'll keep it */ | |
| 177 void SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) { | |
| 178 QTextDocument* document = new QTextDocument(text_edit); | |
| 179 document->setDocumentLayout(new QPlainTextDocumentLayout(document)); | |
| 180 document->setPlainText(data); | |
| 181 text_edit->setDocument(document); | |
| 182 } | |
| 183 | |
| 184 } // namespace TextWidgets | |
| 185 | |
| 186 #include "gui/widgets/moc_text.cpp" |
