Mercurial > minori
annotate src/sidebar.cpp @ 8:b1f73678ef61
update
text paragraphs are now their own objects, as they should be
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sat, 26 Aug 2023 03:39:34 -0400 |
| parents | 07a9095eaeed |
| children |
| rev | line source |
|---|---|
| 7 | 1 #include "sidebar.h" |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
2 #include <QListWidget> |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
3 #include <QListWidgetItem> |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
4 #include <QFrame> |
| 7 | 5 #include <QMouseEvent> |
| 6 #include <QMessageBox> | |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
7 |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
8 SideBar::SideBar(QWidget *parent) |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
9 : QListWidget(parent) |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
10 { |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
11 setObjectName("sidebar"); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
12 setFrameShape(QFrame::NoFrame); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
13 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
14 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 7 | 15 setSelectionMode(QAbstractItemView::SingleSelection); |
| 16 setSelectionBehavior(QAbstractItemView::SelectItems); | |
| 17 setMouseTracking(true); | |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
18 viewport()->setAutoFillBackground(false); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
19 setStyleSheet("font-size: 12px"); |
| 7 | 20 connect(this, &QListWidget::currentRowChanged, this, [this](int index){ |
| 21 emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); | |
| 22 }); | |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
23 } |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
24 |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
25 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
26 QListWidgetItem* item = new QListWidgetItem(this); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
27 item->setText(name); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
28 if (!icon.isNull()) |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
29 item->setIcon(icon); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
30 return item; |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
31 } |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
32 |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
33 QListWidgetItem* SideBar::AddSeparator() { |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
34 QListWidgetItem* item = new QListWidgetItem(this); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
35 setStyleSheet("QListWidget::item:disabled {background: transparent;}"); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
36 QFrame* line = new QFrame(this); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
37 line->setFrameShape(QFrame::HLine); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
38 line->setFrameShadow(QFrame::Sunken); |
| 7 | 39 line->setMouseTracking(true); |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
40 line->setEnabled(false); |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
41 setItemWidget(item, line); |
| 7 | 42 item->setFlags(Qt::NoItemFlags); |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
43 return item; |
|
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
1
diff
changeset
|
44 } |
| 7 | 45 |
| 46 int SideBar::RemoveSeparatorsFromIndex(int index) { | |
| 47 int i, j; | |
| 48 for (i = 0, j = 0; i < index; i++) { | |
| 49 if (!IndexIsSeparator(indexFromItem(item(i)))) j++; | |
| 50 } | |
| 51 return j; | |
| 52 } | |
| 53 | |
| 54 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
| 55 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled); | |
| 56 } | |
| 57 | |
| 58 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex & index, | |
| 59 const QEvent * event) const { | |
| 60 if (IndexIsSeparator(index)) | |
| 61 return QItemSelectionModel::NoUpdate; | |
| 62 return QItemSelectionModel::ClearAndSelect; | |
| 63 /* silence unused parameter warnings */ | |
| 64 (void)event; | |
| 65 } | |
| 66 | |
| 67 void SideBar::mouseMoveEvent(QMouseEvent *event) { | |
| 68 if (!IndexIsSeparator(indexAt(event->pos()))) | |
| 69 setCursor(Qt::PointingHandCursor); | |
| 70 else | |
| 71 unsetCursor(); | |
| 72 QListView::mouseMoveEvent(event); | |
| 73 } | |
| 74 | |
| 75 #include "moc_sidebar.cpp" |
