Mercurial > minori
diff src/gui/widgets/text.cc @ 291:9a88e1725fd2
*: refactor lots of stuff
I forgot to put this into different commits, oops!
anyway, it doesn't really matter *that* much since this is an
unfinished hobby project anyway. once it starts getting stable
commit history will be more important, but for now it's not
that big of a deal
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 12 May 2024 16:31:07 -0400 |
parents | 862d0d8619f6 |
children | b82841e76e79 |
line wrap: on
line diff
--- a/src/gui/widgets/text.cc Wed May 08 17:32:28 2024 -0400 +++ b/src/gui/widgets/text.cc Sun May 12 16:31:07 2024 -0400 @@ -6,82 +6,130 @@ #include <QLabel> #include <QTextBlock> #include <QVBoxLayout> +#include <QScrollArea> +#include <QDebug> + +/* WARNING: GARBAGE CODE FOLLOWS + * + * This file is filled with spaghetti to make this + * stupid text render how I want it to. + * + * many cases of hacking with setSizePolicy() are seen + * all around this file. Edit it only if really + * necessary, please. +*/ namespace TextWidgets { -Header::Header(const QString& title, QWidget* parent) : QWidget(parent) { +Header::Header(const QString& title, QWidget* parent) + : QWidget(parent) + , static_text_title(title) { QVBoxLayout* layout = new QVBoxLayout(this); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); - static_text_title = new QLabel(title, this); - static_text_title->setTextFormat(Qt::PlainText); + static_text_title.setTextFormat(Qt::PlainText); { - QFont font = static_text_title->font(); + QFont font = static_text_title.font(); font.setWeight(QFont::Bold); - static_text_title->setFont(font); + static_text_title.setFont(font); } - static_text_line = new QFrame(this); - static_text_line->setFrameShape(QFrame::HLine); - static_text_line->setFrameShadow(QFrame::Sunken); - static_text_line->setFixedHeight(2); + static_text_line.setFrameShape(QFrame::HLine); + static_text_line.setFrameShadow(QFrame::Sunken); + static_text_line.setFixedHeight(2); - layout->addWidget(static_text_title); - layout->addWidget(static_text_line); + layout->addWidget(&static_text_title); + layout->addWidget(&static_text_line); layout->setSpacing(0); layout->setContentsMargins(0, 0, 0, 0); } void Header::SetText(const QString& text) { - static_text_title->setText(text); + static_text_title.setText(text); updateGeometry(); } -/* for now, this is a QLabel with a couple of default settings. - * - * eventually I'll have to implement this as a QScrollArea, just in case - * some random text decides to overflow or something. - */ -Paragraph::Paragraph(const QString& text, QWidget* parent) : QLabel(text, parent) { - setTextInteractionFlags(Qt::TextBrowserInteraction); - setFrameShape(QFrame::NoFrame); - setCursor(Qt::IBeamCursor); /* emulate Taiga */ - setWordWrap(true); +Paragraph::Paragraph(const QString& text, QWidget* parent) : QWidget(parent) { + /* meh */ + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); + + QVBoxLayout* layout = new QVBoxLayout(this); + layout->setSpacing(0); + layout->setContentsMargins(0, 0, 0, 0); + + text_edit.setTextInteractionFlags(Qt::TextBrowserInteraction); + text_edit.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + text_edit.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + text_edit.setStyleSheet("background: transparent;"); + + text_edit.document()->setDocumentMargin(0); - setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); + SetText(text); + + layout->addWidget(&text_edit); +} + +void Paragraph::SetText(const QString& text) { + text_edit.document()->setPlainText(text); + + /* return the view to the start */ + QTextCursor cursor = text_edit.textCursor(); + cursor.setPosition(0); + text_edit.setTextCursor(cursor); +} + +QSize Paragraph::minimumSizeHint() const { + return QSize(0, 0); } -/* kept here for legacy reasons, see explanation above */ -void Paragraph::SetText(const QString& text) { - setText(text); +/* highly based upon... some stackoverflow answer for PyQt */ +QSize Paragraph::sizeHint() const { + QTextDocument* doc = text_edit.document(); + doc->adjustSize(); + + long h = 0; + for (QTextBlock line = doc->begin(); line != doc->end(); line = line.next()) + h += doc->documentLayout()->blockBoundingRect(line).height(); + + return QSize(doc->size().width(), h); +} + +QPlainTextEdit* Paragraph::GetLabel() { + return &text_edit; } -/* Equivalent to Paragraph(), but disables word wrap. */ -Line::Line(QWidget* parent) : Paragraph("", parent) { - setWordWrap(false); +Line::Line(QWidget* parent) : QWidget(parent) { + QVBoxLayout* layout = new QVBoxLayout(this); + layout->setSpacing(0); + layout->setContentsMargins(0, 0, 0, 0); + + line_edit_.setReadOnly(true); + line_edit_.setFrame(false); + line_edit_.setStyleSheet("background: transparent;"); + + layout->addWidget(&line_edit_); } -Line::Line(const QString& text, QWidget* parent) : Paragraph(text, parent) { - setWordWrap(false); +Line::Line(const QString& text, QWidget* parent) : Line(parent) { + SetText(text); } -/* legacy function, don't use in new code */ void Line::SetText(const QString& text) { - setText(text); + line_edit_.setText(text); + line_edit_.setCursorPosition(0); } Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { - QFont fnt(font()); + QFont fnt(line_edit_.font()); fnt.setPixelSize(16); - setFont(fnt); + line_edit_.setFont(fnt); - QPalette pal(palette()); - pal.setColor(QPalette::Text, pal.color(QPalette::Highlight)); - setPalette(pal); + line_edit_.setForegroundRole(QPalette::Highlight); } -Section::Section(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { +Section::Section(const QString& title, const QString& data, QWidget* parent) + : QWidget(parent) { QVBoxLayout* layout = new QVBoxLayout(this); header = new Header(title, this); @@ -90,9 +138,9 @@ QHBoxLayout* content_layout = new QHBoxLayout(content); paragraph = new Paragraph(data, this); - paragraph->setTextInteractionFlags(Qt::NoTextInteraction); - paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); - paragraph->setWordWrap(QTextOption::NoWrap); + paragraph->GetLabel()->setTextInteractionFlags(Qt::NoTextInteraction); + paragraph->GetLabel()->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); + paragraph->GetLabel()->setWordWrapMode(QTextOption::NoWrap); content_layout->addWidget(paragraph); content_layout->setSpacing(0); @@ -113,33 +161,32 @@ return paragraph; } -LabelledParagraph::LabelledParagraph(const QString& label, const QString& data, QWidget* parent) : QWidget(parent) { +/* despite being named a "labelled paragraph" this uses QLabels for simplicity */ +LabelledParagraph::LabelledParagraph(const QString& label, const QString& data, QWidget* parent) + : QWidget(parent) + , labels_(label) + , data_(data) { QHBoxLayout* ly = new QHBoxLayout(this); - labels = new Paragraph(label, this); - labels->setTextInteractionFlags(Qt::NoTextInteraction); - labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); - labels->setWordWrap(QTextOption::NoWrap); - labels->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding); + labels_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); + data_.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); - paragraph = new Paragraph(data, this); - paragraph->setTextInteractionFlags(Qt::NoTextInteraction); - paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); - paragraph->setWordWrap(QTextOption::NoWrap); - paragraph->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); - - ly->addWidget(labels, 0, Qt::AlignTop); - ly->addWidget(paragraph, 0, Qt::AlignTop); + ly->addWidget(&labels_, 0, Qt::AlignTop); + ly->addWidget(&data_, 0, Qt::AlignTop); ly->setSpacing(20); ly->setContentsMargins(0, 0, 0, 0); } -Paragraph* LabelledParagraph::GetLabels() { - return labels; +QLabel* LabelledParagraph::GetLabels() { + return &labels_; } -Paragraph* LabelledParagraph::GetParagraph() { - return paragraph; +QLabel* LabelledParagraph::GetData() { + return &data_; +} + +QLabel* LabelledParagraph::GetParagraph() { + return GetData(); } LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) @@ -151,7 +198,7 @@ // this is not accessible from the object because there's really // no reason to make it accessible... content = new LabelledParagraph(label, data, this); - content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); + content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); content->setContentsMargins(12, 0, 0, 0); layout->addWidget(header); @@ -164,11 +211,15 @@ return header; } -Paragraph* LabelledSection::GetLabels() { +QLabel* LabelledSection::GetLabels() { return content->GetLabels(); } -Paragraph* LabelledSection::GetParagraph() { +QLabel* LabelledSection::GetData() { + return content->GetData(); +} + +QLabel* LabelledSection::GetParagraph() { return content->GetParagraph(); }