diff src/ui_utils.cpp @ 8:b1f73678ef61

update text paragraphs are now their own objects, as they should be
author Paper <mrpapersonic@gmail.com>
date Sat, 26 Aug 2023 03:39:34 -0400
parents 07a9095eaeed
children
line wrap: on
line diff
--- a/src/ui_utils.cpp	Thu Aug 24 23:11:38 2023 -0400
+++ b/src/ui_utils.cpp	Sat Aug 26 03:39:34 2023 -0400
@@ -12,7 +12,9 @@
 #include "sys/win32/dark_theme.h"
 #endif
 
-QIcon UiUtils::CreateSideBarIcon(const char* file) {
+namespace UiUtils {
+
+QIcon CreateSideBarIcon(const char* file) {
 	QPixmap pixmap(file, "PNG");
 	QIcon result;
 	result.addPixmap(pixmap, QIcon::Normal);
@@ -20,7 +22,7 @@
 	return result;
 }
 
-bool UiUtils::IsInDarkMode() {
+bool IsInDarkMode() {
 	if (session.config.theme != OS)
 		return (session.config.theme == DARK);
 #ifdef MACOSX
@@ -43,113 +45,154 @@
 	return (session.config.theme == DARK);
 }
 
-void UiUtils::CreateTextHeader(QWidget* parent, QString title) {
-	QLabel* static_text_title  = new QLabel(title, parent);
+Header::Header(QString title, QWidget* parent)
+	: QWidget(parent) {
+	setLayout(new QVBoxLayout);
+	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);
+	static_text_title->setFixedHeight(16);
 
-	static_text_title->setFixedHeight(16);
-	parent->layout()->addWidget(static_text_title);
-
-	QFrame* static_text_line = new QFrame(parent);
+	static_text_line = new QFrame(this);
 	static_text_line->setFrameShape(QFrame::HLine);
 	static_text_line->setFrameShadow(QFrame::Sunken);
 	static_text_line->setFixedHeight(2);
-	parent->layout()->addWidget(static_text_line);
+
+	layout()->addWidget(static_text_title);
+	layout()->addWidget(static_text_line);
+	layout()->setSpacing(0);
+	layout()->setMargin(0);
+}
+
+void Header::SetTitle(QString title) {
+	static_text_title->setText(title);
 }
 
-QPlainTextEdit* UiUtils::CreateTextParagraph(QWidget* parent, QString title, QString data) {
-	QWidget* paragraph_master = new QWidget(parent);
-	paragraph_master->setLayout(new QVBoxLayout);
-
-	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->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);
+TextParagraph::TextParagraph(QString title, QString data, QWidget* parent)
+	: QWidget(parent) {
+	setLayout(new QVBoxLayout);
 
-	return paragraph;
-}
-
-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);
+	header = new Header(title, this);
 
-	QWidget* paragraph_and_label = new QWidget(paragraph_master);
-	paragraph_and_label->setLayout(new QHBoxLayout);
-	paragraph_and_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+	QWidget* content = new QWidget(this);
+	content->setLayout(new QHBoxLayout);
 
-	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->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
-	label_t->setFixedWidth(123);
-
-	Paragraph* paragraph = new Paragraph(data, paragraph_and_label);
+	paragraph = new Paragraph(data, this);
 	paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
 	paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
 	paragraph->setWordWrapMode(QTextOption::NoWrap);
 
-	((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);
+	content->layout()->addWidget(paragraph);
+	content->layout()->setSpacing(0);
+	content->layout()->setMargin(0);
+	content->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);
+	layout()->addWidget(header);
+	layout()->addWidget(paragraph);
+	layout()->setSpacing(0);
+	layout()->setMargin(0);
+}
 
+Header* TextParagraph::GetHeader() {
+	return header;
+}
+
+Paragraph* TextParagraph::GetParagraph() {
 	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) {
-	QWidget* paragraph_master = new QWidget(parent);
-	paragraph_master->setLayout(new QVBoxLayout);
+LabelledTextParagraph::LabelledTextParagraph(QString title, QString label, QString data, QWidget* parent)
+	: QWidget(parent) {
+	setLayout(new QVBoxLayout);
+
+	header = new Header(title, this);
 
-	CreateTextHeader(paragraph_master, title);
+	// 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::Maximum);
 
-	QWidget* paragraph_widget = new QWidget(paragraph_master);
-	paragraph_widget->setLayout(new QHBoxLayout);
-	paragraph_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+	labels = new Paragraph(label, this);
+	labels->setTextInteractionFlags(Qt::NoTextInteraction);
+	labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
+	labels->setWordWrapMode(QTextOption::NoWrap);
+	labels->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+	labels->setFixedWidth(123);
 
-	Paragraph* text_edit = new Paragraph(data, paragraph_widget);
-
-	paragraph_widget->layout()->addWidget(text_edit);
-
-	paragraph_widget->setContentsMargins(12, 0, 0, 0);
+	paragraph = new Paragraph(data, this);
+	paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
+	paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
+	paragraph->setWordWrapMode(QTextOption::NoWrap);
 
-	paragraph_master->layout()->addWidget(paragraph_widget);
-	paragraph_master->layout()->setSpacing(0);
-	paragraph_master->layout()->setMargin(0);
+	QHBoxLayout* content_layout = new QHBoxLayout;
+	content_layout->addWidget(labels, 0, Qt::AlignTop);
+	content_layout->addWidget(paragraph, 0, Qt::AlignTop);
+	content_layout->setSpacing(0);
+	content_layout->setMargin(0);
+	content->setLayout(content_layout);
 
-	paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+	content->setContentsMargins(12, 0, 0, 0);
 
-	return text_edit;
+	layout()->addWidget(header);
+	layout()->addWidget(content);
+	layout()->setSpacing(0);
+	layout()->setMargin(0);
 }
 
-void UiUtils::SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) {
+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) {
+	setLayout(new QVBoxLayout);
+
+	header = new Header(title, this);
+
+	QWidget* content = new QWidget(this);
+	content->setLayout(new QHBoxLayout);
+
+	paragraph = new Paragraph(data, content);
+
+	content->layout()->addWidget(paragraph);
+	content->layout()->setSpacing(0);
+	content->layout()->setMargin(0);
+	content->setContentsMargins(12, 0, 0, 0);
+
+	layout()->addWidget(header);
+	layout()->addWidget(content);
+	layout()->setSpacing(0);
+	layout()->setMargin(0);
+}
+
+Header* SelectableTextParagraph::GetHeader() {
+	return header;
+}
+
+Paragraph* SelectableTextParagraph::GetParagraph() {
+	return paragraph;
+}
+
+void SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) {
 	QTextDocument* document = new QTextDocument(text_edit);
 	document->setDocumentLayout(new QPlainTextDocumentLayout(document));
 	document->setPlainText(data);
 	text_edit->setDocument(document);
 }
 
+/* inherits QPlainTextEdit and gives a much more reasonable minimum size */
 Paragraph::Paragraph(QString text, QWidget* parent)
 	: QPlainTextEdit(text, parent) {
 	setReadOnly(true);
@@ -161,6 +204,7 @@
 	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
 }
 
+/* highly based upon... some stackoverflow answer for PyQt */
 QSize Paragraph::minimumSizeHint() const {
 	QTextDocument* doc = document();
 	long h = (long)(blockBoundingGeometry(doc->findBlockByNumber(doc->blockCount() - 1)).bottom() + (2 * doc->documentMargin()));
@@ -170,3 +214,5 @@
 QSize Paragraph::sizeHint() const {
 	return minimumSizeHint();
 }
+
+} // namespace UiUtils