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 labels->setFixedWidth(123);
|
|
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
|
|
93 QHBoxLayout* content_layout = new QHBoxLayout;
|
|
94 content_layout->addWidget(labels, 0, Qt::AlignTop);
|
|
95 content_layout->addWidget(paragraph, 0, Qt::AlignTop);
|
|
96 content_layout->setSpacing(0);
|
|
97 content_layout->setMargin(0);
|
|
98 content->setLayout(content_layout);
|
|
99
|
|
100 content->setContentsMargins(12, 0, 0, 0);
|
|
101
|
|
102 layout()->addWidget(header);
|
|
103 layout()->addWidget(content);
|
|
104 layout()->setSpacing(0);
|
|
105 layout()->setMargin(0);
|
|
106 }
|
|
107
|
|
108 Header* LabelledTextParagraph::GetHeader() {
|
|
109 return header;
|
|
110 }
|
|
111
|
|
112 Paragraph* LabelledTextParagraph::GetLabels() {
|
|
113 return labels;
|
|
114 }
|
|
115
|
|
116 Paragraph* LabelledTextParagraph::GetParagraph() {
|
|
117 return paragraph;
|
|
118 }
|
|
119
|
|
120 SelectableTextParagraph::SelectableTextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) {
|
|
121 setLayout(new QVBoxLayout);
|
|
122
|
|
123 header = new Header(title, this);
|
|
124
|
|
125 QWidget* content = new QWidget(this);
|
|
126 content->setLayout(new QHBoxLayout);
|
|
127
|
|
128 paragraph = new Paragraph(data, content);
|
|
129
|
|
130 content->layout()->addWidget(paragraph);
|
|
131 content->layout()->setSpacing(0);
|
|
132 content->layout()->setMargin(0);
|
|
133 content->setContentsMargins(12, 0, 0, 0);
|
|
134
|
|
135 layout()->addWidget(header);
|
|
136 layout()->addWidget(content);
|
|
137 layout()->setSpacing(0);
|
|
138 layout()->setMargin(0);
|
|
139 }
|
|
140
|
|
141 Header* SelectableTextParagraph::GetHeader() {
|
|
142 return header;
|
|
143 }
|
|
144
|
|
145 Paragraph* SelectableTextParagraph::GetParagraph() {
|
|
146 return paragraph;
|
|
147 }
|
|
148
|
|
149 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */
|
|
150 Paragraph::Paragraph(QString text, QWidget* parent) : QPlainTextEdit(text, parent) {
|
|
151 setReadOnly(true);
|
|
152 setTextInteractionFlags(Qt::TextBrowserInteraction);
|
|
153 setFrameShape(QFrame::NoFrame);
|
|
154 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
155 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
156
|
|
157 QPalette pal;
|
|
158 pal.setColor(QPalette::Window, QColor(0, 0, 0, 0));
|
|
159 setPalette(pal);
|
|
160
|
|
161 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
162 }
|
|
163
|
|
164 /* highly based upon... some stackoverflow answer for PyQt */
|
|
165 QSize Paragraph::minimumSizeHint() const {
|
|
166 QTextDocument* doc = document();
|
49
|
167 doc->adjustSize();
|
|
168 long h = 0;
|
|
169 for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) {
|
|
170 h += doc->documentLayout()->blockBoundingRect(line).height();
|
|
171 }
|
46
|
172 return QSize(QPlainTextEdit::sizeHint().width(), h);
|
|
173 }
|
|
174
|
|
175 QSize Paragraph::sizeHint() const {
|
|
176 return minimumSizeHint();
|
|
177 }
|
|
178
|
|
179 /* this is still actually useful, so we'll keep it */
|
|
180 void SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) {
|
|
181 QTextDocument* document = new QTextDocument(text_edit);
|
|
182 document->setDocumentLayout(new QPlainTextDocumentLayout(document));
|
|
183 document->setPlainText(data);
|
|
184 text_edit->setDocument(document);
|
|
185 }
|
|
186
|
|
187 } // namespace TextWidgets
|
|
188
|
|
189 #include "gui/widgets/moc_text.cpp"
|