comparison 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
comparison
equal deleted inserted replaced
6:1d82f6e04d7d 7:07a9095eaeed
1 #include "sidebar.h"
1 #include <QListWidget> 2 #include <QListWidget>
2 #include <QListWidgetItem> 3 #include <QListWidgetItem>
3 #include <QFrame> 4 #include <QFrame>
4 #include "sidebar.h" 5 #include <QMouseEvent>
6 #include <QMessageBox>
5 7
6 SideBar::SideBar(QWidget *parent) 8 SideBar::SideBar(QWidget *parent)
7 : QListWidget(parent) 9 : QListWidget(parent)
8 { 10 {
9 setObjectName("sidebar"); 11 setObjectName("sidebar");
10 setFrameShape(QFrame::NoFrame); 12 setFrameShape(QFrame::NoFrame);
11 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 13 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
12 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 14 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
15 setSelectionMode(QAbstractItemView::SingleSelection);
16 setSelectionBehavior(QAbstractItemView::SelectItems);
17 setMouseTracking(true);
13 viewport()->setAutoFillBackground(false); 18 viewport()->setAutoFillBackground(false);
14 setStyleSheet("font-size: 12px"); 19 setStyleSheet("font-size: 12px");
20 connect(this, &QListWidget::currentRowChanged, this, [this](int index){
21 emit CurrentItemChanged(RemoveSeparatorsFromIndex(index));
22 });
15 } 23 }
16 24
17 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { 25 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) {
18 QListWidgetItem* item = new QListWidgetItem(this); 26 QListWidgetItem* item = new QListWidgetItem(this);
19 item->setText(name); 27 item->setText(name);
22 return item; 30 return item;
23 } 31 }
24 32
25 QListWidgetItem* SideBar::AddSeparator() { 33 QListWidgetItem* SideBar::AddSeparator() {
26 QListWidgetItem* item = new QListWidgetItem(this); 34 QListWidgetItem* item = new QListWidgetItem(this);
27 item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
28 setStyleSheet("QListWidget::item:disabled {background: transparent;}"); 35 setStyleSheet("QListWidget::item:disabled {background: transparent;}");
29 QFrame* line = new QFrame(this); 36 QFrame* line = new QFrame(this);
30 line->setFrameShape(QFrame::HLine); 37 line->setFrameShape(QFrame::HLine);
31 line->setFrameShadow(QFrame::Sunken); 38 line->setFrameShadow(QFrame::Sunken);
39 line->setMouseTracking(true);
32 line->setEnabled(false); 40 line->setEnabled(false);
33 setItemWidget(item, line); 41 setItemWidget(item, line);
42 item->setFlags(Qt::NoItemFlags);
34 return item; 43 return item;
35 } 44 }
45
46 int SideBar::RemoveSeparatorsFromIndex(int index) {
47 int i, j;
48 for (i = 0, j = 0; i < index; i++) {
49 if (!IndexIsSeparator(indexFromItem(item(i)))) j++;
50 }
51 return j;
52 }
53
54 bool SideBar::IndexIsSeparator(QModelIndex index) const {
55 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled);
56 }
57
58 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex & index,
59 const QEvent * event) const {
60 if (IndexIsSeparator(index))
61 return QItemSelectionModel::NoUpdate;
62 return QItemSelectionModel::ClearAndSelect;
63 /* silence unused parameter warnings */
64 (void)event;
65 }
66
67 void SideBar::mouseMoveEvent(QMouseEvent *event) {
68 if (!IndexIsSeparator(indexAt(event->pos())))
69 setCursor(Qt::PointingHandCursor);
70 else
71 unsetCursor();
72 QListView::mouseMoveEvent(event);
73 }
74
75 #include "moc_sidebar.cpp"