From 18291af42caf18165f99dd0300738576b96379a6 Mon Sep 17 00:00:00 2001 From: Dongmei Wang Date: Fri, 22 Jan 2016 20:19:28 -0800 Subject: [PATCH] QTabBar: Add setAccessibleTabName() and accessibleTabName() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, a tab's text is used as its accessibleName. When a tab's text is empty, there is no API to set the tab's accessibleName. The two APIs are added to set and return the accessibleName property of a tab. Task-number: QTBUG-46530 Change-Id: Idf88b5f905fe66c6365ea0eeb650e74211db90e1 Reviewed-by: Jan Arve Sæther --- src/widgets/accessible/complexwidgets.cpp | 24 ++++++++++++---- src/widgets/widgets/qtabbar.cpp | 28 +++++++++++++++++++ src/widgets/widgets/qtabbar.h | 5 ++++ src/widgets/widgets/qtabbar_p.h | 3 ++ .../qaccessibility/tst_qaccessibility.cpp | 19 ++++++++++++- 5 files changed, 72 insertions(+), 7 deletions(-) diff --git a/src/widgets/accessible/complexwidgets.cpp b/src/widgets/accessible/complexwidgets.cpp index 8faa73b68b..463019dbd0 100644 --- a/src/widgets/accessible/complexwidgets.cpp +++ b/src/widgets/accessible/complexwidgets.cpp @@ -120,19 +120,26 @@ public: { if (!isValid()) return QString(); + QString str; switch (t) { case QAccessible::Name: - return qt_accStripAmp(m_parent->tabText(m_index)); + str = m_parent->accessibleTabName(m_index); + if (str.isEmpty()) + str = qt_accStripAmp(m_parent->tabText(m_index)); + break; case QAccessible::Accelerator: - return qt_accHotKey(m_parent->tabText(m_index)); + str = qt_accHotKey(m_parent->tabText(m_index)); + break; case QAccessible::Description: - return m_parent->tabToolTip(m_index); + str = m_parent->tabToolTip(m_index); + break; case QAccessible::Help: - return m_parent->tabWhatsThis(m_index); + str = m_parent->tabWhatsThis(m_index); + break; default: break; } - return QString(); + return str; } void setText(QAccessible::Text, const QString &) Q_DECL_OVERRIDE {} @@ -237,7 +244,12 @@ int QAccessibleTabBar::childCount() const QString QAccessibleTabBar::text(QAccessible::Text t) const { if (t == QAccessible::Name) { - return qt_accStripAmp(tabBar()->tabText(tabBar()->currentIndex())); + const QTabBar *tBar = tabBar(); + int idx = tBar->currentIndex(); + QString str = tBar->accessibleTabName(idx); + if (str.isEmpty()) + str = qt_accStripAmp(tBar->tabText(idx)); + return str; } else if (t == QAccessible::Accelerator) { return qt_accHotKey(tabBar()->tabText(tabBar()->currentIndex())); } diff --git a/src/widgets/widgets/qtabbar.cpp b/src/widgets/widgets/qtabbar.cpp index 6c748c09bb..9cc44007bd 100644 --- a/src/widgets/widgets/qtabbar.cpp +++ b/src/widgets/widgets/qtabbar.cpp @@ -2522,6 +2522,34 @@ QWidget *QTabBar::tabButton(int index, ButtonPosition position) const return d->tabList.at(index).rightWidget; } +#ifndef QT_NO_ACCESSIBILITY +/*! + Sets the accessibleName of the tab at position \a index to \a name. +*/ +void QTabBar::setAccessibleTabName(int index, const QString &name) +{ + Q_D(QTabBar); + if (QTabBarPrivate::Tab *tab = d->at(index)) { + tab->accessibleName = name; + QAccessibleEvent event(this, QAccessible::NameChanged); + event.setChild(index); + QAccessible::updateAccessibility(&event); + } +} + +/*! + Returns the accessibleName of the tab at position \a index, or an empty + string if \a index is out of range. +*/ +QString QTabBar::accessibleTabName(int index) const +{ + Q_D(const QTabBar); + if (const QTabBarPrivate::Tab *tab = d->at(index)) + return tab->accessibleName; + return QString(); +} +#endif // QT_NO_ACCESSIBILITY + CloseButton::CloseButton(QWidget *parent) : QAbstractButton(parent) { diff --git a/src/widgets/widgets/qtabbar.h b/src/widgets/widgets/qtabbar.h index 138c5470c6..84b2d40aea 100644 --- a/src/widgets/widgets/qtabbar.h +++ b/src/widgets/widgets/qtabbar.h @@ -172,6 +172,11 @@ public: bool changeCurrentOnDrag() const; void setChangeCurrentOnDrag(bool change); +#ifndef QT_NO_ACCESSIBILITY + QString accessibleTabName(int index) const; + void setAccessibleTabName(int index, const QString &name); +#endif + public Q_SLOTS: void setCurrentIndex(int index); diff --git a/src/widgets/widgets/qtabbar_p.h b/src/widgets/widgets/qtabbar_p.h index 48f235f050..7c50ebd235 100644 --- a/src/widgets/widgets/qtabbar_p.h +++ b/src/widgets/widgets/qtabbar_p.h @@ -131,6 +131,9 @@ public: QWidget *rightWidget; int lastTab; int dragOffset; +#ifndef QT_NO_ACCESSIBILITY + QString accessibleName; +#endif #ifndef QT_NO_ANIMATION ~Tab() { delete animation; } diff --git a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp index 046c77d6c3..2f5936f1ad 100644 --- a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp +++ b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp @@ -1301,6 +1301,16 @@ void tst_QAccessibility::tabTest() child2->actionInterface()->doAction(QAccessibleActionInterface::pressAction()); QCOMPARE(tabBar->currentIndex(), 1); + // Test that setAccessibleTabName changes a tab's accessible name + tabBar->setAccessibleTabName(0, "AccFoo"); + tabBar->setAccessibleTabName(1, "AccBar"); + QCOMPARE(child1->text(QAccessible::Name), QLatin1String("AccFoo")); + QCOMPARE(child2->text(QAccessible::Name), QLatin1String("AccBar")); + tabBar->setCurrentIndex(0); + QCOMPARE(interface->text(QAccessible::Name), QLatin1String("AccFoo")); + tabBar->setCurrentIndex(1); + QCOMPARE(interface->text(QAccessible::Name), QLatin1String("AccBar")); + delete tabBar; QTestAccessibility::clearEvents(); } @@ -1338,10 +1348,17 @@ void tst_QAccessibility::tabWidgetTest() QCOMPARE(tabButton1Interface->text(QAccessible::Name), QLatin1String("Tab 1")); QAccessibleInterface* tabButton2Interface = tabBarInterface->child(1); - QVERIFY(tabButton1Interface); + QVERIFY(tabButton2Interface); QCOMPARE(tabButton2Interface->role(), QAccessible::PageTab); QCOMPARE(tabButton2Interface->text(QAccessible::Name), QLatin1String("Tab 2")); + // Test that setAccessibleTabName changes a tab's accessible name + tabWidget->setCurrentIndex(0); + tabWidget->tabBar()->setAccessibleTabName(0, "Acc Tab"); + QCOMPARE(tabButton1Interface->role(), QAccessible::PageTab); + QCOMPARE(tabButton1Interface->text(QAccessible::Name), QLatin1String("Acc Tab")); + QCOMPARE(tabBarInterface->text(QAccessible::Name), QLatin1String("Acc Tab")); + QAccessibleInterface* tabButtonLeft = tabBarInterface->child(2); QVERIFY(tabButtonLeft); QCOMPARE(tabButtonLeft->role(), QAccessible::PushButton);