Mercurial > minori
annotate src/gui/widgets/sidebar.cc @ 250:c130f47f6f48
*: many many changes
e.g. the search page is actually implemented now!
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Sun, 04 Feb 2024 21:17:17 -0500 |
| parents | 4d461ef7d424 |
| children | a0eeb2cc7e6d |
| rev | line source |
|---|---|
| 46 | 1 #include "gui/widgets/sidebar.h" |
| 2 #include <QFrame> | |
| 3 #include <QListWidget> | |
| 4 #include <QListWidgetItem> | |
| 5 #include <QMouseEvent> | |
| 6 | |
| 250 | 7 #include <iostream> |
| 8 | |
| 46 | 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 connect(this, &QListWidget::currentRowChanged, this, | |
| 22 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); | |
| 23 } | |
| 24 | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
25 void SideBar::SetCurrentItem(int index) { |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
26 setCurrentRow(AddSeparatorsToIndex(index)); |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
27 } |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
28 |
|
112
80f49f623d30
locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
29 int SideBar::GetCurrentItem() { |
|
80f49f623d30
locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
30 return RemoveSeparatorsFromIndex(currentRow()); |
|
80f49f623d30
locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
31 } |
|
80f49f623d30
locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
32 |
| 69 | 33 void SideBar::SetBackgroundColor(QColor color) { |
|
71
5f9bdcea3d01
sidebar: fix regression caused by SetBackgroundColor()
Paper <mrpapersonic@gmail.com>
parents:
69
diff
changeset
|
34 viewport()->setAutoFillBackground(color != Qt::transparent); |
| 250 | 35 |
| 69 | 36 QPalette pal(palette()); |
| 37 pal.setColor(QPalette::Window, color); | |
| 38 setPalette(pal); | |
| 39 } | |
| 40 | |
| 46 | 41 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
| 42 QListWidgetItem* item = new QListWidgetItem(this); | |
| 43 item->setText(name); | |
| 44 if (!icon.isNull()) | |
| 45 item->setIcon(icon); | |
| 46 return item; | |
| 47 } | |
| 48 | |
| 49 QIcon SideBar::CreateIcon(const char* file) { | |
| 50 QPixmap pixmap(file, "PNG"); | |
| 51 QIcon result; | |
| 52 result.addPixmap(pixmap, QIcon::Normal); | |
| 53 result.addPixmap(pixmap, QIcon::Selected); | |
| 54 return result; | |
| 55 } | |
| 56 | |
| 57 QListWidgetItem* SideBar::AddSeparator() { | |
| 58 QListWidgetItem* item = new QListWidgetItem(this); | |
| 59 QFrame* line = new QFrame(this); | |
| 60 line->setFrameShape(QFrame::HLine); | |
| 61 line->setFrameShadow(QFrame::Sunken); | |
| 62 line->setMouseTracking(true); | |
| 63 line->setEnabled(false); | |
| 64 | |
| 65 setItemWidget(item, line); | |
| 66 item->setFlags(Qt::NoItemFlags); | |
|
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
63
diff
changeset
|
67 item->setBackground(QBrush(Qt::transparent)); |
| 46 | 68 return item; |
| 69 } | |
| 70 | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
71 int SideBar::AddSeparatorsToIndex(int index) { |
| 250 | 72 int i = 0, separators = 0, items = 0; |
| 73 | |
| 74 for (; items <= index; ) { | |
| 75 if (IndexIsSeparator(indexFromItem(item(i++)))) { | |
| 76 separators++; | |
| 77 } else { | |
| 78 items++; | |
| 79 } | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
80 } |
| 250 | 81 |
| 82 return index + separators; | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
83 } |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
84 |
| 46 | 85 int SideBar::RemoveSeparatorsFromIndex(int index) { |
| 250 | 86 int i = 0, items = 0; |
| 87 for (; i < index; i++) { | |
| 46 | 88 if (!IndexIsSeparator(indexFromItem(item(i)))) |
| 250 | 89 items++; |
| 46 | 90 } |
| 250 | 91 return items; |
| 46 | 92 } |
| 93 | |
| 94 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
| 250 | 95 return !index.isValid() || !(index.flags() & Qt::ItemIsEnabled); |
| 46 | 96 } |
| 97 | |
| 98 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
| 99 if (IndexIsSeparator(index)) | |
| 100 return QItemSelectionModel::NoUpdate; | |
| 101 return QItemSelectionModel::ClearAndSelect; | |
| 102 } | |
| 103 | |
| 104 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
| 105 if (!IndexIsSeparator(indexAt(event->pos()))) | |
| 106 setCursor(Qt::PointingHandCursor); | |
| 107 else | |
| 108 unsetCursor(); | |
| 109 QListView::mouseMoveEvent(event); | |
| 110 } |
