diff src/ui_utils.cpp @ 7:07a9095eaeed

Update Refactored some code, moved some around
author Paper <mrpapersonic@gmail.com>
date Thu, 24 Aug 2023 23:11:38 -0400
parents 1d82f6e04d7d
children b1f73678ef61
line wrap: on
line diff
--- a/src/ui_utils.cpp	Wed Aug 16 00:49:17 2023 -0400
+++ b/src/ui_utils.cpp	Thu Aug 24 23:11:38 2023 -0400
@@ -1,5 +1,11 @@
+#include <QPixmap>
+#include <QLabel>
+#include <QFrame>
+#include <QVBoxLayout>
+#include <QTextBlock>
 #include "window.h"
 #include "ui_utils.h"
+#include "session.h"
 #ifdef MACOSX
 #include "sys/osx/dark_theme.h"
 #else
@@ -37,75 +43,130 @@
 	return (session.config.theme == DARK);
 }
 
-void UiUtils::CreateTextHeader(QWidget* parent, QString title, QPoint point, QSize size) {
+void UiUtils::CreateTextHeader(QWidget* parent, QString title) {
 	QLabel* static_text_title  = new QLabel(title, parent);
 	static_text_title->setTextFormat(Qt::PlainText);
-	static_text_title->setStyleSheet("font-weight: bold");
-	static_text_title->move(point.x(), point.y());
-	static_text_title->resize(size.width(), 16);
+
+	QFont font = static_text_title->font();
+	font.setWeight(QFont::Bold);
+	static_text_title->setFont(font);
+
+	static_text_title->setFixedHeight(16);
+	parent->layout()->addWidget(static_text_title);
 
 	QFrame* static_text_line = new QFrame(parent);
 	static_text_line->setFrameShape(QFrame::HLine);
 	static_text_line->setFrameShadow(QFrame::Sunken);
-	static_text_line->resize(size.width(), 2);
-	static_text_line->move(point.x(), point.y()+18);
+	static_text_line->setFixedHeight(2);
+	parent->layout()->addWidget(static_text_line);
 }
 
-QPlainTextEdit* UiUtils::CreateTextParagraph(QWidget* parent, QString title, QString data, QPoint point, QSize size) {
-	CreateTextHeader(parent, title, point, size);
+QPlainTextEdit* UiUtils::CreateTextParagraph(QWidget* parent, QString title, QString data) {
+	QWidget* paragraph_master = new QWidget(parent);
+	paragraph_master->setLayout(new QVBoxLayout);
 
-	QPlainTextEdit* paragraph = new QPlainTextEdit(data, parent);
+	CreateTextHeader(paragraph_master, title);
+
+	Paragraph* paragraph = new Paragraph(data, paragraph_master);
 	paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
 	paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
 	paragraph->setWordWrapMode(QTextOption::NoWrap);
-	paragraph->setFrameShape(QFrame::NoFrame);
-	paragraph->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-	paragraph->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-	paragraph->setStyleSheet("background: transparent;");
-	paragraph->move(point.x()+12, point.y()+32);
-	paragraph->resize(size.width()-12, size.height()-32);
+	paragraph->setContentsMargins(12, 0, 0, 0);
+
+	paragraph_master->layout()->addWidget(paragraph);
+	paragraph_master->layout()->setSpacing(0);
+	paragraph_master->layout()->setMargin(0);
+	paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+
 	return paragraph;
 }
 
-QPlainTextEdit* UiUtils::CreateTextParagraphWithLabels(QWidget* parent, QString title, QString label, QString data, QPoint point, QSize size) {
-	CreateTextHeader(parent, title, point, size);
+QPlainTextEdit* UiUtils::CreateTextParagraphWithLabels(QWidget* parent, QString title, QString label, QString data) {
+	QWidget* paragraph_master = new QWidget(parent);
+	paragraph_master->setLayout(new QVBoxLayout);
+
+	CreateTextHeader(paragraph_master, title);
 
-	QPlainTextEdit* label_t = new QPlainTextEdit(label, parent);
+	QWidget* paragraph_and_label = new QWidget(paragraph_master);
+	paragraph_and_label->setLayout(new QHBoxLayout);
+	paragraph_and_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+
+	Paragraph* label_t = new Paragraph(label, paragraph_and_label);
 	label_t->setTextInteractionFlags(Qt::NoTextInteraction);
 	label_t->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
 	label_t->setWordWrapMode(QTextOption::NoWrap);
-	label_t->setFrameShape(QFrame::NoFrame);
-	label_t->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-	label_t->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-	label_t->setStyleSheet("background: transparent;");
-	label_t->move(point.x()+12, point.y()+32);
-	label_t->resize(90, size.height()-32);
+	label_t->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+	label_t->setFixedWidth(123);
 
-	QPlainTextEdit* paragraph = new QPlainTextEdit(data, parent);
+	Paragraph* paragraph = new Paragraph(data, paragraph_and_label);
 	paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
 	paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
 	paragraph->setWordWrapMode(QTextOption::NoWrap);
-	paragraph->setFrameShape(QFrame::NoFrame);
-	paragraph->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-	paragraph->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-	paragraph->setStyleSheet("background: transparent;");
-	paragraph->move(point.x()+102, point.y()+32);
-	paragraph->resize(size.width()-102, size.height()-32);
+
+	((QBoxLayout*)paragraph_and_label->layout())->addWidget(label_t, 0, Qt::AlignTop);
+	((QBoxLayout*)paragraph_and_label->layout())->addWidget(paragraph, 0, Qt::AlignTop);
+
+	paragraph_and_label->setContentsMargins(12, 0, 0, 0);
+
+	paragraph_master->layout()->addWidget(paragraph_and_label);
+	paragraph_master->layout()->setSpacing(0);
+	paragraph_master->layout()->setMargin(0);
+	paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+
 	return paragraph;
 }
 
 /* As far as I can tell, this is identical to the way Taiga implements it.
    Kind of cool, I didn't even look into the source code for it :p */
-QPlainTextEdit* UiUtils::CreateSelectableTextParagraph(QWidget* parent, QString title, QString data, QPoint point, QSize size) {
-	CreateTextHeader(parent, title, point, size);
+QPlainTextEdit* UiUtils::CreateSelectableTextParagraph(QWidget* parent, QString title, QString data) {
+	QWidget* paragraph_master = new QWidget(parent);
+	paragraph_master->setLayout(new QVBoxLayout);
+
+	CreateTextHeader(paragraph_master, title);
+
+	QWidget* paragraph_widget = new QWidget(paragraph_master);
+	paragraph_widget->setLayout(new QHBoxLayout);
+	paragraph_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
 
-	QPlainTextEdit* text_edit = new QPlainTextEdit(data, parent);
-	text_edit->setReadOnly(true);
-	text_edit->setFrameShape(QFrame::NoFrame);
-	text_edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-	text_edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-	text_edit->setStyleSheet("background: transparent;");
-	text_edit->move(point.x()+12, point.y()+32);
-	text_edit->resize(size.width()-12, size.height()-32);
+	Paragraph* text_edit = new Paragraph(data, paragraph_widget);
+
+	paragraph_widget->layout()->addWidget(text_edit);
+
+	paragraph_widget->setContentsMargins(12, 0, 0, 0);
+
+	paragraph_master->layout()->addWidget(paragraph_widget);
+	paragraph_master->layout()->setSpacing(0);
+	paragraph_master->layout()->setMargin(0);
+
+	paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+
 	return text_edit;
 }
+
+void UiUtils::SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) {
+	QTextDocument* document = new QTextDocument(text_edit);
+	document->setDocumentLayout(new QPlainTextDocumentLayout(document));
+	document->setPlainText(data);
+	text_edit->setDocument(document);
+}
+
+Paragraph::Paragraph(QString text, QWidget* parent)
+	: QPlainTextEdit(text, parent) {
+	setReadOnly(true);
+	setTextInteractionFlags(Qt::TextBrowserInteraction);
+	setFrameShape(QFrame::NoFrame);
+	setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+	setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+	setStyleSheet("background: transparent;");
+	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
+}
+
+QSize Paragraph::minimumSizeHint() const {
+	QTextDocument* doc = document();
+	long h = (long)(blockBoundingGeometry(doc->findBlockByNumber(doc->blockCount() - 1)).bottom() + (2 * doc->documentMargin()));
+	return QSize(QPlainTextEdit::sizeHint().width(), (long)h);
+}
+
+QSize Paragraph::sizeHint() const {
+	return minimumSizeHint();
+}