a11y: Make QAccessibleButton and friends resolve role dynamically

The role may changed based on the checkable state of the button, eg, so
we need to resolve the role at runtime instead of hard-coding it in the
constructor.

Change-Id: I78faee08189c5510ca9964b07ad94bcf5d4fa11b
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
This commit is contained in:
Tor Arne Vestbø 2014-09-19 15:27:16 +02:00
parent 40add779bd
commit 076d22efa6
3 changed files with 49 additions and 38 deletions

View File

@ -95,37 +95,13 @@ QAccessibleInterface *qAccessibleFactory(const QString &classname, QObject *obje
#endif
#ifndef QT_NO_TOOLBUTTON
} else if (classname == QLatin1String("QToolButton")) {
QAccessible::Role role = QAccessible::NoRole;
#ifndef QT_NO_MENU
QToolButton *tb = qobject_cast<QToolButton*>(widget);
if (!tb->menu())
role = tb->isCheckable() ? QAccessible::CheckBox : QAccessible::PushButton;
else if (tb->popupMode() == QToolButton::DelayedPopup)
role = QAccessible::ButtonDropDown;
else
#endif
role = QAccessible::ButtonMenu;
iface = new QAccessibleToolButton(widget, role);
iface = new QAccessibleToolButton(widget);
#endif // QT_NO_TOOLBUTTON
} else if (classname == QLatin1String("QCheckBox")) {
iface = new QAccessibleButton(widget, QAccessible::CheckBox);
} else if (classname == QLatin1String("QRadioButton")) {
iface = new QAccessibleButton(widget, QAccessible::RadioButton);
} else if (classname == QLatin1String("QPushButton")) {
QAccessible::Role role = QAccessible::NoRole;
QPushButton *pb = qobject_cast<QPushButton*>(widget);
#ifndef QT_NO_MENU
if (pb->menu())
role = QAccessible::ButtonMenu;
else
#endif
if (pb->isCheckable())
role = QAccessible::CheckBox;
else
role = QAccessible::PushButton;
iface = new QAccessibleButton(widget, role);
} else if (classname == QLatin1String("QAbstractButton")) {
iface = new QAccessibleButton(widget, QAccessible::PushButton);
} else if (classname == QLatin1String("QCheckBox")
|| classname == QLatin1String("QRadioButton")
|| classname == QLatin1String("QPushButton")
|| classname == QLatin1String("QAbstractButton")) {
iface = new QAccessibleButton(widget);
} else if (classname == QLatin1String("QDialog")) {
iface = new QAccessibleWidget(widget, QAccessible::Dialog);
} else if (classname == QLatin1String("QMessageBox")) {

View File

@ -84,12 +84,14 @@ QString qt_accHotKey(const QString &text);
/*!
Creates a QAccessibleButton object for \a w.
\a role is propagated to the QAccessibleWidget constructor.
*/
QAccessibleButton::QAccessibleButton(QWidget *w, QAccessible::Role role)
: QAccessibleWidget(w, role)
QAccessibleButton::QAccessibleButton(QWidget *w)
: QAccessibleWidget(w)
{
Q_ASSERT(button());
// FIXME: The checkable state of the button is dynamic,
// while we only update the controlling signal once :(
if (button()->isCheckable())
addControllingSignal(QLatin1String("toggled(bool)"));
else
@ -178,6 +180,23 @@ QRect QAccessibleButton::rect() const
return QAccessibleWidget::rect();
}
QAccessible::Role QAccessibleButton::role() const
{
QAbstractButton *ab = button();
#ifndef QT_NO_MENU
if (QPushButton *pb = qobject_cast<QPushButton*>(ab)) {
if (pb->menu())
return QAccessible::ButtonMenu;
}
#endif
if (ab->isCheckable())
return ab->autoExclusive() ? QAccessible::RadioButton : QAccessible::CheckBox;
return QAccessible::Button;
}
QStringList QAccessibleButton::actionNames() const
{
QStringList names;
@ -244,10 +263,9 @@ QStringList QAccessibleButton::keyBindingsForAction(const QString &actionName) c
/*!
Creates a QAccessibleToolButton object for \a w.
\a role is propagated to the QAccessibleWidget constructor.
*/
QAccessibleToolButton::QAccessibleToolButton(QWidget *w, QAccessible::Role role)
: QAccessibleButton(w, role)
QAccessibleToolButton::QAccessibleToolButton(QWidget *w)
: QAccessibleButton(w)
{
Q_ASSERT(toolButton());
}
@ -287,6 +305,21 @@ int QAccessibleToolButton::childCount() const
return isSplitButton() ? 1 : 0;
}
QAccessible::Role QAccessibleToolButton::role() const
{
QAbstractButton *ab = button();
#ifndef QT_NO_MENU
QToolButton *tb = qobject_cast<QToolButton*>(ab);
if (!tb->menu())
return tb->isCheckable() ? QAccessible::CheckBox : QAccessible::PushButton;
else if (tb->popupMode() == QToolButton::DelayedPopup)
return QAccessible::ButtonDropDown;
#endif
return QAccessible::ButtonMenu;
}
QAccessibleInterface *QAccessibleToolButton::child(int index) const
{
#ifndef QT_NO_MENU

View File

@ -59,11 +59,12 @@ class QAccessibleButton : public QAccessibleWidget
{
Q_DECLARE_TR_FUNCTIONS(QAccessibleButton)
public:
QAccessibleButton(QWidget *w, QAccessible::Role r);
QAccessibleButton(QWidget *w);
QString text(QAccessible::Text t) const Q_DECL_OVERRIDE;
QAccessible::State state() const Q_DECL_OVERRIDE;
QRect rect() const Q_DECL_OVERRIDE;
QAccessible::Role role() const Q_DECL_OVERRIDE;
QStringList actionNames() const Q_DECL_OVERRIDE;
void doAction(const QString &actionName) Q_DECL_OVERRIDE;
@ -77,9 +78,10 @@ protected:
class QAccessibleToolButton : public QAccessibleButton
{
public:
QAccessibleToolButton(QWidget *w, QAccessible::Role role);
QAccessibleToolButton(QWidget *w);
QAccessible::State state() const Q_DECL_OVERRIDE;
QAccessible::Role role() const Q_DECL_OVERRIDE;
int childCount() const Q_DECL_OVERRIDE;
QAccessibleInterface *child(int index) const Q_DECL_OVERRIDE;