view 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 source

#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
#include "sys/win32/dark_theme.h"
#endif

QIcon UiUtils::CreateSideBarIcon(const char* file) {
	QPixmap pixmap(file, "PNG");
	QIcon result;
	result.addPixmap(pixmap, QIcon::Normal);
	result.addPixmap(pixmap, QIcon::Selected);
	return result;
}

bool UiUtils::IsInDarkMode() {
	if (session.config.theme != OS)
		return (session.config.theme == DARK);
#ifdef MACOSX
	if (osx::DarkThemeAvailable()) {
		if (osx::IsInDarkTheme()) {
			return true;
		} else {
			return false;
		}
	}
#elif defined(WIN32)
	if (win32::DarkThemeAvailable()) {
		if (win32::IsInDarkTheme()) {
			return true;
		} else {
			return false;
		}
	}
#endif
	return (session.config.theme == DARK);
}

void UiUtils::CreateTextHeader(QWidget* parent, QString title) {
	QLabel* static_text_title  = new QLabel(title, parent);
	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);
	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->setFixedHeight(2);
	parent->layout()->addWidget(static_text_line);
}

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);

	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);

	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->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
	label_t->setFixedWidth(123);

	Paragraph* paragraph = new Paragraph(data, paragraph_and_label);
	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);

	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) {
	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);

	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();
}