Mercurial > minori
annotate src/gui/widgets/sidebar.cc @ 260:dd211ff68b36
pages/seasons: add initial functionality
the menu doesn't work yet, but it's a good start
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 03 Apr 2024 19:48:38 -0400 |
parents | 862d0d8619f6 |
children | 9a88e1725fd2 |
rev | line source |
---|---|
46 | 1 #include "gui/widgets/sidebar.h" |
2 #include <QFrame> | |
3 #include <QListWidget> | |
4 #include <QListWidgetItem> | |
5 #include <QMouseEvent> | |
6 | |
7 SideBar::SideBar(QWidget* parent) : QListWidget(parent) { | |
8 setFrameShape(QFrame::NoFrame); | |
9 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
10 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
11 setSelectionMode(QAbstractItemView::SingleSelection); | |
12 setSelectionBehavior(QAbstractItemView::SelectItems); | |
13 setMouseTracking(true); | |
14 /* FIXME: is there an easy way to do this with palettes? */ | |
15 setStyleSheet("QListWidget::item:disabled { background: transparent }"); | |
69 | 16 |
17 SetBackgroundColor(Qt::transparent); | |
46 | 18 |
19 connect(this, &QListWidget::currentRowChanged, this, | |
20 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); }); | |
21 } | |
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 | 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 | 33 |
69 | 34 QPalette pal(palette()); |
35 pal.setColor(QPalette::Window, color); | |
36 setPalette(pal); | |
37 } | |
38 | |
46 | 39 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
40 QListWidgetItem* item = new QListWidgetItem(this); | |
41 item->setText(name); | |
42 if (!icon.isNull()) | |
43 item->setIcon(icon); | |
44 return item; | |
45 } | |
46 | |
47 QIcon SideBar::CreateIcon(const char* file) { | |
48 QPixmap pixmap(file, "PNG"); | |
49 QIcon result; | |
50 result.addPixmap(pixmap, QIcon::Normal); | |
51 result.addPixmap(pixmap, QIcon::Selected); | |
52 return result; | |
53 } | |
54 | |
55 QListWidgetItem* SideBar::AddSeparator() { | |
56 QListWidgetItem* item = new QListWidgetItem(this); | |
57 QFrame* line = new QFrame(this); | |
58 line->setFrameShape(QFrame::HLine); | |
59 line->setFrameShadow(QFrame::Sunken); | |
60 line->setMouseTracking(true); | |
61 line->setEnabled(false); | |
62 | |
63 setItemWidget(item, line); | |
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 | 66 return item; |
67 } | |
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 | 71 |
258 | 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 | 74 separators++; |
75 } else { | |
76 items++; | |
77 } | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
78 } |
250 | 79 |
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 | 83 int SideBar::RemoveSeparatorsFromIndex(int index) { |
250 | 84 int i = 0, items = 0; |
85 for (; i < index; i++) { | |
46 | 86 if (!IndexIsSeparator(indexFromItem(item(i)))) |
250 | 87 items++; |
46 | 88 } |
250 | 89 return items; |
46 | 90 } |
91 | |
92 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
250 | 93 return !index.isValid() || !(index.flags() & Qt::ItemIsEnabled); |
46 | 94 } |
95 | |
96 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
97 if (IndexIsSeparator(index)) | |
98 return QItemSelectionModel::NoUpdate; | |
99 return QItemSelectionModel::ClearAndSelect; | |
100 } | |
101 | |
102 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
103 if (!IndexIsSeparator(indexAt(event->pos()))) | |
104 setCursor(Qt::PointingHandCursor); | |
105 else | |
106 unsetCursor(); | |
107 QListView::mouseMoveEvent(event); | |
108 } |