Mercurial > minori
diff src/gui/widgets/text.cc @ 348:6b0768158dcd
text: redesign almost every widget
i.e. Paragraph is now a QLabel, etc etc, some things will probably
break, idc
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Tue, 25 Jun 2024 11:19:54 -0400 |
parents | b82841e76e79 |
children | f81bed4e04ac |
line wrap: on
line diff
--- a/src/gui/widgets/text.cc Sun Jun 23 10:32:09 2024 -0400 +++ b/src/gui/widgets/text.cc Tue Jun 25 11:19:54 2024 -0400 @@ -1,5 +1,6 @@ #include "gui/widgets/text.h" #include "core/session.h" +#include "core/strings.h" #include <QDebug> #include <QFrame> @@ -9,272 +10,128 @@ #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) +/* Generic header meant to be used in conjunction with Section<T> */ + +Header::Header(QWidget* parent) : QWidget(parent) - , static_text_title(title) { + , title_(new QLabel(this)) + , separator_(new QFrame(this)) { QVBoxLayout* layout = new QVBoxLayout(this); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); - static_text_title.setTextFormat(Qt::PlainText); + title_->setTextFormat(Qt::PlainText); { - QFont font = static_text_title.font(); + QFont font = title_->font(); font.setWeight(QFont::Bold); - static_text_title.setFont(font); + title_->setFont(font); } - static_text_line.setFrameShape(QFrame::HLine); - static_text_line.setFrameShadow(QFrame::Sunken); - static_text_line.setFixedHeight(2); + separator_->setFrameShape(QFrame::HLine); + separator_->setFrameShadow(QFrame::Sunken); + separator_->setFixedHeight(2); - layout->addWidget(&static_text_title); - layout->addWidget(&static_text_line); + layout->addWidget(title_.data()); + layout->addWidget(separator_.data()); layout->setSpacing(0); layout->setContentsMargins(0, 0, 0, 0); } -void Header::SetText(const QString& text) { - static_text_title.setText(text); +void Header::SetText(const std::string& text) { + title_->setText(Strings::ToQString(text)); updateGeometry(); } -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.setFrameStyle(QFrame::NoFrame); - - text_edit.document()->setDocumentMargin(0); - - SetText(text); - - layout->addWidget(&text_edit); -} - -void Paragraph::SetText(const QString& text) { - text_edit.document()->setPlainText(text); +/* ---------------------------------------------------------------------------------- */ +/* "Paragraph" widgets, as in widgets meant to hold a bunch of text. */ - /* return the view to the start */ - QTextCursor cursor = text_edit.textCursor(); - cursor.setPosition(0); - text_edit.setTextCursor(cursor); -} - -bool Paragraph::hasHeightForWidth() const { - return true; -} - -int Paragraph::heightForWidth(int w) const { - QTextDocument* doc = text_edit.document(); - doc->setTextWidth(w); - - return doc->size().toSize().height(); -} - -QPlainTextEdit* Paragraph::GetLabel() { - return &text_edit; -} - -Line::Line(QWidget* parent) : QWidget(parent) { - QVBoxLayout* layout = new QVBoxLayout(this); +Paragraph::Paragraph(QWidget *parent) : QWidget(parent), label_(new QLabel) { + 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;"); + label_->setTextInteractionFlags(Qt::TextBrowserInteraction); - layout->addWidget(&line_edit_); -} + /* defaults */ + SetWordWrap(true); + SetSelectable(true); -Line::Line(const QString& text, QWidget* parent) : Line(parent) { - SetText(text); + layout->addWidget(label_.data()); } -void Line::SetText(const QString& text) { - line_edit_.setText(text); - line_edit_.setCursorPosition(0); +void Paragraph::SetText(const std::string& text) { + label_->setText(Strings::ToQString(text)); } -Title::Title(const QString& title, QWidget* parent) : Line(title, parent) { - QFont fnt(line_edit_.font()); - fnt.setPixelSize(16); - line_edit_.setFont(fnt); - - line_edit_.setForegroundRole(QPalette::Highlight); +void Paragraph::SetSelectable(bool enable) { + label_->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents, !enable); + label_->setCursor(enable ? Qt::IBeamCursor : Qt::ArrowCursor); } -Section::Section(const QString& title, const QString& data, QWidget* parent) - : QWidget(parent) { - QVBoxLayout* layout = new QVBoxLayout(this); - - header = new Header(title, this); - - QWidget* content = new QWidget(this); - QHBoxLayout* content_layout = new QHBoxLayout(content); - - paragraph = new Paragraph(data, this); - 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); - content_layout->setContentsMargins(0, 0, 0, 0); - content->setContentsMargins(12, 0, 0, 0); - - layout->addWidget(header); - layout->addWidget(paragraph); - layout->setSpacing(0); - layout->setContentsMargins(0, 0, 0, 0); +void Paragraph::SetWordWrap(bool enable) { + label_->setWordWrap(enable); } -Header* Section::GetHeader() { - return header; -} +/* LabelledParagraph implementation */ -Paragraph* Section::GetParagraph() { - return paragraph; -} - -/* despite being named a "labelled paragraph" this uses QLabels for simplicity */ -LabelledParagraph::LabelledParagraph(const QString& label, const QString& data, QWidget* parent) +LabelledParagraph::LabelledParagraph(QWidget* parent) : QWidget(parent) - , labels_(label) - , data_(data) { + , contents_(new QWidget) + , contents_layout_(new QGridLayout) { QHBoxLayout* ly = new QHBoxLayout(this); - labels_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); - data_.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); + contents_layout_->setVerticalSpacing(1); + contents_layout_->setHorizontalSpacing(20); + contents_layout_->setContentsMargins(0, 0, 0, 0); + contents_layout_->setColumnStretch(1, 0); - ly->addWidget(&labels_, 0, Qt::AlignTop); - ly->addWidget(&data_, 0, Qt::AlignTop); - ly->setSpacing(20); + contents_->setLayout(contents_layout_.data()); + + ly->addWidget(contents_.data()); ly->setContentsMargins(0, 0, 0, 0); } -QLabel* LabelledParagraph::GetLabels() { - return &labels_; -} - -QLabel* LabelledParagraph::GetData() { - return &data_; -} - -QLabel* LabelledParagraph::GetParagraph() { - return GetData(); +LabelledParagraph::~LabelledParagraph() { + data_.clear(); } -LabelledSection::LabelledSection(const QString& title, const QString& label, const QString& data, QWidget* parent) - : QWidget(parent) { - QVBoxLayout* layout = new QVBoxLayout(this); - - header = new Header(title, this); - - // 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::Maximum); - content->setContentsMargins(12, 0, 0, 0); +void LabelledParagraph::Clear(void) { + for (auto& [label, data] : data_) { + contents_layout_->removeWidget(label.data()); + contents_layout_->removeWidget(data.data()); + } - layout->addWidget(header); - layout->addWidget(content); - layout->setSpacing(0); - layout->setContentsMargins(0, 0, 0, 0); -} - -Header* LabelledSection::GetHeader() { - return header; -} - -QLabel* LabelledSection::GetLabels() { - return content->GetLabels(); -} - -QLabel* LabelledSection::GetData() { - return content->GetData(); -} - -QLabel* LabelledSection::GetParagraph() { - return content->GetParagraph(); + data_.clear(); } -SelectableSection::SelectableSection(const QString& title, const QString& data, QWidget* parent) : QWidget(parent) { - QVBoxLayout* layout = new QVBoxLayout(this); - - header = new Header(title, this); +void LabelledParagraph::SetData(const std::vector<std::pair<std::string, std::string>>& data) { + Clear(); - QWidget* content = new QWidget(this); - QHBoxLayout* content_layout = new QHBoxLayout(content); - - paragraph = new Paragraph(data, content); + data_.reserve(data.size()); + for (std::size_t i = 0; i < data.size(); i++) { + QSharedPointer<QLabel> first(new QLabel); + QSharedPointer<QLabel> second(new QLabel); - content_layout->addWidget(paragraph); - content_layout->setSpacing(0); - content_layout->setContentsMargins(12, 0, 0, 0); - content->setContentsMargins(0, 0, 0, 0); + first->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); + + first->setText(Strings::ToQString(data[i].first)); + second->setText(Strings::ToQString(data[i].second)); - layout->addWidget(header); - layout->addWidget(content); - layout->setSpacing(0); - layout->setContentsMargins(0, 0, 0, 0); -} + data_.push_back({first, second}); -Header* SelectableSection::GetHeader() { - return header; + contents_layout_->addWidget(first.data(), i, 0); + contents_layout_->addWidget(second.data(), i, 1); + } } -Paragraph* SelectableSection::GetParagraph() { - return paragraph; -} - -OneLineSection::OneLineSection(const QString& title, const QString& text, QWidget* parent) : QWidget(parent) { - QVBoxLayout* layout = new QVBoxLayout(this); - - header = new Header(title, this); - - QWidget* content = new QWidget(this); - QHBoxLayout* content_layout = new QHBoxLayout(content); - - line = new Line(text, content); +void LabelledParagraph::SetStyle(int style) { + const QString style_sheet = (style & LabelledParagraph::BoldedLabels) ? "font-weight: bold;" : ""; + for (auto& [label, data] : data_) + label->setStyleSheet(style_sheet); - content_layout->addWidget(line); - content_layout->setSpacing(0); - content_layout->setContentsMargins(0, 0, 0, 0); - content->setContentsMargins(12, 0, 0, 0); - - layout->addWidget(header); - layout->addWidget(content); - layout->setSpacing(0); - layout->setContentsMargins(0, 0, 0, 0); -} - -Header* OneLineSection::GetHeader() { - return header; -} - -Line* OneLineSection::GetLine() { - return line; + // TODO ElidedData } } // namespace TextWidgets