Mercurial > minori
annotate src/gui/widgets/sidebar.cc @ 187:9613d72b097e
*: multiple performance improvements
like marking `static const` when it makes sense...
date: change old stupid heap-based method to a structure which should
make copying the thing actually make a copy.
also many performance-based changes, like removing the std::tie
dependency and forward-declaring nlohmann json
*: replace every instance of QString::fromUtf8 to Strings::ToQString.
the main difference is that our function will always convert exactly
what is in the string, while some other times it would only convert
up to the nearest NUL byte
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Wed, 06 Dec 2023 13:43:54 -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" |
