QtCanpool 3.3.0
载入中...
搜索中...
未找到
qxnavigationbar.cpp
浏览该文件的文档.
1
5#include "qxnavigationbar.h"
6
7#include <QtCore/QList>
8#include <QtWidgets/QButtonGroup>
9#include <QtWidgets/QToolButton>
10#include <QtWidgets/QVBoxLayout>
11
13
15static const int s_defaultIconSize = 24;
16
17class QxNavigationBarPrivate
18{
19 QX_DECLARE_PUBLIC(QxNavigationBar)
20public:
21 QxNavigationBarPrivate();
22 void init();
24 void updateButtonIds();
26 void syncCurrentIndex();
27 QToolButton *buttonAt(int index) const;
28public:
29 QVBoxLayout *m_layout;
30 QButtonGroup *m_group;
31 QList<QToolButton *> m_buttons;
32 QSize m_iconSize;
33 int m_currentIndex;
34};
35
36QxNavigationBarPrivate::QxNavigationBarPrivate()
37 : m_layout(Q_NULLPTR)
38 , m_group(Q_NULLPTR)
39 , m_iconSize(s_defaultIconSize, s_defaultIconSize)
40 , m_currentIndex(-1)
41{
42}
43
44void QxNavigationBarPrivate::init()
45{
46 Q_Q(QxNavigationBar);
47
48 m_layout = new QVBoxLayout(q);
49 m_layout->setContentsMargins(2, 2, 2, 2);
50 m_layout->setSpacing(2);
51 // Keeps the entries packed at the top when the rail is taller than the list.
52 m_layout->addStretch(1);
53
54 m_group = new QButtonGroup(q);
55 m_group->setExclusive(true);
56 QObject::connect(m_group, &QButtonGroup::idClicked, q, &QxNavigationBar::setCurrentIndex);
57
58 q->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
59}
60
61QToolButton *QxNavigationBarPrivate::buttonAt(int index) const
62{
63 if (index < 0 || index >= m_buttons.count()) {
64 return Q_NULLPTR;
65 }
66 return m_buttons.at(index);
67}
68
69void QxNavigationBarPrivate::updateButtonIds()
70{
71 for (int i = 0; i < m_buttons.count(); ++i) {
72 m_group->setId(m_buttons.at(i), i);
73 }
74}
75
76void QxNavigationBarPrivate::syncCurrentIndex()
77{
78 Q_Q(QxNavigationBar);
79
80 int index = -1;
81 for (int i = 0; i < m_buttons.count(); ++i) {
82 if (m_buttons.at(i)->isChecked()) {
83 index = i;
84 break;
85 }
86 }
87 if (index == m_currentIndex) {
88 return;
89 }
90 m_currentIndex = index;
91 Q_EMIT q->currentChanged(index);
92}
93
94QxNavigationBar::QxNavigationBar(QWidget *parent)
95 : QWidget(parent)
96 , d_ptr(new QxNavigationBarPrivate())
97{
98 d_ptr->setPublic(this);
99 Q_D(QxNavigationBar);
100 d->init();
101}
102
107
108int QxNavigationBar::addItem(const QIcon &icon, const QString &text)
109{
110 Q_D(QxNavigationBar);
111 const int index = d->m_buttons.count();
112 insertItem(index, icon, text);
113 return index;
114}
115
116void QxNavigationBar::insertItem(int index, const QIcon &icon, const QString &text)
117{
118 Q_D(QxNavigationBar);
119 if (index < 0 || index > d->m_buttons.count()) {
120 return;
121 }
122
123 QToolButton *button = new QToolButton(this);
124 button->setCheckable(true);
125 button->setAutoRaise(true);
126 button->setFocusPolicy(Qt::NoFocus);
127 button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
128 button->setIconSize(d->m_iconSize);
129 button->setIcon(icon);
130 button->setText(text);
131
132 d->m_layout->insertWidget(index, button);
133 d->m_buttons.insert(index, button);
134 d->m_group->addButton(button);
135 d->updateButtonIds();
136
137 // An entry inserted in front of the selected one shifts its index; the
138 // selection itself stays on the same button.
139 d->syncCurrentIndex();
140}
141
143{
144 Q_D(QxNavigationBar);
145 QToolButton *button = d->buttonAt(index);
146 if (!button) {
147 return;
148 }
149
150 d->m_buttons.removeAt(index);
151 d->m_group->removeButton(button);
152 d->m_layout->removeWidget(button);
153 button->hide();
154 button->deleteLater();
155 d->updateButtonIds();
156
157 d->syncCurrentIndex();
158}
159
161{
162 Q_D(QxNavigationBar);
163 while (!d->m_buttons.isEmpty()) {
164 removeItem(d->m_buttons.count() - 1);
165 }
166}
167
169{
170 Q_D(const QxNavigationBar);
171 return d->m_buttons.count();
172}
173
175{
176 Q_D(const QxNavigationBar);
177 return d->m_currentIndex;
178}
179
180QString QxNavigationBar::itemText(int index) const
181{
182 Q_D(const QxNavigationBar);
183 QToolButton *button = d->buttonAt(index);
184 return button ? button->text() : QString();
185}
186
187QIcon QxNavigationBar::itemIcon(int index) const
188{
189 Q_D(const QxNavigationBar);
190 QToolButton *button = d->buttonAt(index);
191 return button ? button->icon() : QIcon();
192}
193
195{
196 Q_D(const QxNavigationBar);
197 QToolButton *button = d->buttonAt(index);
198 return button ? button->isEnabled() : false;
199}
200
202{
203 Q_D(const QxNavigationBar);
204 return d->m_iconSize;
205}
206
207QToolButton *QxNavigationBar::button(int index) const
208{
209 Q_D(const QxNavigationBar);
210 return d->buttonAt(index);
211}
212
214{
215 Q_D(QxNavigationBar);
216 if (index < -1 || index >= d->m_buttons.count()) {
217 return;
218 }
219 if (index == d->m_currentIndex) {
220 return;
221 }
222
223 d->m_currentIndex = index;
224 if (index < 0) {
225 // An exclusive group refuses to uncheck its member, so exclusivity is
226 // lifted for the moment it takes to clear the selection.
227 const bool exclusive = d->m_group->exclusive();
228 d->m_group->setExclusive(false);
229 for (QToolButton *button : d->m_buttons) {
230 button->setChecked(false);
231 }
232 d->m_group->setExclusive(exclusive);
233 } else {
234 d->m_buttons.at(index)->setChecked(true);
235 }
236
237 Q_EMIT currentChanged(index);
238}
239
240void QxNavigationBar::setItemText(int index, const QString &text)
241{
242 Q_D(QxNavigationBar);
243 QToolButton *button = d->buttonAt(index);
244 if (button) {
245 button->setText(text);
246 }
247}
248
249void QxNavigationBar::setItemIcon(int index, const QIcon &icon)
250{
251 Q_D(QxNavigationBar);
252 QToolButton *button = d->buttonAt(index);
253 if (button) {
254 button->setIcon(icon);
255 }
256}
257
259{
260 Q_D(QxNavigationBar);
261 QToolButton *button = d->buttonAt(index);
262 if (button) {
263 button->setEnabled(enabled);
264 }
265}
266
267void QxNavigationBar::setItemToolTip(int index, const QString &toolTip)
268{
269 Q_D(QxNavigationBar);
270 QToolButton *button = d->buttonAt(index);
271 if (button) {
272 button->setToolTip(toolTip);
273 }
274}
275
276void QxNavigationBar::setIconSize(const QSize &size)
277{
278 Q_D(QxNavigationBar);
279 d->m_iconSize = size;
280 for (QToolButton *button : d->m_buttons) {
281 button->setIconSize(size);
282 }
283}
284
A vertical rail of exclusive, checkable entries.
int count() const
Number of entries.
int addItem(const QIcon &icon, const QString &text)
Appends an entry and returns its index.
void insertItem(int index, const QIcon &icon, const QString &text)
Inserts an entry at the given index, which may also be count().
bool isItemEnabled(int index) const
int currentIndex() const
Index of the selected entry, or -1 when nothing is selected.
QIcon itemIcon(int index) const
void setIconSize(const QSize &size)
QSize iconSize() const
Icon size shared by every entry; 24x24 by default.
void setItemEnabled(int index, bool enabled)
QToolButton * button(int index) const
The button behind the given index, for the rare case that needs to tweak something the bar does not e...
QString itemText(int index) const
void removeItem(int index)
Removes the entry at the given index; no-op when it is out of range.
void currentChanged(int index)
Emitted when the selection moves: on a click, on an explicit setCurrentIndex(), and also when an entr...
void setCurrentIndex(int index)
Selects the entry at the given index; pass -1 to clear the selection.
void setItemText(int index, const QString &text)
void clear()
Removes every entry.
void setItemToolTip(int index, const QString &toolTip)
void setItemIcon(int index, const QIcon &icon)
#define QX_APP_END_NAMESPACE
#define QX_DECLARE_PUBLIC(Class)
#define Q_NULLPTR
Copyleft (C) 2025 maminjie canpool@163.com SPDX-License-Identifier: MulanPSL-2.0
#define QX_APP_BEGIN_NAMESPACE
#define QX_FINI_PRIVATE()
bool enabled
Definition qxlogger.cpp:27
QColor text
QColor button