comparison src/gui/widgets/text.cc @ 365:f81bed4e04ac

*: megacommit that probably breaks things
author Paper <paper@paper.us.eu.org>
date Wed, 02 Oct 2024 23:06:43 -0400
parents 6b0768158dcd
children
comparison
equal deleted inserted replaced
364:99c961c91809 365:f81bed4e04ac
7 #include <QLabel> 7 #include <QLabel>
8 #include <QTextBlock> 8 #include <QTextBlock>
9 #include <QVBoxLayout> 9 #include <QVBoxLayout>
10 #include <QScrollArea> 10 #include <QScrollArea>
11 #include <QDebug> 11 #include <QDebug>
12 #include <QPainter>
12 13
13 namespace TextWidgets { 14 namespace TextWidgets {
14 15
15 /* Generic header meant to be used in conjunction with Section<T> */ 16 /* Generic header meant to be used in conjunction with Section<T> */
16 17
17 Header::Header(QWidget* parent) 18 Header::Header(QWidget* parent)
18 : QWidget(parent) 19 : QWidget(parent)
19 , title_(new QLabel(this)) 20 , title_(new QLabel)
20 , separator_(new QFrame(this)) { 21 , separator_(new QFrame) {
21 QVBoxLayout* layout = new QVBoxLayout(this); 22 QVBoxLayout* layout = new QVBoxLayout(this);
22 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); 23 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
23 24
24 title_->setTextFormat(Qt::PlainText); 25 title_->setTextFormat(Qt::PlainText);
25 26
40 } 41 }
41 42
42 void Header::SetText(const std::string& text) { 43 void Header::SetText(const std::string& text) {
43 title_->setText(Strings::ToQString(text)); 44 title_->setText(Strings::ToQString(text));
44 updateGeometry(); 45 updateGeometry();
46 }
47
48 Label::Label(QWidget *parent) : QLabel(parent) {}
49 Label::Label(const QString &string, QWidget *parent) : QLabel(string, parent) {}
50
51 void Label::SetElidingMode(bool elide) {
52 elide_ = elide;
53 update();
54 }
55
56 void Label::paintEvent(QPaintEvent *event) {
57 if (elide_) {
58 /* bruh */
59 if (wordWrap()) {
60 QFrame::paintEvent(event);
61
62 const QString content = text();
63
64 QPainter painter(this);
65 QFontMetrics fontMetrics = painter.fontMetrics();
66
67 bool didElide = false;
68 int lineSpacing = fontMetrics.lineSpacing();
69 int y = 0;
70
71 QTextLayout textLayout(content, painter.font());
72 textLayout.beginLayout();
73 for (;;) {
74 QTextLine line = textLayout.createLine();
75
76 if (!line.isValid())
77 break;
78
79 line.setLineWidth(width());
80 int nextLineY = y + lineSpacing;
81
82 if (height() >= nextLineY + lineSpacing) {
83 line.draw(&painter, QPoint(0, y));
84 y = nextLineY;
85 } else {
86 QString lastLine = content.mid(line.textStart());
87 QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width());
88 painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine);
89 line = textLayout.createLine();
90 didElide = line.isValid();
91 break;
92 }
93 }
94 textLayout.endLayout();
95 } else {
96 QString backup_text = QLabel::text();
97
98 QFontMetrics metric(fontMetrics());
99 QString elided_text = metric.elidedText(backup_text, Qt::ElideRight, width());
100
101 QLabel::setText(elided_text);
102 QLabel::paintEvent(event);
103 QLabel::setText(backup_text);
104 }
105 } else {
106 /* QLabel can handle everything... */
107 QLabel::paintEvent(event);
108 }
45 } 109 }
46 110
47 /* ---------------------------------------------------------------------------------- */ 111 /* ---------------------------------------------------------------------------------- */
48 /* "Paragraph" widgets, as in widgets meant to hold a bunch of text. */ 112 /* "Paragraph" widgets, as in widgets meant to hold a bunch of text. */
49 113
109 void LabelledParagraph::SetData(const std::vector<std::pair<std::string, std::string>>& data) { 173 void LabelledParagraph::SetData(const std::vector<std::pair<std::string, std::string>>& data) {
110 Clear(); 174 Clear();
111 175
112 data_.reserve(data.size()); 176 data_.reserve(data.size());
113 for (std::size_t i = 0; i < data.size(); i++) { 177 for (std::size_t i = 0; i < data.size(); i++) {
114 QSharedPointer<QLabel> first(new QLabel); 178 QSharedPointer<Label> first(new Label);
115 QSharedPointer<QLabel> second(new QLabel); 179 QSharedPointer<Label> second(new Label);
116 180
117 first->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); 181 first->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
118 182
119 first->setText(Strings::ToQString(data[i].first)); 183 first->setText(Strings::ToQString(data[i].first));
120 second->setText(Strings::ToQString(data[i].second)); 184 second->setText(Strings::ToQString(data[i].second));
121 185
122 data_.push_back({first, second}); 186 data_.push_back({first, second});
123 187
124 contents_layout_->addWidget(first.data(), i, 0); 188 contents_layout_->addWidget(first.data(), i, 0);
125 contents_layout_->addWidget(second.data(), i, 1); 189 contents_layout_->addWidget(second.data(), i, 1);
126 } 190 }
127 } 191 }
128 192
129 void LabelledParagraph::SetStyle(int style) { 193 void LabelledParagraph::SetStyle(int style) {
130 const QString style_sheet = (style & LabelledParagraph::BoldedLabels) ? "font-weight: bold;" : ""; 194 const QString style_sheet = (style & LabelledParagraph::BoldedLabels) ? "font-weight: bold;" : "";
131 for (auto& [label, data] : data_) 195 for (auto& [label, data] : data_)
132 label->setStyleSheet(style_sheet); 196 label->setStyleSheet(style_sheet);
133 197
134 // TODO ElidedData 198 if (style & LabelledParagraph::ElidedData) {
199 for (auto& [label, data] : data_) {
200 data->setWordWrap(false);
201 data->SetElidingMode(true);
202 }
203 }
135 } 204 }
136 205
137 } // namespace TextWidgets 206 } // namespace TextWidgets