QtCanpool 3.3.0
载入中...
搜索中...
未找到
qxtabbar.cpp
浏览该文件的文档.
1
5#include "qxtabbar.h"
6#include "qxtabbar_p.h"
7
8#include <QAction>
9#include <QWidgetAction>
10#include <QActionGroup>
11#include <QActionEvent>
12#include <QLayout>
13#include <QToolButton>
14#include <QDebug>
15
17
18QxTabBarPrivate::QxTabBarPrivate()
19 : m_currentIndex(-1)
20 , m_togglable(true)
21{
22#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
23 m_togglable = false;
24#endif
25}
26
30
32{
33 Q_Q(QxTabBar);
34 q->layout()->setSpacing(0);
35 q->layout()->setContentsMargins(0, 0, 0, 0);
36
37 m_group = new QActionGroup(q);
38#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
39 m_group->setExclusive(true);
40#else
41 m_group->setExclusionPolicy(QActionGroup::ExclusionPolicy::ExclusiveOptional);
42#endif
43 connect(m_group, &QActionGroup::triggered, this, &QxTabBarPrivate::onTriggered);
44
45 connect(q, &QxTabBar::orientationChanged, this, &QxTabBarPrivate::onOrientationChanged);
46}
47
48int QxTabBarPrivate::indexOf(QAction *action)
49{
50 for (int i = 0; i < m_tabs.count(); ++i) {
51 if (m_tabs.at(i) == action) {
52 return i;
53 }
54 }
55 return -1;
56}
57
59{
60 Q_Q(QxTabBar);
61 if (q->orientation() == Qt::Vertical) {
62 int w = 0;
63 foreach (QAction *act, m_tabs) {
64 int aw = q->widgetForAction(act)->sizeHint().width();
65 if (w < aw)
66 w = aw;
67 }
68 foreach (QAction *act, m_tabs) {
69 q->widgetForAction(act)->setFixedWidth(w);
70 }
71 } else {
72 foreach (QAction *act, m_tabs) {
73 QWidget *widget = q->widgetForAction(act);
74 widget->setFixedWidth(widget->sizeHint().width());
75 }
76 }
77}
78
79void QxTabBarPrivate::onTriggered(QAction *action)
80{
81 Q_Q(QxTabBar);
82 int index = indexOf(action);
83 if (index != m_currentIndex) {
84 m_currentIndex = index;
85 Q_EMIT q->currentChanged(index);
86 }
87 if (m_togglable) {
88 Q_EMIT q->currentToggled(index, action->isChecked());
89 }
90}
91
92void QxTabBarPrivate::onOrientationChanged(Qt::Orientation orientation)
93{
94 Q_UNUSED(orientation);
96}
97
98QxTabBar::QxTabBar(QWidget *parent)
99 : QToolBar{parent}
100{
102 Q_D(QxTabBar);
103 d->init();
104}
105
107 : QToolBar{parent}
108{
110 d->init();
111}
112
117
118int QxTabBar::addTab(const QString &text)
119{
120 return addTab(QIcon(), text);
121}
122
123int QxTabBar::addTab(const QIcon &icon, const QString &text)
124{
125 return insertTab(-1, icon, text);
126}
127
128int QxTabBar::insertTab(int index, const QString &text)
129{
130 return insertTab(index, QIcon(), text);
131}
132
133int QxTabBar::insertTab(int index, const QIcon &icon, const QString &text)
134{
135 Q_D(QxTabBar);
136 QAction *action = new QAction(this);
137 action->setIcon(icon);
138 action->setText(text);
139 action->setCheckable(true);
140
141 if (!d->validIndex(index)) {
142 index = d->m_tabs.count();
143 // add to list first, so that inheritance class knows whether to append or insert
144 d->m_tabs.append(action);
145 addAction(action);
146 } else {
147 QAction *beforeAction = d->m_tabs.at(index);
148 d->m_tabs.insert(index, action);
149 insertAction(beforeAction, action);
150 }
151 d->m_group->addAction(action);
152
153 if (d->m_tabs.count() == 1) {
154 setCurrentIndex(index);
155 } else if (index <= d->m_currentIndex) {
156 ++d->m_currentIndex;
157 }
158
159 d->layoutActions();
160
161 return index;
162}
163
164void QxTabBar::removeTab(int index)
165{
166 Q_D(QxTabBar);
167 if (!d->validIndex(index)) {
168 return;
169 }
170 QAction *action = d->m_tabs.takeAt(index);
171 removeAction(action);
172 d->m_group->removeAction(action);
173 delete action;
174
175 int newIndex = -1;
176 if (index == d->m_currentIndex) {
177 d->m_currentIndex = -1;
178 int newCnt = d->m_tabs.count();
179 if (newCnt > 0) {
180 newIndex = index;
181 if (newIndex >= newCnt) {
182 newIndex = newCnt - 1;
183 }
184 setCurrentIndex(newIndex);
185 }
186 } else if (index < d->m_currentIndex) {
187 newIndex = d->m_currentIndex - 1;
188 d->m_currentIndex = -1;
189 setCurrentIndex(newIndex);
190 }
191 // empty or setCurrentIndex failed
192 if (d->m_currentIndex == -1) {
193 Q_EMIT currentChanged(-1);
194 }
195}
196
197void QxTabBar::setTabEnabled(int index, bool enable)
198{
199 Q_D(QxTabBar);
200 if (d->validIndex(index)) {
201 d->m_tabs[index]->setEnabled(enable);
202 }
203}
204
205bool QxTabBar::isTabEnabled(int index) const
206{
207 Q_D(const QxTabBar);
208 if (d->validIndex(index)) {
209 return d->m_tabs.at(index)->isEnabled();
210 }
211 return false;
212}
213
214void QxTabBar::setTabVisible(int index, bool visible)
215{
216 Q_D(QxTabBar);
217 if (d->validIndex(index)) {
218 d->m_tabs.at(index)->setVisible(visible);
219 }
220}
221
222bool QxTabBar::isTabVisible(int index) const
223{
224 Q_D(const QxTabBar);
225 if (d->validIndex(index)) {
226 return d->m_tabs.at(index)->isVisible();
227 }
228 return false;
229}
230
231void QxTabBar::setTabToolTip(int index, const QString &toolTip)
232{
233 Q_D(QxTabBar);
234 if (d->validIndex(index)) {
235 d->m_tabs[index]->setToolTip(toolTip);
236 }
237}
238
239QString QxTabBar::tabToolTip(int index) const
240{
241 Q_D(const QxTabBar);
242 if (d->validIndex(index)) {
243 return d->m_tabs.at(index)->toolTip();
244 }
245 return QString();
246}
247
248bool QxTabBar::isTabChecked(int index) const
249{
250 Q_D(const QxTabBar);
251 if (d->validIndex(index)) {
252 return d->m_tabs.at(index)->isChecked();
253 }
254 return false;
255}
256
258{
259 Q_D(QxTabBar);
260 if (d->m_currentIndex != index && isTabEnabled(index)) {
261 // update m_currentIndex in onTriggered
262 d->m_tabs.at(index)->trigger();
263 }
264}
265
267{
268 Q_D(const QxTabBar);
269 return d->m_currentIndex;
270}
271
273{
274#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
275 Q_UNUSED(able);
276#else
277 Q_D(QxTabBar);
278 if (d->m_togglable == able) {
279 return;
280 }
281 d->m_togglable = able;
282 d->m_group->setExclusionPolicy(able ? QActionGroup::ExclusionPolicy::ExclusiveOptional
283 : QActionGroup::ExclusionPolicy::Exclusive);
284#endif
285}
286
288{
289 Q_D(const QxTabBar);
290 return d->m_togglable;
291}
292
293void QxTabBar::setTabStyle(Qt::ToolButtonStyle style)
294{
295 Q_D(QxTabBar);
296 setToolButtonStyle(style);
297 // If layoutActions are called in the slot function of toolButtonStyleChanged,
298 // the size of Actions cannot be updated immediately (the size after the last
299 // format change is retrieved at the next format change)
300 d->layoutActions();
301}
302
304{
305 Q_D(const QxTabBar);
306 return d->m_tabs.count();
307}
308
309void QxTabBar::actionEvent(QActionEvent *event)
310{
311 QToolBar::actionEvent(event);
312}
313
virtual ~QxTabBarPrivate()
Definition qxtabbar.cpp:27
QList< QAction * > m_tabs
Definition qxtabbar_p.h:42
QActionGroup * m_group
Definition qxtabbar_p.h:43
void onOrientationChanged(Qt::Orientation orientation)
Definition qxtabbar.cpp:92
int indexOf(QAction *action)
Definition qxtabbar.cpp:48
void onTriggered(QAction *action)
Definition qxtabbar.cpp:79
int currentIndex() const
Definition qxtabbar.cpp:266
bool isTogglable() const
Definition qxtabbar.cpp:287
int addTab(const QString &text)
Definition qxtabbar.cpp:118
virtual ~QxTabBar()
Definition qxtabbar.cpp:113
virtual void actionEvent(QActionEvent *event)
Definition qxtabbar.cpp:309
int insertTab(int index, const QString &text)
Definition qxtabbar.cpp:128
void setTabEnabled(int index, bool enable)
Definition qxtabbar.cpp:197
void setTabToolTip(int index, const QString &toolTip)
Definition qxtabbar.cpp:231
bool isTabEnabled(int index) const
Definition qxtabbar.cpp:205
void setTogglable(bool able)
Definition qxtabbar.cpp:272
void setTabStyle(Qt::ToolButtonStyle style)
Definition qxtabbar.cpp:293
bool isTabChecked(int index) const
Definition qxtabbar.cpp:248
QxTabBar(QWidget *parent=0)
Definition qxtabbar.cpp:98
void setTabVisible(int index, bool visible)
Definition qxtabbar.cpp:214
void setCurrentIndex(int index)
Definition qxtabbar.cpp:257
void currentChanged(int index)
QString tabToolTip(int index) const
Definition qxtabbar.cpp:239
bool isTabVisible(int index) const
Definition qxtabbar.cpp:222
int count() const
Definition qxtabbar.cpp:303
void removeTab(int index)
Definition qxtabbar.cpp:164
#define QX_APP_END_NAMESPACE
#define QX_INIT_PRIVATE(Class)
#define QX_APP_BEGIN_NAMESPACE
#define QX_SET_PRIVATE(Dptr)
#define QX_FINI_PRIVATE()
QColor text