Mercurial > minori
annotate src/gui/widgets/sidebar.cpp @ 58:b7a1c0010ffd
sidebar: link view menu and sidebar together
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 28 Sep 2023 13:32:21 -0400 |
parents | d0adc4aedfc8 |
children | 4c6dd5999b39 |
rev | line source |
---|---|
46 | 1 #include "gui/widgets/sidebar.h" |
2 #include <QFrame> | |
3 #include <QListWidget> | |
4 #include <QListWidgetItem> | |
5 #include <QMessageBox> | |
6 #include <QMouseEvent> | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
7 #include <QDebug> |
46 | 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 qDebug() << AddSeparatorsToIndex(index) << "\n"; |
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 |
46 | 33 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
34 QListWidgetItem* item = new QListWidgetItem(this); | |
35 item->setText(name); | |
36 if (!icon.isNull()) | |
37 item->setIcon(icon); | |
38 return item; | |
39 } | |
40 | |
41 QIcon SideBar::CreateIcon(const char* file) { | |
42 QPixmap pixmap(file, "PNG"); | |
43 QIcon result; | |
44 result.addPixmap(pixmap, QIcon::Normal); | |
45 result.addPixmap(pixmap, QIcon::Selected); | |
46 return result; | |
47 } | |
48 | |
49 QListWidgetItem* SideBar::AddSeparator() { | |
50 QListWidgetItem* item = new QListWidgetItem(this); | |
51 QFrame* line = new QFrame(this); | |
52 line->setFrameShape(QFrame::HLine); | |
53 line->setFrameShadow(QFrame::Sunken); | |
54 line->setMouseTracking(true); | |
55 line->setEnabled(false); | |
56 | |
57 /* | |
58 QPalette pal; | |
59 pal.setColor(QPalette::Window, Qt::transparent); | |
60 line->setPalette(pal); | |
61 */ | |
62 | |
63 setItemWidget(item, line); | |
64 item->setFlags(Qt::NoItemFlags); | |
65 return item; | |
66 } | |
67 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
68 int SideBar::AddSeparatorsToIndex(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
69 int i, j; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
70 for (i = 0, j = 0; i < index; ) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
71 i++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
72 if (IndexIsSeparator(indexFromItem(item(i)))) |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
73 j++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
74 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
75 return i+j; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
76 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
77 |
46 | 78 int SideBar::RemoveSeparatorsFromIndex(int index) { |
79 int i, j; | |
80 for (i = 0, j = 0; i < index; i++) { | |
81 if (!IndexIsSeparator(indexFromItem(item(i)))) | |
82 j++; | |
83 } | |
84 return j; | |
85 } | |
86 | |
87 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
88 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled); | |
89 } | |
90 | |
91 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
92 if (IndexIsSeparator(index)) | |
93 return QItemSelectionModel::NoUpdate; | |
94 return QItemSelectionModel::ClearAndSelect; | |
95 } | |
96 | |
97 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
98 if (!IndexIsSeparator(indexAt(event->pos()))) | |
99 setCursor(Qt::PointingHandCursor); | |
100 else | |
101 unsetCursor(); | |
102 QListView::mouseMoveEvent(event); | |
103 } | |
104 | |
105 #include "gui/widgets/moc_sidebar.cpp" |