Mercurial > minori
annotate src/gui/widgets/sidebar.cpp @ 69:27a19dd6cba1
*: fix up some stuff
settings: due to my own incompetence OnOK caused the entire program to crash,
now that's fixed :)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 03 Oct 2023 03:38:25 -0400 |
parents | 2417121d894e |
children | 5f9bdcea3d01 |
rev | line source |
---|---|
46 | 1 #include "gui/widgets/sidebar.h" |
63 | 2 #include <QDebug> |
46 | 3 #include <QFrame> |
4 #include <QListWidget> | |
5 #include <QListWidgetItem> | |
6 #include <QMessageBox> | |
7 #include <QMouseEvent> | |
8 | |
9 SideBar::SideBar(QWidget* parent) : QListWidget(parent) { | |
10 setFrameShape(QFrame::NoFrame); | |
11 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
12 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
13 setSelectionMode(QAbstractItemView::SingleSelection); | |
14 setSelectionBehavior(QAbstractItemView::SelectItems); | |
15 setMouseTracking(true); | |
16 /* FIXME: is there an easy way to do this with palettes? */ | |
17 setStyleSheet("QListWidget::item:disabled { background: transparent }"); | |
69 | 18 |
19 SetBackgroundColor(Qt::transparent); | |
46 | 20 |
21 QFont font; | |
22 font.setPointSize(9); | |
23 setFont(font); | |
24 | |
25 connect(this, &QListWidget::currentRowChanged, this, | |
26 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); | |
27 } | |
28 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
29 void SideBar::SetCurrentItem(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
30 setCurrentRow(AddSeparatorsToIndex(index)); |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
31 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
32 |
69 | 33 void SideBar::SetBackgroundColor(QColor color) { |
34 QPalette pal(palette()); | |
35 pal.setColor(QPalette::Window, color); | |
36 setPalette(pal); | |
37 } | |
38 | |
46 | 39 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
40 QListWidgetItem* item = new QListWidgetItem(this); | |
41 item->setText(name); | |
42 if (!icon.isNull()) | |
43 item->setIcon(icon); | |
44 return item; | |
45 } | |
46 | |
47 QIcon SideBar::CreateIcon(const char* file) { | |
48 QPixmap pixmap(file, "PNG"); | |
49 QIcon result; | |
50 result.addPixmap(pixmap, QIcon::Normal); | |
51 result.addPixmap(pixmap, QIcon::Selected); | |
52 return result; | |
53 } | |
54 | |
55 QListWidgetItem* SideBar::AddSeparator() { | |
56 QListWidgetItem* item = new QListWidgetItem(this); | |
57 QFrame* line = new QFrame(this); | |
58 line->setFrameShape(QFrame::HLine); | |
59 line->setFrameShadow(QFrame::Sunken); | |
60 line->setMouseTracking(true); | |
61 line->setEnabled(false); | |
62 | |
63 setItemWidget(item, line); | |
64 item->setFlags(Qt::NoItemFlags); | |
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
63
diff
changeset
|
65 item->setBackground(QBrush(Qt::transparent)); |
46 | 66 return item; |
67 } | |
68 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
69 int SideBar::AddSeparatorsToIndex(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
70 int i, j; |
63 | 71 for (i = 0, j = 0; i < index;) { |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
72 i++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
73 if (IndexIsSeparator(indexFromItem(item(i)))) |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
74 j++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
75 } |
63 | 76 return i + j; |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
77 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
78 |
46 | 79 int SideBar::RemoveSeparatorsFromIndex(int index) { |
80 int i, j; | |
81 for (i = 0, j = 0; i < index; i++) { | |
82 if (!IndexIsSeparator(indexFromItem(item(i)))) | |
83 j++; | |
84 } | |
85 return j; | |
86 } | |
87 | |
88 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
89 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled); | |
90 } | |
91 | |
92 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
93 if (IndexIsSeparator(index)) | |
94 return QItemSelectionModel::NoUpdate; | |
95 return QItemSelectionModel::ClearAndSelect; | |
96 } | |
97 | |
98 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
99 if (!IndexIsSeparator(indexAt(event->pos()))) | |
100 setCursor(Qt::PointingHandCursor); | |
101 else | |
102 unsetCursor(); | |
103 QListView::mouseMoveEvent(event); | |
104 } | |
105 | |
106 #include "gui/widgets/moc_sidebar.cpp" |