Mercurial > minori
annotate src/gui/widgets/sidebar.cc @ 291:9a88e1725fd2
*: refactor lots of stuff
I forgot to put this into different commits, oops!
anyway, it doesn't really matter *that* much since this is an
unfinished hobby project anyway. once it starts getting stable
commit history will be more important, but for now it's not
that big of a deal
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Sun, 12 May 2024 16:31:07 -0400 |
| parents | 862d0d8619f6 |
| children | 99cbc51433e4 |
| 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 } |
| 34 | |
| 46 | 35 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
| 36 QListWidgetItem* item = new QListWidgetItem(this); | |
| 37 item->setText(name); | |
| 38 if (!icon.isNull()) | |
| 39 item->setIcon(icon); | |
| 40 return item; | |
| 41 } | |
| 42 | |
| 43 QIcon SideBar::CreateIcon(const char* file) { | |
| 44 QPixmap pixmap(file, "PNG"); | |
| 45 QIcon result; | |
| 46 result.addPixmap(pixmap, QIcon::Normal); | |
| 47 result.addPixmap(pixmap, QIcon::Selected); | |
| 48 return result; | |
| 49 } | |
| 50 | |
| 51 QListWidgetItem* SideBar::AddSeparator() { | |
| 52 QListWidgetItem* item = new QListWidgetItem(this); | |
| 53 QFrame* line = new QFrame(this); | |
| 54 line->setFrameShape(QFrame::HLine); | |
| 55 line->setFrameShadow(QFrame::Sunken); | |
| 56 line->setMouseTracking(true); | |
| 57 line->setEnabled(false); | |
| 58 | |
| 59 setItemWidget(item, line); | |
| 60 item->setFlags(Qt::NoItemFlags); | |
|
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
63
diff
changeset
|
61 item->setBackground(QBrush(Qt::transparent)); |
| 46 | 62 return item; |
| 63 } | |
| 64 | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
65 int SideBar::AddSeparatorsToIndex(int index) { |
|
252
a0eeb2cc7e6d
*: resolve make distcheck failures
Paper <paper@paper.us.eu.org>
parents:
250
diff
changeset
|
66 int separators = 0, items = 0; |
| 250 | 67 |
| 258 | 68 for (; items <= index;) { |
|
252
a0eeb2cc7e6d
*: resolve make distcheck failures
Paper <paper@paper.us.eu.org>
parents:
250
diff
changeset
|
69 if (IndexIsSeparator(indexFromItem(item(items + separators)))) { |
| 250 | 70 separators++; |
| 71 } else { | |
| 72 items++; | |
| 73 } | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
74 } |
| 250 | 75 |
| 76 return index + separators; | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
77 } |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
78 |
| 46 | 79 int SideBar::RemoveSeparatorsFromIndex(int index) { |
| 250 | 80 int i = 0, items = 0; |
| 81 for (; i < index; i++) { | |
| 46 | 82 if (!IndexIsSeparator(indexFromItem(item(i)))) |
| 250 | 83 items++; |
| 46 | 84 } |
| 250 | 85 return items; |
| 46 | 86 } |
| 87 | |
| 88 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
| 250 | 89 return !index.isValid() || !(index.flags() & Qt::ItemIsEnabled); |
| 46 | 90 } |
| 91 | |
| 92 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
| 93 if (IndexIsSeparator(index)) | |
| 94 return QItemSelectionModel::NoUpdate; | |
| 95 return QItemSelectionModel::ClearAndSelect; | |
| 96 } | |
| 97 | |
| 98 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
| 99 if (!IndexIsSeparator(indexAt(event->pos()))) | |
| 100 setCursor(Qt::PointingHandCursor); | |
| 101 else | |
| 102 unsetCursor(); | |
| 103 QListView::mouseMoveEvent(event); | |
| 104 } |
