Mercurial > minori
diff src/gui/widgets/text.cc @ 370:ea3a74ed2ef9
*: hm, last commit wasn't quite finished?
author | Paper <paper@tflc.us> |
---|---|
date | Fri, 25 Jul 2025 10:22:04 -0400 |
parents | f81bed4e04ac |
children |
line wrap: on
line diff
--- a/src/gui/widgets/text.cc Fri Jul 25 10:16:02 2025 -0400 +++ b/src/gui/widgets/text.cc Fri Jul 25 10:22:04 2025 -0400 @@ -5,21 +5,18 @@ #include <QDebug> #include <QFrame> #include <QLabel> +#include <QPainter> +#include <QScrollArea> #include <QTextBlock> #include <QVBoxLayout> -#include <QScrollArea> -#include <QDebug> -#include <QPainter> namespace TextWidgets { /* Generic header meant to be used in conjunction with Section<T> */ -Header::Header(QWidget* parent) - : QWidget(parent) - , title_(new QLabel) - , separator_(new QFrame) { - QVBoxLayout* layout = new QVBoxLayout(this); +Header::Header(QWidget *parent) : QWidget(parent), title_(new QLabel), separator_(new QFrame) +{ + QVBoxLayout *layout = new QVBoxLayout(this); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); title_->setTextFormat(Qt::PlainText); @@ -40,20 +37,27 @@ layout->setContentsMargins(0, 0, 0, 0); } -void Header::SetText(const std::string& text) { +void Header::SetText(const std::string &text) +{ title_->setText(Strings::ToQString(text)); updateGeometry(); } -Label::Label(QWidget *parent) : QLabel(parent) {} -Label::Label(const QString &string, QWidget *parent) : QLabel(string, parent) {} +Label::Label(QWidget *parent) : QLabel(parent) +{ +} +Label::Label(const QString &string, QWidget *parent) : QLabel(string, parent) +{ +} -void Label::SetElidingMode(bool elide) { +void Label::SetElidingMode(bool elide) +{ elide_ = elide; update(); } -void Label::paintEvent(QPaintEvent *event) { +void Label::paintEvent(QPaintEvent *event) +{ if (elide_) { /* bruh */ if (wordWrap()) { @@ -61,27 +65,27 @@ const QString content = text(); - QPainter painter(this); - QFontMetrics fontMetrics = painter.fontMetrics(); + QPainter painter(this); + QFontMetrics fontMetrics = painter.fontMetrics(); - bool didElide = false; - int lineSpacing = fontMetrics.lineSpacing(); - int y = 0; + bool didElide = false; + int lineSpacing = fontMetrics.lineSpacing(); + int y = 0; - QTextLayout textLayout(content, painter.font()); - textLayout.beginLayout(); - for (;;) { - QTextLine line = textLayout.createLine(); + QTextLayout textLayout(content, painter.font()); + textLayout.beginLayout(); + for (;;) { + QTextLine line = textLayout.createLine(); - if (!line.isValid()) - break; + if (!line.isValid()) + break; - line.setLineWidth(width()); - int nextLineY = y + lineSpacing; + line.setLineWidth(width()); + int nextLineY = y + lineSpacing; - if (height() >= nextLineY + lineSpacing) { - line.draw(&painter, QPoint(0, y)); - y = nextLineY; + if (height() >= nextLineY + lineSpacing) { + line.draw(&painter, QPoint(0, y)); + y = nextLineY; } else { QString lastLine = content.mid(line.textStart()); QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width()); @@ -111,7 +115,8 @@ /* ---------------------------------------------------------------------------------- */ /* "Paragraph" widgets, as in widgets meant to hold a bunch of text. */ -Paragraph::Paragraph(QWidget *parent) : QWidget(parent), label_(new QLabel) { +Paragraph::Paragraph(QWidget *parent) : QWidget(parent), label_(new QLabel) +{ QVBoxLayout *layout = new QVBoxLayout(this); layout->setSpacing(0); layout->setContentsMargins(0, 0, 0, 0); @@ -125,26 +130,28 @@ layout->addWidget(label_.data()); } -void Paragraph::SetText(const std::string& text) { +void Paragraph::SetText(const std::string &text) +{ label_->setText(Strings::ToQString(text)); } -void Paragraph::SetSelectable(bool enable) { +void Paragraph::SetSelectable(bool enable) +{ label_->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents, !enable); label_->setCursor(enable ? Qt::IBeamCursor : Qt::ArrowCursor); } -void Paragraph::SetWordWrap(bool enable) { +void Paragraph::SetWordWrap(bool enable) +{ label_->setWordWrap(enable); } /* LabelledParagraph implementation */ -LabelledParagraph::LabelledParagraph(QWidget* parent) - : QWidget(parent) - , contents_(new QWidget) - , contents_layout_(new QGridLayout) { - QHBoxLayout* ly = new QHBoxLayout(this); +LabelledParagraph::LabelledParagraph(QWidget *parent) + : QWidget(parent), contents_(new QWidget), contents_layout_(new QGridLayout) +{ + QHBoxLayout *ly = new QHBoxLayout(this); contents_layout_->setVerticalSpacing(1); contents_layout_->setHorizontalSpacing(20); @@ -157,12 +164,14 @@ ly->setContentsMargins(0, 0, 0, 0); } -LabelledParagraph::~LabelledParagraph() { +LabelledParagraph::~LabelledParagraph() +{ data_.clear(); } -void LabelledParagraph::Clear(void) { - for (auto& [label, data] : data_) { +void LabelledParagraph::Clear(void) +{ + for (auto &[label, data] : data_) { contents_layout_->removeWidget(label.data()); contents_layout_->removeWidget(data.data()); } @@ -170,7 +179,8 @@ data_.clear(); } -void LabelledParagraph::SetData(const std::vector<std::pair<std::string, std::string>>& data) { +void LabelledParagraph::SetData(const std::vector<std::pair<std::string, std::string>> &data) +{ Clear(); data_.reserve(data.size()); @@ -185,18 +195,19 @@ data_.push_back({first, second}); - contents_layout_->addWidget(first.data(), i, 0); + contents_layout_->addWidget(first.data(), i, 0); contents_layout_->addWidget(second.data(), i, 1); } } -void LabelledParagraph::SetStyle(int style) { +void LabelledParagraph::SetStyle(int style) +{ const QString style_sheet = (style & LabelledParagraph::BoldedLabels) ? "font-weight: bold;" : ""; - for (auto& [label, data] : data_) + for (auto &[label, data] : data_) label->setStyleSheet(style_sheet); if (style & LabelledParagraph::ElidedData) { - for (auto& [label, data] : data_) { + for (auto &[label, data] : data_) { data->setWordWrap(false); data->SetElidingMode(true); }