Mercurial > minori
annotate src/gui/widgets/sidebar.cc @ 137:69db40272acd
dep/animia: [WIP] huge refactor
this WILL NOT compile, because lots of code has been changed
and every API in the original codebase has been removed.
note that this api setup is not exactly permanent...
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Fri, 10 Nov 2023 13:52:47 -0500 |
| parents | 80f49f623d30 |
| children | 4d461ef7d424 |
| rev | line source |
|---|---|
| 46 | 1 #include "gui/widgets/sidebar.h" |
| 2 #include <QFrame> | |
| 3 #include <QListWidget> | |
| 4 #include <QListWidgetItem> | |
| 5 #include <QMouseEvent> | |
| 6 | |
| 7 SideBar::SideBar(QWidget* parent) : QListWidget(parent) { | |
| 8 setFrameShape(QFrame::NoFrame); | |
| 9 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 10 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
| 11 setSelectionMode(QAbstractItemView::SingleSelection); | |
| 12 setSelectionBehavior(QAbstractItemView::SelectItems); | |
| 13 setMouseTracking(true); | |
| 14 /* FIXME: is there an easy way to do this with palettes? */ | |
| 15 setStyleSheet("QListWidget::item:disabled { background: transparent }"); | |
| 69 | 16 |
| 17 SetBackgroundColor(Qt::transparent); | |
| 46 | 18 |
| 19 connect(this, &QListWidget::currentRowChanged, this, | |
| 20 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); | |
| 21 } | |
| 22 | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
23 void SideBar::SetCurrentItem(int index) { |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
24 setCurrentRow(AddSeparatorsToIndex(index)); |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
25 } |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
26 |
|
112
80f49f623d30
locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
27 int SideBar::GetCurrentItem() { |
|
80f49f623d30
locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
28 return RemoveSeparatorsFromIndex(currentRow()); |
|
80f49f623d30
locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
29 } |
|
80f49f623d30
locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
30 |
| 69 | 31 void SideBar::SetBackgroundColor(QColor color) { |
|
71
5f9bdcea3d01
sidebar: fix regression caused by SetBackgroundColor()
Paper <mrpapersonic@gmail.com>
parents:
69
diff
changeset
|
32 viewport()->setAutoFillBackground(color != Qt::transparent); |
| 69 | 33 QPalette pal(palette()); |
| 34 pal.setColor(QPalette::Window, color); | |
| 35 setPalette(pal); | |
| 36 } | |
| 37 | |
| 46 | 38 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
| 39 QListWidgetItem* item = new QListWidgetItem(this); | |
| 40 item->setText(name); | |
| 41 if (!icon.isNull()) | |
| 42 item->setIcon(icon); | |
| 43 return item; | |
| 44 } | |
| 45 | |
| 46 QIcon SideBar::CreateIcon(const char* file) { | |
| 47 QPixmap pixmap(file, "PNG"); | |
| 48 QIcon result; | |
| 49 result.addPixmap(pixmap, QIcon::Normal); | |
| 50 result.addPixmap(pixmap, QIcon::Selected); | |
| 51 return result; | |
| 52 } | |
| 53 | |
| 54 QListWidgetItem* SideBar::AddSeparator() { | |
| 55 QListWidgetItem* item = new QListWidgetItem(this); | |
| 56 QFrame* line = new QFrame(this); | |
| 57 line->setFrameShape(QFrame::HLine); | |
| 58 line->setFrameShadow(QFrame::Sunken); | |
| 59 line->setMouseTracking(true); | |
| 60 line->setEnabled(false); | |
| 61 | |
| 62 setItemWidget(item, line); | |
| 63 item->setFlags(Qt::NoItemFlags); | |
|
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
63
diff
changeset
|
64 item->setBackground(QBrush(Qt::transparent)); |
| 46 | 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; |
| 63 | 70 for (i = 0, j = 0; i < index;) { |
|
58
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 } |
| 63 | 75 return i + j; |
|
58
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" |
