Mercurial > minori
annotate src/gui/widgets/sidebar.cpp @ 63:3d2decf093bb
*: fix many clang warnings
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 01 Oct 2023 06:39:47 -0400 |
parents | 4c6dd5999b39 |
children | 2417121d894e |
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 }"); | |
18 viewport()->setAutoFillBackground(false); | |
19 | |
20 QFont font; | |
21 font.setPointSize(9); | |
22 setFont(font); | |
23 | |
24 connect(this, &QListWidget::currentRowChanged, this, | |
25 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); | |
26 } | |
27 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
28 void SideBar::SetCurrentItem(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
29 setCurrentRow(AddSeparatorsToIndex(index)); |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
30 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
31 |
46 | 32 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
33 QListWidgetItem* item = new QListWidgetItem(this); | |
34 item->setText(name); | |
35 if (!icon.isNull()) | |
36 item->setIcon(icon); | |
37 return item; | |
38 } | |
39 | |
40 QIcon SideBar::CreateIcon(const char* file) { | |
41 QPixmap pixmap(file, "PNG"); | |
42 QIcon result; | |
43 result.addPixmap(pixmap, QIcon::Normal); | |
44 result.addPixmap(pixmap, QIcon::Selected); | |
45 return result; | |
46 } | |
47 | |
48 QListWidgetItem* SideBar::AddSeparator() { | |
49 QListWidgetItem* item = new QListWidgetItem(this); | |
50 QFrame* line = new QFrame(this); | |
51 line->setFrameShape(QFrame::HLine); | |
52 line->setFrameShadow(QFrame::Sunken); | |
53 line->setMouseTracking(true); | |
54 line->setEnabled(false); | |
55 | |
56 /* | |
57 QPalette pal; | |
58 pal.setColor(QPalette::Window, Qt::transparent); | |
59 line->setPalette(pal); | |
60 */ | |
61 | |
62 setItemWidget(item, line); | |
63 item->setFlags(Qt::NoItemFlags); | |
64 return item; | |
65 } | |
66 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
67 int SideBar::AddSeparatorsToIndex(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
68 int i, j; |
63 | 69 for (i = 0, j = 0; i < index;) { |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
70 i++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
71 if (IndexIsSeparator(indexFromItem(item(i)))) |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
72 j++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
73 } |
63 | 74 return i + j; |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
75 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
76 |
46 | 77 int SideBar::RemoveSeparatorsFromIndex(int index) { |
78 int i, j; | |
79 for (i = 0, j = 0; i < index; i++) { | |
80 if (!IndexIsSeparator(indexFromItem(item(i)))) | |
81 j++; | |
82 } | |
83 return j; | |
84 } | |
85 | |
86 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
87 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled); | |
88 } | |
89 | |
90 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
91 if (IndexIsSeparator(index)) | |
92 return QItemSelectionModel::NoUpdate; | |
93 return QItemSelectionModel::ClearAndSelect; | |
94 } | |
95 | |
96 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
97 if (!IndexIsSeparator(indexAt(event->pos()))) | |
98 setCursor(Qt::PointingHandCursor); | |
99 else | |
100 unsetCursor(); | |
101 QListView::mouseMoveEvent(event); | |
102 } | |
103 | |
104 #include "gui/widgets/moc_sidebar.cpp" |