annotate src/gui/widgets/sidebar.cc @ 327:b5d6c27c308f

anime: refactor Anime::SeriesSeason to Season class ToLocalString has also been altered to take in both season and year because lots of locales actually treat formatting seasons differently! most notably is Russian which adds a suffix at the end to notate seasons(??)
author Paper <paper@paper.us.eu.org>
date Thu, 13 Jun 2024 01:49:18 -0400
parents 91ac90a34003
children
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);
305
91ac90a34003 core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents: 294
diff changeset
14
46
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
294
99cbc51433e4 *: cleanup uses of QPalette
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
17 SetBackgroundTransparent(true);
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
294
99cbc51433e4 *: cleanup uses of QPalette
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
31 void SideBar::SetBackgroundTransparent(bool yes) {
99cbc51433e4 *: cleanup uses of QPalette
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
32 viewport()->setAutoFillBackground(!yes);
69
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
33 }
27a19dd6cba1 *: fix up some stuff
Paper <mrpapersonic@gmail.com>
parents: 68
diff changeset
34
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 QListWidgetItem* item = new QListWidgetItem(this);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 item->setText(name);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 if (!icon.isNull())
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 item->setIcon(icon);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 return item;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 QIcon SideBar::CreateIcon(const char* file) {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 QPixmap pixmap(file, "PNG");
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 QIcon result;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 result.addPixmap(pixmap, QIcon::Normal);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 result.addPixmap(pixmap, QIcon::Selected);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 return result;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 QListWidgetItem* SideBar::AddSeparator() {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 QListWidgetItem* item = new QListWidgetItem(this);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 QFrame* line = new QFrame(this);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 line->setFrameShape(QFrame::HLine);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 line->setFrameShadow(QFrame::Sunken);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 line->setMouseTracking(true);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 line->setEnabled(false);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 setItemWidget(item, line);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 item->setFlags(Qt::NoItemFlags);
68
2417121d894e *: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents: 63
diff changeset
61 item->setBackground(QBrush(Qt::transparent));
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 return item;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64
58
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
65 int SideBar::AddSeparatorsToIndex(int index) {
252
a0eeb2cc7e6d *: resolve make distcheck failures
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
66 int separators = 0, items = 0;
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
67
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 252
diff changeset
68 for (; items <= index;) {
252
a0eeb2cc7e6d *: resolve make distcheck failures
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
69 if (IndexIsSeparator(indexFromItem(item(items + separators)))) {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
70 separators++;
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
71 } else {
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
72 items++;
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
73 }
58
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
74 }
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
75
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
76 return index + separators;
58
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
77 }
b7a1c0010ffd sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents: 46
diff changeset
78
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 int SideBar::RemoveSeparatorsFromIndex(int index) {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
80 int i = 0, items = 0;
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
81 for (; i < index; i++) {
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 if (!IndexIsSeparator(indexFromItem(item(i))))
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
83 items++;
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 }
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
85 return items;
46
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 bool SideBar::IndexIsSeparator(QModelIndex index) const {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 236
diff changeset
89 return !index.isValid() || !(index.flags() & Qt::ItemIsEnabled);
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 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 if (IndexIsSeparator(index))
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 return QItemSelectionModel::NoUpdate;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 return QItemSelectionModel::ClearAndSelect;
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 }
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
98 void SideBar::mouseMoveEvent(QMouseEvent* event) {
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 if (!IndexIsSeparator(indexAt(event->pos())))
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 setCursor(Qt::PointingHandCursor);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101 else
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 unsetCursor();
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 QListView::mouseMoveEvent(event);
d0adc4aedfc8 *: update...
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104 }