annotate src/sidebar.cpp @ 7:07a9095eaeed

Update Refactored some code, moved some around
author Paper <mrpapersonic@gmail.com>
date Thu, 24 Aug 2023 23:11:38 -0400
parents 1d82f6e04d7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
5 #include <QMouseEvent>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
15 setSelectionMode(QAbstractItemView::SingleSelection);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
16 setSelectionBehavior(QAbstractItemView::SelectItems);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
20 connect(this, &QListWidget::currentRowChanged, this, [this](int index){
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
21 emit CurrentItemChanged(RemoveSeparatorsFromIndex(index));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
45
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
46 int SideBar::RemoveSeparatorsFromIndex(int index) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
47 int i, j;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
48 for (i = 0, j = 0; i < index; i++) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
49 if (!IndexIsSeparator(indexFromItem(item(i)))) j++;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
50 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
51 return j;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
52 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
53
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
54 bool SideBar::IndexIsSeparator(QModelIndex index) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
55 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
56 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
57
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
58 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex & index,
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
59 const QEvent * event) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
60 if (IndexIsSeparator(index))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
61 return QItemSelectionModel::NoUpdate;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
62 return QItemSelectionModel::ClearAndSelect;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
63 /* silence unused parameter warnings */
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
64 (void)event;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
65 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
66
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
67 void SideBar::mouseMoveEvent(QMouseEvent *event) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
68 if (!IndexIsSeparator(indexAt(event->pos())))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
69 setCursor(Qt::PointingHandCursor);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
70 else
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
71 unsetCursor();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
72 QListView::mouseMoveEvent(event);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
73 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
74
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
75 #include "moc_sidebar.cpp"