comparison src/gui/widgets/sidebar.cc @ 81:9b2b41f83a5e

boring: mass rename to cc because this is a very unix-y project, it makes more sense to use the 'cc' extension
author Paper <mrpapersonic@gmail.com>
date Mon, 23 Oct 2023 12:07:27 -0400
parents src/gui/widgets/sidebar.cpp@6f7385bd334c
children 80f49f623d30
comparison
equal deleted inserted replaced
80:825506f0e221 81:9b2b41f83a5e
1 #include "gui/widgets/sidebar.h"
2 #include <QFrame>
3 #include <QListWidget>
4 #include <QListWidgetItem>
5 #include <QMouseEvent>
6
7 SideBar::SideBar(QWidget* parent) : QListWidget(parent) {
8 setFrameShape(QFrame::NoFrame);
9 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
10 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
11 setSelectionMode(QAbstractItemView::SingleSelection);
12 setSelectionBehavior(QAbstractItemView::SelectItems);
13 setMouseTracking(true);
14 /* FIXME: is there an easy way to do this with palettes? */
15 setStyleSheet("QListWidget::item:disabled { background: transparent }");
16
17 SetBackgroundColor(Qt::transparent);
18
19 connect(this, &QListWidget::currentRowChanged, this,
20 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); });
21 }
22
23 void SideBar::SetCurrentItem(int index) {
24 setCurrentRow(AddSeparatorsToIndex(index));
25 }
26
27 void SideBar::SetBackgroundColor(QColor color) {
28 viewport()->setAutoFillBackground(color != Qt::transparent);
29 QPalette pal(palette());
30 pal.setColor(QPalette::Window, color);
31 setPalette(pal);
32 }
33
34 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) {
35 QListWidgetItem* item = new QListWidgetItem(this);
36 item->setText(name);
37 if (!icon.isNull())
38 item->setIcon(icon);
39 return item;
40 }
41
42 QIcon SideBar::CreateIcon(const char* file) {
43 QPixmap pixmap(file, "PNG");
44 QIcon result;
45 result.addPixmap(pixmap, QIcon::Normal);
46 result.addPixmap(pixmap, QIcon::Selected);
47 return result;
48 }
49
50 QListWidgetItem* SideBar::AddSeparator() {
51 QListWidgetItem* item = new QListWidgetItem(this);
52 QFrame* line = new QFrame(this);
53 line->setFrameShape(QFrame::HLine);
54 line->setFrameShadow(QFrame::Sunken);
55 line->setMouseTracking(true);
56 line->setEnabled(false);
57
58 setItemWidget(item, line);
59 item->setFlags(Qt::NoItemFlags);
60 item->setBackground(QBrush(Qt::transparent));
61 return item;
62 }
63
64 int SideBar::AddSeparatorsToIndex(int index) {
65 int i, j;
66 for (i = 0, j = 0; i < index;) {
67 i++;
68 if (IndexIsSeparator(indexFromItem(item(i))))
69 j++;
70 }
71 return i + j;
72 }
73
74 int SideBar::RemoveSeparatorsFromIndex(int index) {
75 int i, j;
76 for (i = 0, j = 0; i < index; i++) {
77 if (!IndexIsSeparator(indexFromItem(item(i))))
78 j++;
79 }
80 return j;
81 }
82
83 bool SideBar::IndexIsSeparator(QModelIndex index) const {
84 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled);
85 }
86
87 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const {
88 if (IndexIsSeparator(index))
89 return QItemSelectionModel::NoUpdate;
90 return QItemSelectionModel::ClearAndSelect;
91 }
92
93 void SideBar::mouseMoveEvent(QMouseEvent* event) {
94 if (!IndexIsSeparator(indexAt(event->pos())))
95 setCursor(Qt::PointingHandCursor);
96 else
97 unsetCursor();
98 QListView::mouseMoveEvent(event);
99 }
100
101 #include "gui/widgets/moc_sidebar.cpp"