Mercurial > minori
annotate src/gui/widgets/sidebar.cc @ 367:8d45d892be88 default tip
*: instead of pugixml, use Qt XML features
this means we have one extra Qt dependency though...
author | Paper <paper@tflc.us> |
---|---|
date | Sun, 17 Nov 2024 22:55:47 -0500 |
parents | 91ac90a34003 |
children |
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); | |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
294
diff
changeset
|
14 |
46 | 15 setStyleSheet("QListWidget::item:disabled { background: transparent }"); |
69 | 16 |
294 | 17 SetBackgroundTransparent(true); |
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 |
294 | 31 void SideBar::SetBackgroundTransparent(bool yes) { |
32 viewport()->setAutoFillBackground(!yes); | |
69 | 33 } |
34 | |
46 | 35 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) { |
36 QListWidgetItem* item = new QListWidgetItem(this); | |
37 item->setText(name); | |
38 if (!icon.isNull()) | |
39 item->setIcon(icon); | |
40 return item; | |
41 } | |
42 | |
43 QIcon SideBar::CreateIcon(const char* file) { | |
44 QPixmap pixmap(file, "PNG"); | |
45 QIcon result; | |
46 result.addPixmap(pixmap, QIcon::Normal); | |
47 result.addPixmap(pixmap, QIcon::Selected); | |
48 return result; | |
49 } | |
50 | |
51 QListWidgetItem* SideBar::AddSeparator() { | |
52 QListWidgetItem* item = new QListWidgetItem(this); | |
53 QFrame* line = new QFrame(this); | |
54 line->setFrameShape(QFrame::HLine); | |
55 line->setFrameShadow(QFrame::Sunken); | |
56 line->setMouseTracking(true); | |
57 line->setEnabled(false); | |
58 | |
59 setItemWidget(item, line); | |
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 | 62 return item; |
63 } | |
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 | 67 |
258 | 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 | 70 separators++; |
71 } else { | |
72 items++; | |
73 } | |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
74 } |
250 | 75 |
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 | 79 int SideBar::RemoveSeparatorsFromIndex(int index) { |
250 | 80 int i = 0, items = 0; |
81 for (; i < index; i++) { | |
46 | 82 if (!IndexIsSeparator(indexFromItem(item(i)))) |
250 | 83 items++; |
46 | 84 } |
250 | 85 return items; |
46 | 86 } |
87 | |
88 bool SideBar::IndexIsSeparator(QModelIndex index) const { | |
250 | 89 return !index.isValid() || !(index.flags() & Qt::ItemIsEnabled); |
46 | 90 } |
91 | |
92 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const { | |
93 if (IndexIsSeparator(index)) | |
94 return QItemSelectionModel::NoUpdate; | |
95 return QItemSelectionModel::ClearAndSelect; | |
96 } | |
97 | |
98 void SideBar::mouseMoveEvent(QMouseEvent* event) { | |
99 if (!IndexIsSeparator(indexAt(event->pos()))) | |
100 setCursor(Qt::PointingHandCursor); | |
101 else | |
102 unsetCursor(); | |
103 QListView::mouseMoveEvent(event); | |
104 } |