Mercurial > minori
annotate src/gui/widgets/sidebar.cpp @ 74:5ccb99bfa605
fix regressions on macOS
for now, we're setting our font sizes using setPixelSize, and later I'll
use a macro or something to make the point size constant across platforms
also anilist user id stuff stopped working :) that's fixed now
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 03 Oct 2023 06:12:43 -0400 |
parents | 5f9bdcea3d01 |
children | d3e9310598b1 |
rev | line source |
---|---|
46 | 1 #include "gui/widgets/sidebar.h" |
63 | 2 #include <QDebug> |
46 | 3 #include <QFrame> |
4 #include <QListWidget> | |
5 #include <QListWidgetItem> | |
6 #include <QMessageBox> | |
7 #include <QMouseEvent> | |
8 | |
9 SideBar::SideBar(QWidget* parent) : QListWidget(parent) { | |
10 setFrameShape(QFrame::NoFrame); | |
11 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
12 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
13 setSelectionMode(QAbstractItemView::SingleSelection); | |
14 setSelectionBehavior(QAbstractItemView::SelectItems); | |
15 setMouseTracking(true); | |
16 /* FIXME: is there an easy way to do this with palettes? */ | |
17 setStyleSheet("QListWidget::item:disabled { background: transparent }"); | |
69 | 18 |
19 SetBackgroundColor(Qt::transparent); | |
46 | 20 |
21 QFont font; | |
74 | 22 font.setPixelSize(12); |
46 | 23 setFont(font); |
24 | |
25 connect(this, &QListWidget::currentRowChanged, this, | |
26 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); | |
27 } | |
28 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
29 void SideBar::SetCurrentItem(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
30 setCurrentRow(AddSeparatorsToIndex(index)); |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
31 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
32 |
69 | 33 void SideBar::SetBackgroundColor(QColor color) { |
71
5f9bdcea3d01
sidebar: fix regression caused by SetBackgroundColor()
Paper <mrpapersonic@gmail.com>
parents:
69
diff
changeset
|
34 viewport()->setAutoFillBackground(color != Qt::transparent); |
69 | 35 QPalette pal(palette()); |
36 pal.setColor(QPalette::Window, color); | |
37 setPalette(pal); | |
38 } | |
39 | |
46 | 40 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
41 QListWidgetItem* item = new QListWidgetItem(this); | |
42 item->setText(name); | |
43 if (!icon.isNull()) | |
44 item->setIcon(icon); | |
45 return item; | |
46 } | |
47 | |
48 QIcon SideBar::CreateIcon(const char* file) { | |
49 QPixmap pixmap(file, "PNG"); | |
50 QIcon result; | |
51 result.addPixmap(pixmap, QIcon::Normal); | |
52 result.addPixmap(pixmap, QIcon::Selected); | |
53 return result; | |
54 } | |
55 | |
56 QListWidgetItem* SideBar::AddSeparator() { | |
57 QListWidgetItem* item = new QListWidgetItem(this); | |
58 QFrame* line = new QFrame(this); | |
59 line->setFrameShape(QFrame::HLine); | |
60 line->setFrameShadow(QFrame::Sunken); | |
61 line->setMouseTracking(true); | |
62 line->setEnabled(false); | |
63 | |
64 setItemWidget(item, line); | |
65 item->setFlags(Qt::NoItemFlags); | |
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
63
diff
changeset
|
66 item->setBackground(QBrush(Qt::transparent)); |
46 | 67 return item; |
68 } | |
69 | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
70 int SideBar::AddSeparatorsToIndex(int index) { |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
71 int i, j; |
63 | 72 for (i = 0, j = 0; i < index;) { |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
73 i++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
74 if (IndexIsSeparator(indexFromItem(item(i)))) |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
75 j++; |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
76 } |
63 | 77 return i + j; |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
78 } |
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
79 |
46 | 80 int SideBar::RemoveSeparatorsFromIndex(int index) { |
81 int i, j; | |
82 for (i = 0, j = 0; i < index; i++) { | |
83 if (!IndexIsSeparator(indexFromItem(item(i)))) | |
84 j++; | |
85 } | |
86 return j; | |
87 } | |
88 | |
89 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
90 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled); | |
91 } | |
92 | |
93 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
94 if (IndexIsSeparator(index)) | |
95 return QItemSelectionModel::NoUpdate; | |
96 return QItemSelectionModel::ClearAndSelect; | |
97 } | |
98 | |
99 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
100 if (!IndexIsSeparator(indexAt(event->pos()))) | |
101 setCursor(Qt::PointingHandCursor); | |
102 else | |
103 unsetCursor(); | |
104 QListView::mouseMoveEvent(event); | |
105 } | |
106 | |
107 #include "gui/widgets/moc_sidebar.cpp" |