comparison src/gui/sidebar.cpp @ 9:5c0397762b53

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