Mercurial > minori
annotate src/gui/widgets/sidebar.cpp @ 68:2417121d894e
*: normalize usage of layouts
before, I used them two ways, once was by setting the layout later
by using setLayout(QWidget), and the other was just using the constructor.
I find the constructor to be easier to read, so I chose that one.
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Mon, 02 Oct 2023 21:33:25 -0400 |
| parents | 3d2decf093bb |
| children | 27a19dd6cba1 |
| rev | line source |
|---|---|
| 46 | 1 #include "gui/widgets/sidebar.h" |
| 63 | 2 #include <QDebug> |
| 46 | 3 #include <QFrame> |
| 4 #include <QListWidget> | |
| 5 #include <QListWidgetItem> | |
| 6 #include <QMessageBox> | |
| 7 #include <QMouseEvent> | |
| 8 | |
| 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 }"); | |
| 18 viewport()->setAutoFillBackground(false); | |
| 19 | |
| 20 QFont font; | |
| 21 font.setPointSize(9); | |
| 22 setFont(font); | |
| 23 | |
| 24 connect(this, &QListWidget::currentRowChanged, this, | |
| 25 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); | |
| 26 } | |
| 27 | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
28 void SideBar::SetCurrentItem(int index) { |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
29 setCurrentRow(AddSeparatorsToIndex(index)); |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
30 } |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
31 |
| 46 | 32 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
| 33 QListWidgetItem* item = new QListWidgetItem(this); | |
| 34 item->setText(name); | |
| 35 if (!icon.isNull()) | |
| 36 item->setIcon(icon); | |
| 37 return item; | |
| 38 } | |
| 39 | |
| 40 QIcon SideBar::CreateIcon(const char* file) { | |
| 41 QPixmap pixmap(file, "PNG"); | |
| 42 QIcon result; | |
| 43 result.addPixmap(pixmap, QIcon::Normal); | |
| 44 result.addPixmap(pixmap, QIcon::Selected); | |
| 45 return result; | |
| 46 } | |
| 47 | |
| 48 QListWidgetItem* SideBar::AddSeparator() { | |
| 49 QListWidgetItem* item = new QListWidgetItem(this); | |
| 50 QFrame* line = new QFrame(this); | |
| 51 line->setFrameShape(QFrame::HLine); | |
| 52 line->setFrameShadow(QFrame::Sunken); | |
| 53 line->setMouseTracking(true); | |
| 54 line->setEnabled(false); | |
| 55 | |
| 56 setItemWidget(item, line); | |
| 57 item->setFlags(Qt::NoItemFlags); | |
|
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
63
diff
changeset
|
58 item->setBackground(QBrush(Qt::transparent)); |
| 46 | 59 return item; |
| 60 } | |
| 61 | |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
62 int SideBar::AddSeparatorsToIndex(int index) { |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
63 int i, j; |
| 63 | 64 for (i = 0, j = 0; i < index;) { |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
65 i++; |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
66 if (IndexIsSeparator(indexFromItem(item(i)))) |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
67 j++; |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
68 } |
| 63 | 69 return i + j; |
|
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
70 } |
|
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
71 |
| 46 | 72 int SideBar::RemoveSeparatorsFromIndex(int index) { |
| 73 int i, j; | |
| 74 for (i = 0, j = 0; i < index; i++) { | |
| 75 if (!IndexIsSeparator(indexFromItem(item(i)))) | |
| 76 j++; | |
| 77 } | |
| 78 return j; | |
| 79 } | |
| 80 | |
| 81 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
| 82 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled); | |
| 83 } | |
| 84 | |
| 85 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
| 86 if (IndexIsSeparator(index)) | |
| 87 return QItemSelectionModel::NoUpdate; | |
| 88 return QItemSelectionModel::ClearAndSelect; | |
| 89 } | |
| 90 | |
| 91 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
| 92 if (!IndexIsSeparator(indexAt(event->pos()))) | |
| 93 setCursor(Qt::PointingHandCursor); | |
| 94 else | |
| 95 unsetCursor(); | |
| 96 QListView::mouseMoveEvent(event); | |
| 97 } | |
| 98 | |
| 99 #include "gui/widgets/moc_sidebar.cpp" |
