annotate src/gui/widgets/sidebar.cc @ 254:d14f8e0e40c3

[UNFINISHED] *: update anime button
author Paper <paper@paper.us.eu.org>
date Wed, 07 Feb 2024 07:57:37 -0500
parents a0eeb2cc7e6d
children 862d0d8619f6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include "gui/widgets/sidebar.h"
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #include <QFrame>
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include <QListWidget>
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <QListWidgetItem>
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <QMouseEvent>
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 SideBar::SideBar(QWidget* parent) : QListWidget(parent) {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 setFrameShape(QFrame::NoFrame);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 setSelectionMode(QAbstractItemView::SingleSelection);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 setSelectionBehavior(QAbstractItemView::SelectItems);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 setMouseTracking(true);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 /* FIXME: is there an easy way to do this with palettes? */
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 setStyleSheet("QListWidget::item:disabled { background: transparent }");
69
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
16
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
17 SetBackgroundColor(Qt::transparent);
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 connect(this, &QListWidget::currentRowChanged, this,
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); });
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22
58
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
23 void SideBar::SetCurrentItem(int index) {
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
24 setCurrentRow(AddSeparatorsToIndex(index));
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
25 }
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
26
112
80f49f623d30 locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
27 int SideBar::GetCurrentItem() {
80f49f623d30 locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
28 return RemoveSeparatorsFromIndex(currentRow());
80f49f623d30 locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
29 }
80f49f623d30 locale: allow switching locales without restarting
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
30
69
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
31 void SideBar::SetBackgroundColor(QColor color) {
71
5f9bdcea3d01 sidebar: fix regression caused by SetBackgroundColor()
Paper <mrpapersonic@gmail.com>
parents: 69
diff changeset
32 viewport()->setAutoFillBackground(color != Qt::transparent);
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
33
69
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
34 QPalette pal(palette());
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
35 pal.setColor(QPalette::Window, color);
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
36 setPalette(pal);
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
37 }
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
38
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 QListWidgetItem* item = new QListWidgetItem(this);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 item->setText(name);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 if (!icon.isNull())
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 item->setIcon(icon);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 return item;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 QIcon SideBar::CreateIcon(const char* file) {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 QPixmap pixmap(file, "PNG");
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 QIcon result;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 result.addPixmap(pixmap, QIcon::Normal);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 result.addPixmap(pixmap, QIcon::Selected);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 return result;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 QListWidgetItem* SideBar::AddSeparator() {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 QListWidgetItem* item = new QListWidgetItem(this);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 QFrame* line = new QFrame(this);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 line->setFrameShape(QFrame::HLine);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 line->setFrameShadow(QFrame::Sunken);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 line->setMouseTracking(true);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 line->setEnabled(false);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 setItemWidget(item, line);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 item->setFlags(Qt::NoItemFlags);
68
2417121d894e *: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents: 63
diff changeset
65 item->setBackground(QBrush(Qt::transparent));
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 return item;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68
58
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
69 int SideBar::AddSeparatorsToIndex(int index) {
252
a0eeb2cc7e6d *: resolve make distcheck failures
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
70 int separators = 0, items = 0;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
71
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
72 for (; items <= index; ) {
252
a0eeb2cc7e6d *: resolve make distcheck failures
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
73 if (IndexIsSeparator(indexFromItem(item(items + separators)))) {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
74 separators++;
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
75 } else {
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
76 items++;
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
77 }
58
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
78 }
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
79
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
80 return index + separators;
58
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
81 }
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
82
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 int SideBar::RemoveSeparatorsFromIndex(int index) {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
84 int i = 0, items = 0;
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
85 for (; i < index; i++) {
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 if (!IndexIsSeparator(indexFromItem(item(i))))
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
87 items++;
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 }
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
89 return items;
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 bool SideBar::IndexIsSeparator(QModelIndex index) const {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
93 return !index.isValid() || !(index.flags() & Qt::ItemIsEnabled);
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97 if (IndexIsSeparator(index))
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
98 return QItemSelectionModel::NoUpdate;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 return QItemSelectionModel::ClearAndSelect;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 void SideBar::mouseMoveEvent(QMouseEvent* event) {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 if (!IndexIsSeparator(indexAt(event->pos())))
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104 setCursor(Qt::PointingHandCursor);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105 else
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 unsetCursor();
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107 QListView::mouseMoveEvent(event);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108 }