view src/gui/widgets/sidebar.cc @ 337:a7d4e5107531

dep/animone: REFACTOR ALL THE THINGS 1: animone now has its own syntax divergent from anisthesia, making different platforms actually have their own sections 2: process names in animone are now called `comm' (this will probably break things). this is what its called in bsd/linux so I'm just going to use it everywhere 3: the X11 code now checks for the existence of a UTF-8 window title and passes it if available 4: ANYTHING THATS NOT LINUX IS 100% UNTESTED AND CAN AND WILL BREAK! I still actually need to test the bsd code. to be honest I'm probably going to move all of the bsds into separate files because they're all essentially different operating systems at this point
author Paper <paper@paper.us.eu.org>
date Wed, 19 Jun 2024 12:51:15 -0400
parents 91ac90a34003
children
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);

	setStyleSheet("QListWidget::item:disabled { background: transparent }");

	SetBackgroundTransparent(true);

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

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

int SideBar::GetCurrentItem() {
	return RemoveSeparatorsFromIndex(currentRow());
}

void SideBar::SetBackgroundTransparent(bool yes) {
	viewport()->setAutoFillBackground(!yes);
}

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 separators = 0, items = 0;

	for (; items <= index;) {
		if (IndexIsSeparator(indexFromItem(item(items + separators)))) {
			separators++;
		} else {
			items++;
		}
	}

	return index + separators;
}

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

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