Mercurial > minori
annotate src/gui/widgets/sidebar.cc @ 105:6d8da6e64d61
theme: add dark stylesheet, make it actually usable
win32: make the titlebar black where available
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 05 Nov 2023 03:54:26 -0500 |
parents | 9b2b41f83a5e |
children | 80f49f623d30 |
rev | line source |
---|---|
46 | 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 }"); | |
69 | 16 |
17 SetBackgroundColor(Qt::transparent); | |
46 | 18 |
19 connect(this, &QListWidget::currentRowChanged, this, | |
20 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); | |
21 } | |
22 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
23 void SideBar::SetCurrentItem(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
24 setCurrentRow(AddSeparatorsToIndex(index)); |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
25 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
26 |
69 | 27 void SideBar::SetBackgroundColor(QColor color) { |
71
5f9bdcea3d01
sidebar: fix regression caused by SetBackgroundColor()
Paper <mrpapersonic@gmail.com>
parents:
69
diff
changeset
|
28 viewport()->setAutoFillBackground(color != Qt::transparent); |
69 | 29 QPalette pal(palette()); |
30 pal.setColor(QPalette::Window, color); | |
31 setPalette(pal); | |
32 } | |
33 | |
46 | 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); | |
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
63
diff
changeset
|
60 item->setBackground(QBrush(Qt::transparent)); |
46 | 61 return item; |
62 } | |
63 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
64 int SideBar::AddSeparatorsToIndex(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
65 int i, j; |
63 | 66 for (i = 0, j = 0; i < index;) { |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
67 i++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
68 if (IndexIsSeparator(indexFromItem(item(i)))) |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
69 j++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
70 } |
63 | 71 return i + j; |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
72 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
73 |
46 | 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" |