Mercurial > minori
diff src/sidebar.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 |
line wrap: on
line diff
--- a/src/sidebar.cpp Wed Aug 16 00:49:17 2023 -0400 +++ b/src/sidebar.cpp Thu Aug 24 23:11:38 2023 -0400 @@ -1,7 +1,9 @@ +#include "sidebar.h" #include <QListWidget> #include <QListWidgetItem> #include <QFrame> -#include "sidebar.h" +#include <QMouseEvent> +#include <QMessageBox> SideBar::SideBar(QWidget *parent) : QListWidget(parent) @@ -10,8 +12,14 @@ setFrameShape(QFrame::NoFrame); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setSelectionMode(QAbstractItemView::SingleSelection); + setSelectionBehavior(QAbstractItemView::SelectItems); + setMouseTracking(true); viewport()->setAutoFillBackground(false); setStyleSheet("font-size: 12px"); + connect(this, &QListWidget::currentRowChanged, this, [this](int index){ + emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); + }); } QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { @@ -24,12 +32,44 @@ QListWidgetItem* SideBar::AddSeparator() { QListWidgetItem* item = new QListWidgetItem(this); - item->setFlags(item->flags() & ~Qt::ItemIsEnabled); setStyleSheet("QListWidget::item:disabled {background: transparent;}"); 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); return item; } + +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 * event) const { + if (IndexIsSeparator(index)) + return QItemSelectionModel::NoUpdate; + return QItemSelectionModel::ClearAndSelect; + /* silence unused parameter warnings */ + (void)event; +} + +void SideBar::mouseMoveEvent(QMouseEvent *event) { + if (!IndexIsSeparator(indexAt(event->pos()))) + setCursor(Qt::PointingHandCursor); + else + unsetCursor(); + QListView::mouseMoveEvent(event); +} + +#include "moc_sidebar.cpp"