annotate src/gui/sidebar.cpp @ 37:9ae9365dd4ea

window.cpp: fix sidebar on Linux
author Paper <mrpapersonic@gmail.com>
date Thu, 21 Sep 2023 17:15:43 -0400
parents 2743011a6042
children
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 setFrameShape(QFrame::NoFrame);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
10 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
11 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
12 setSelectionMode(QAbstractItemView::SingleSelection);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
13 setSelectionBehavior(QAbstractItemView::SelectItems);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
14 setMouseTracking(true);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
15 viewport()->setAutoFillBackground(false);
37
9ae9365dd4ea window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents: 36
diff changeset
16
36
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
17 QFont font;
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
18 font.setPointSize(9);
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
19 setFont(font);
37
9ae9365dd4ea window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents: 36
diff changeset
20
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
21 connect(this, &QListWidget::currentRowChanged, this,
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
22 [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
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) {
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
26 QListWidgetItem* item = new QListWidgetItem(this);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
27 item->setText(name);
6
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
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
33 QIcon SideBar::CreateIcon(const char* file) {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
34 QPixmap pixmap(file, "PNG");
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
35 QIcon result;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
36 result.addPixmap(pixmap, QIcon::Normal);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
37 result.addPixmap(pixmap, QIcon::Selected);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
38 return result;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
39 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
40
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
41 QListWidgetItem* SideBar::AddSeparator() {
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
42 QListWidgetItem* item = new QListWidgetItem(this);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
43 QFrame* line = new QFrame(this);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
44 line->setFrameShape(QFrame::HLine);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45 line->setFrameShadow(QFrame::Sunken);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
46 line->setMouseTracking(true);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
47 line->setEnabled(false);
36
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
48
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
49 QPalette pal;
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
50 pal.setColor(QPalette::Window, QColor(0, 0, 0, 0));
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
51 line->setPalette(pal);
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
52
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
53 setItemWidget(item, line);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
54 item->setFlags(Qt::NoItemFlags);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
55 return item;
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56 }
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
57
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
58 int SideBar::RemoveSeparatorsFromIndex(int index) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
59 int i, j;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
60 for (i = 0, j = 0; i < index; i++) {
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
61 if (!IndexIsSeparator(indexFromItem(item(i))))
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
62 j++;
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
63 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
64 return j;
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 bool SideBar::IndexIsSeparator(QModelIndex index) const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
68 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
69 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
70
36
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
71 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const {
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
72 if (IndexIsSeparator(index))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
73 return QItemSelectionModel::NoUpdate;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
74 return QItemSelectionModel::ClearAndSelect;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
75 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
76
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
77 void SideBar::mouseMoveEvent(QMouseEvent* event) {
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
78 if (!IndexIsSeparator(indexAt(event->pos())))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
79 setCursor(Qt::PointingHandCursor);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
80 else
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
81 unsetCursor();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
82 QListView::mouseMoveEvent(event);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
83 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
84
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
85 #include "gui/moc_sidebar.cpp"