view src/gui/widgets/sidebar.cpp @ 75:d3e9310598b1

*: refactor some stuff text: "TextParagraph"s are now called sections, because that's the actual word for it :P text: new classes: Line and OneLineSection, solves many problems with paragraphs that are only one line long (ex. going out of bounds) http: reworked http stuff to allow threaded get requests, also moved it to its own file to (hopefully) remove clutter eventually I'll make a threaded post request method and use that in the "basic" function
author Paper <mrpapersonic@gmail.com>
date Wed, 04 Oct 2023 01:42:30 -0400
parents 5ccb99bfa605
children 6f7385bd334c
line wrap: on
line source

#include "gui/widgets/sidebar.h"
#include <QFrame>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMouseEvent>

SideBar::SideBar(QWidget* parent) : QListWidget(parent) {
	setFrameShape(QFrame::NoFrame);
	setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setSelectionMode(QAbstractItemView::SingleSelection);
	setSelectionBehavior(QAbstractItemView::SelectItems);
	setMouseTracking(true);
	/* FIXME: is there an easy way to do this with palettes? */
	setStyleSheet("QListWidget::item:disabled { background: transparent }");

	SetBackgroundColor(Qt::transparent);

	QFont font;
	font.setPixelSize(12);
	setFont(font);

	connect(this, &QListWidget::currentRowChanged, this,
	        [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); });
}

void SideBar::SetCurrentItem(int index) {
	setCurrentRow(AddSeparatorsToIndex(index));
}

void SideBar::SetBackgroundColor(QColor color) {
	viewport()->setAutoFillBackground(color != Qt::transparent);
	QPalette pal(palette());
	pal.setColor(QPalette::Window, color);
	setPalette(pal);
}

QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) {
	QListWidgetItem* item = new QListWidgetItem(this);
	item->setText(name);
	if (!icon.isNull())
		item->setIcon(icon);
	return item;
}

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

QListWidgetItem* SideBar::AddSeparator() {
	QListWidgetItem* item = new QListWidgetItem(this);
	QFrame* line = new QFrame(this);
	line->setFrameShape(QFrame::HLine);
	line->setFrameShadow(QFrame::Sunken);
	line->setMouseTracking(true);
	line->setEnabled(false);

	setItemWidget(item, line);
	item->setFlags(Qt::NoItemFlags);
	item->setBackground(QBrush(Qt::transparent));
	return item;
}

int SideBar::AddSeparatorsToIndex(int index) {
	int i, j;
	for (i = 0, j = 0; i < index;) {
		i++;
		if (IndexIsSeparator(indexFromItem(item(i))))
			j++;
	}
	return i + j;
}

int SideBar::RemoveSeparatorsFromIndex(int index) {
	int i, j;
	for (i = 0, j = 0; i < index; i++) {
		if (!IndexIsSeparator(indexFromItem(item(i))))
			j++;
	}
	return j;
}

bool SideBar::IndexIsSeparator(QModelIndex index) const {
	return !(index.isValid() && index.flags() & Qt::ItemIsEnabled);
}

QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const {
	if (IndexIsSeparator(index))
		return QItemSelectionModel::NoUpdate;
	return QItemSelectionModel::ClearAndSelect;
}

void SideBar::mouseMoveEvent(QMouseEvent* event) {
	if (!IndexIsSeparator(indexAt(event->pos())))
		setCursor(Qt::PointingHandCursor);
	else
		unsetCursor();
	QListView::mouseMoveEvent(event);
}

#include "gui/widgets/moc_sidebar.cpp"