view src/gui/widgets/text.cpp @ 67:442065432549

poster: make posters link to AniList
author Paper <mrpapersonic@gmail.com>
date Mon, 02 Oct 2023 07:06:44 -0400
parents fe719c109dbc
children 5ccb99bfa605
line wrap: on
line source

#include "gui/widgets/text.h"
#include "core/session.h"
#include <QDebug>
#include <QFrame>
#include <QLabel>
#include <QPixmap>
#include <QTextBlock>
#include <QVBoxLayout>

namespace TextWidgets {

Header::Header(QString title, QWidget* parent) : QWidget(parent) {
	QVBoxLayout* layout = new QVBoxLayout(this);
	setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);

	static_text_title = new QLabel(title, this);
	static_text_title->setTextFormat(Qt::PlainText);
	QFont font = static_text_title->font();
	font.setWeight(QFont::Bold);
	static_text_title->setFont(font);
	/* FIXME: is this needed? */
	static_text_title->setFixedHeight(16);

	static_text_line = new QFrame(this);
	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->setSpacing(0);
	layout->setContentsMargins(0, 0, 0, 0);
}

void Header::SetText(QString text) {
	static_text_title->setText(text);
}

TextParagraph::TextParagraph(QString title, 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->setTextInteractionFlags(Qt::NoTextInteraction);
	paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
	paragraph->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);
}

Header* TextParagraph::GetHeader() {
	return header;
}

Paragraph* TextParagraph::GetParagraph() {
	return paragraph;
}

LabelledTextParagraph::LabelledTextParagraph(QString title, QString label, 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...
	QWidget* content = new QWidget(this);
	content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

	labels = new Paragraph(label, this);
	labels->setTextInteractionFlags(Qt::NoTextInteraction);
	labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
	labels->setWordWrapMode(QTextOption::NoWrap);
	labels->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

	paragraph = new Paragraph(data, this);
	paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
	paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
	paragraph->setWordWrapMode(QTextOption::NoWrap);

	QHBoxLayout* content_layout = new QHBoxLayout(content);
	content_layout->addWidget(labels, 0, Qt::AlignTop);
	content_layout->addWidget(paragraph, 0, Qt::AlignTop);
	content_layout->setSpacing(20);
	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* LabelledTextParagraph::GetHeader() {
	return header;
}

Paragraph* LabelledTextParagraph::GetLabels() {
	return labels;
}

Paragraph* LabelledTextParagraph::GetParagraph() {
	return paragraph;
}

SelectableTextParagraph::SelectableTextParagraph(QString title, 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, content);

	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(content);
	layout->setSpacing(0);
	layout->setContentsMargins(0, 0, 0, 0);
}

Header* SelectableTextParagraph::GetHeader() {
	return header;
}

Paragraph* SelectableTextParagraph::GetParagraph() {
	return paragraph;
}

/* inherits QPlainTextEdit and gives a much more reasonable minimum size */
Paragraph::Paragraph(QString text, QWidget* parent) : QPlainTextEdit(text, parent) {
	setTextInteractionFlags(Qt::TextBrowserInteraction);
	setFrameShape(QFrame::NoFrame);
	setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

	QPalette pal;
	pal.setColor(QPalette::Window, QColor(0, 0, 0, 0));
	setPalette(pal);

	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
}

void Paragraph::SetText(QString text) {
	QTextDocument* document = new QTextDocument(this);
	document->setDocumentLayout(new QPlainTextDocumentLayout(document));
	document->setPlainText(text);
	setDocument(document);
}

/* highly based upon... some stackoverflow answer for PyQt */
QSize Paragraph::minimumSizeHint() const {
	return QSize(0, 0);
}

QSize Paragraph::sizeHint() const {
	QTextDocument* doc = 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);
}

Title::Title(QString title, QWidget* parent) : Paragraph(title, parent) {
	setReadOnly(true);
	setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setWordWrapMode(QTextOption::NoWrap);
	setFrameShape(QFrame::NoFrame);
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
	setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

	QFont fnt(font());
	fnt.setPointSize(12);
	setFont(fnt);

	QPalette pal(palette());
	pal.setColor(QPalette::Window, Qt::transparent);
	pal.setColor(QPalette::Text, QColor(0x00, 0x33, 0x99));
	setPalette(pal);
}

} // namespace TextWidgets

#include "gui/widgets/moc_text.cpp"