Mercurial > minori
annotate src/gui/sidebar.cpp @ 37:9ae9365dd4ea
window.cpp: fix sidebar on Linux
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Thu, 21 Sep 2023 17:15:43 -0400 |
| parents | 2743011a6042 |
| children |
| rev | line source |
|---|---|
| 9 | 1 #include "gui/sidebar.h" |
| 2 #include <QFrame> | |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
3 #include <QListWidget> |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
4 #include <QListWidgetItem> |
| 9 | 5 #include <QMessageBox> |
| 7 | 6 #include <QMouseEvent> |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
7 |
| 9 | 8 SideBar::SideBar(QWidget* parent) : QListWidget(parent) { |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
9 setFrameShape(QFrame::NoFrame); |
| 9 | 10 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 11 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 7 | 12 setSelectionMode(QAbstractItemView::SingleSelection); |
| 13 setSelectionBehavior(QAbstractItemView::SelectItems); | |
| 14 setMouseTracking(true); | |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
15 viewport()->setAutoFillBackground(false); |
|
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
16 |
| 36 | 17 QFont font; |
| 18 font.setPointSize(9); | |
| 19 setFont(font); | |
|
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
20 |
| 9 | 21 connect(this, &QListWidget::currentRowChanged, this, |
| 15 | 22 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
23 } |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
24 |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
25 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
| 9 | 26 QListWidgetItem* item = new QListWidgetItem(this); |
| 27 item->setText(name); | |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
28 if (!icon.isNull()) |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
29 item->setIcon(icon); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
30 return item; |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
31 } |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
32 |
| 9 | 33 QIcon SideBar::CreateIcon(const char* file) { |
| 34 QPixmap pixmap(file, "PNG"); | |
| 35 QIcon result; | |
| 36 result.addPixmap(pixmap, QIcon::Normal); | |
| 37 result.addPixmap(pixmap, QIcon::Selected); | |
| 38 return result; | |
| 39 } | |
| 40 | |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
41 QListWidgetItem* SideBar::AddSeparator() { |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
42 QListWidgetItem* item = new QListWidgetItem(this); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
43 QFrame* line = new QFrame(this); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
44 line->setFrameShape(QFrame::HLine); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
45 line->setFrameShadow(QFrame::Sunken); |
| 7 | 46 line->setMouseTracking(true); |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
47 line->setEnabled(false); |
| 36 | 48 |
| 49 QPalette pal; | |
| 50 pal.setColor(QPalette::Window, QColor(0, 0, 0, 0)); | |
| 51 line->setPalette(pal); | |
| 52 | |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
53 setItemWidget(item, line); |
| 7 | 54 item->setFlags(Qt::NoItemFlags); |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
55 return item; |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
56 } |
| 7 | 57 |
| 58 int SideBar::RemoveSeparatorsFromIndex(int index) { | |
| 59 int i, j; | |
| 60 for (i = 0, j = 0; i < index; i++) { | |
| 9 | 61 if (!IndexIsSeparator(indexFromItem(item(i)))) |
| 62 j++; | |
| 7 | 63 } |
| 64 return j; | |
| 65 } | |
| 66 | |
| 67 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
| 68 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled); | |
| 69 } | |
| 70 | |
| 36 | 71 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { |
| 7 | 72 if (IndexIsSeparator(index)) |
| 73 return QItemSelectionModel::NoUpdate; | |
| 74 return QItemSelectionModel::ClearAndSelect; | |
| 75 } | |
| 76 | |
| 9 | 77 void SideBar::mouseMoveEvent(QMouseEvent* event) { |
| 7 | 78 if (!IndexIsSeparator(indexAt(event->pos()))) |
| 79 setCursor(Qt::PointingHandCursor); | |
| 80 else | |
| 81 unsetCursor(); | |
| 82 QListView::mouseMoveEvent(event); | |
| 83 } | |
| 84 | |
| 9 | 85 #include "gui/moc_sidebar.cpp" |
