Added autoHide property to QTabBar

This property is used to automatically hide tab bar if it has only one
tab.

Originally-by: Denis Kovalskiy <denimnumber1@gmail.com>
Change-Id: I6967f760010fa55bad6a5986c29abe7ccf625cf8
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Ivan Komissarov 2014-05-06 16:36:29 +04:00
parent 5835407df1
commit 84c5e4866f
6 changed files with 102 additions and 1 deletions

View File

@ -632,6 +632,14 @@ void QTabBarPrivate::layoutWidgets(int start)
}
}
void QTabBarPrivate::autoHideTabs()
{
Q_Q(QTabBar);
if (autoHide)
q->setVisible(q->count() > 1);
}
void QTabBarPrivate::_q_closeTab()
{
Q_Q(QTabBar);
@ -861,6 +869,7 @@ int QTabBar::insertTab(int index, const QIcon& icon, const QString &text)
}
tabInserted(index);
d->autoHideTabs();
return index;
}
@ -936,6 +945,7 @@ void QTabBar::removeTab(int index)
setCurrentIndex(d->currentIndex - 1);
}
d->refresh();
d->autoHideTabs();
tabRemoved(index);
}
}
@ -2266,6 +2276,36 @@ void QTabBar::setDocumentMode(bool enabled)
d->updateMacBorderMetrics();
}
/*!
\property QTabBar::autoHide
\brief If true, the tab bar is automatically hidden when it contains less
than 2 tabs.
\since 5.4
By default, this property is false.
\sa QWidget::visible
*/
bool QTabBar::autoHide() const
{
Q_D(const QTabBar);
return d->autoHide;
}
void QTabBar::setAutoHide(bool hide)
{
Q_D(QTabBar);
if (d->autoHide == hide)
return;
d->autoHide = hide;
if (hide)
d->autoHideTabs();
else
setVisible(true);
}
/*!
Sets \a widget on the tab \a index. The widget is placed
on the left or right hand side depending upon the \a position.

View File

@ -70,6 +70,7 @@ class Q_WIDGETS_EXPORT QTabBar: public QWidget
Q_PROPERTY(bool expanding READ expanding WRITE setExpanding)
Q_PROPERTY(bool movable READ isMovable WRITE setMovable)
Q_PROPERTY(bool documentMode READ documentMode WRITE setDocumentMode)
Q_PROPERTY(bool autoHide READ autoHide WRITE setAutoHide)
public:
explicit QTabBar(QWidget* parent=0);
@ -166,6 +167,9 @@ public:
bool documentMode() const;
void setDocumentMode(bool set);
bool autoHide() const;
void setAutoHide(bool hide);
public Q_SLOTS:
void setCurrentIndex(int index);

View File

@ -77,7 +77,7 @@ public:
:currentIndex(-1), pressedIndex(-1), shape(QTabBar::RoundedNorth), layoutDirty(false),
drawBase(true), scrollOffset(0), elideModeSetByUser(false), useScrollButtonsSetByUser(false), expanding(true), closeButtonOnTabs(false),
selectionBehaviorOnRemove(QTabBar::SelectRightTab), paintWithOffsets(true), movable(false),
dragInProgress(false), documentMode(false), movingTab(0)
dragInProgress(false), documentMode(false), autoHide(false), movingTab(0)
#ifdef Q_WS_MAC
, previousPressedIndex(-1)
#endif
@ -184,6 +184,7 @@ public:
void updateMacBorderMetrics();
bool isTabInMacUnifiedToolbarArea() const;
void setupMovableTab();
void autoHideTabs();
void makeVisible(int index);
QSize iconSize;
@ -201,6 +202,7 @@ public:
bool movable;
bool dragInProgress;
bool documentMode;
bool autoHide;
QWidget *movingTab;
#ifdef Q_WS_MAC

View File

@ -1350,6 +1350,29 @@ void QTabWidget::setDocumentMode(bool enabled)
setUpLayout();
}
/*!
\property QTabWidget::tabBarAutoHide
\brief If true, the tab bar is automatically hidden when it contains less
than 2 tabs.
\since 5.4
By default, this property is false.
\sa QWidget::visible
*/
bool QTabWidget::tabBarAutoHide() const
{
Q_D(const QTabWidget);
return d->tabs->autoHide();
}
void QTabWidget::setTabBarAutoHide(bool enabled)
{
Q_D(QTabWidget);
return d->tabs->setAutoHide(enabled);
}
/*!
Removes all the pages, but does not delete them. Calling this function
is equivalent to calling removeTab() until the tab widget is empty.

View File

@ -68,6 +68,7 @@ class Q_WIDGETS_EXPORT QTabWidget : public QWidget
Q_PROPERTY(bool documentMode READ documentMode WRITE setDocumentMode)
Q_PROPERTY(bool tabsClosable READ tabsClosable WRITE setTabsClosable)
Q_PROPERTY(bool movable READ isMovable WRITE setMovable)
Q_PROPERTY(bool tabBarAutoHide READ tabBarAutoHide WRITE setTabBarAutoHide)
public:
explicit QTabWidget(QWidget *parent = 0);
@ -140,6 +141,9 @@ public:
bool documentMode() const;
void setDocumentMode(bool set);
bool tabBarAutoHide() const;
void setTabBarAutoHide(bool enabled);
void clear();
QTabBar* tabBar() const;

View File

@ -99,6 +99,7 @@ private slots:
void taskQTBUG_10052_widgetLayoutWhenMoving();
void tabBarClicked();
void autoHide();
};
// Testing get/set functions
@ -701,5 +702,32 @@ void tst_QTabBar::tabBarClicked()
}
}
void tst_QTabBar::autoHide()
{
QTabBar tabBar;
QVERIFY(!tabBar.autoHide());
QVERIFY(!tabBar.isVisible());
tabBar.show();
QVERIFY(tabBar.isVisible());
tabBar.addTab("0");
QVERIFY(tabBar.isVisible());
tabBar.removeTab(0);
QVERIFY(tabBar.isVisible());
tabBar.setAutoHide(true);
QVERIFY(!tabBar.isVisible());
tabBar.addTab("0");
QVERIFY(!tabBar.isVisible());
tabBar.addTab("1");
QVERIFY(tabBar.isVisible());
tabBar.removeTab(0);
QVERIFY(!tabBar.isVisible());
tabBar.removeTab(0);
QVERIFY(!tabBar.isVisible());
tabBar.setAutoHide(false);
QVERIFY(tabBar.isVisible());
}
QTEST_MAIN(tst_QTabBar)
#include "tst_qtabbar.moc"