annotate src/gui/sidebar.cpp @ 11:fc1bf97c528b

*: use C++11 standard I've been meaning to do this for a while, but I didn't want to reimplement the filesystem code. Now we are on C++11 and most compilers from the past 5 centuries should support this now
author Paper <mrpapersonic@gmail.com>
date Sun, 17 Sep 2023 06:14:30 -0400
parents 5c0397762b53
children cde8f67a7c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
1 #include "gui/sidebar.h"
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
2 #include <QFrame>
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
3 #include <QListWidget>
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
4 #include <QListWidgetItem>
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
5 #include <QMessageBox>
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
6 #include <QMouseEvent>
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
7
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
8 SideBar::SideBar(QWidget* parent) : QListWidget(parent) {
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
9 setObjectName("sidebar");
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
10 setFrameShape(QFrame::NoFrame);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
11 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
12 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
13 setSelectionMode(QAbstractItemView::SingleSelection);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
14 setSelectionBehavior(QAbstractItemView::SelectItems);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
15 setMouseTracking(true);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
16 viewport()->setAutoFillBackground(false);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
17 setStyleSheet("font-size: 12px");
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
18 connect(this, &QListWidget::currentRowChanged, this,
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
19 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); });
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
20 }
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
21
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
22 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) {
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
23 QListWidgetItem* item = new QListWidgetItem(this);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
24 item->setText(name);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
25 if (!icon.isNull())
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
26 item->setIcon(icon);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
27 return item;
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
28 }
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
29
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
30 QIcon SideBar::CreateIcon(const char* file) {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
31 QPixmap pixmap(file, "PNG");
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
32 QIcon result;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
33 result.addPixmap(pixmap, QIcon::Normal);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
34 result.addPixmap(pixmap, QIcon::Selected);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
35 return result;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
36 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
37
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
38 QListWidgetItem* SideBar::AddSeparator() {
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
39 QListWidgetItem* item = new QListWidgetItem(this);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
40 setStyleSheet("QListWidget::item:disabled {background: transparent;}");
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
41 QFrame* line = new QFrame(this);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
42 line->setFrameShape(QFrame::HLine);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
43 line->setFrameShadow(QFrame::Sunken);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
44 line->setMouseTracking(true);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45 line->setEnabled(false);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
46 setItemWidget(item, line);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
47 item->setFlags(Qt::NoItemFlags);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
48 return item;
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
49 }
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
50
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
51 int SideBar::RemoveSeparatorsFromIndex(int index) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
52 int i, j;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
53 for (i = 0, j = 0; i < index; i++) {
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
54 if (!IndexIsSeparator(indexFromItem(item(i))))
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
55 j++;
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
56 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
57 return j;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
58 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
59
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
60 bool SideBar::IndexIsSeparator(QModelIndex index) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
61 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
62 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
63
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
64 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const {
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
65 if (IndexIsSeparator(index))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
66 return QItemSelectionModel::NoUpdate;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
67 return QItemSelectionModel::ClearAndSelect;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
68 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
69
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
70 void SideBar::mouseMoveEvent(QMouseEvent* event) {
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
71 if (!IndexIsSeparator(indexAt(event->pos())))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
72 setCursor(Qt::PointingHandCursor);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
73 else
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
74 unsetCursor();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
75 QListView::mouseMoveEvent(event);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
76 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
77
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
78 #include "gui/moc_sidebar.cpp"