Add support for accessibility actions.

Match Cocoa and Qt actions manually. Some have no
corresponding action on the other side.

Change-Id: I775cb8987ab843bd88d57d856ef7c0403290db00
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@nokia.com>
This commit is contained in:
Morten Johan Sorvig 2012-01-10 11:54:54 +01:00 committed by Qt by Nokia
parent d060b6f04f
commit 630131d585
4 changed files with 95 additions and 6 deletions

View File

@ -45,6 +45,8 @@
#include <QtGui>
namespace QCocoaAccessible {
/*
Qt Cocoa Accessibility Overview
@ -64,5 +66,10 @@
*/
NSString *macRole(QAccessible::Role);
NSString *getTranslatedAction(const QString &qtAction);
NSMutableArray *createTranslatedActionsList(const QStringList &qtActions);
QString translateAction(NSString *nsAction);
}
#endif

View File

@ -40,6 +40,8 @@
****************************************************************************/
#include "qcocoaaccessibility.h"
namespace QCocoaAccessible {
typedef QMap<QAccessible::Role, NSString *> QMacAccessibiltyRoleMap;
Q_GLOBAL_STATIC(QMacAccessibiltyRoleMap, qMacAccessibiltyRoleMap);
@ -111,3 +113,60 @@ NSString *macRole(QAccessible::Role qtRole)
return NSAccessibilityUnknownRole;
}
/*
Translates a predefined QAccessibleActionInterface action to a Mac action constant.
Returns 0 if the Qt Action has no mac equivalent. Ownership of the NSString is
not transferred.
*/
NSString *getTranslatedAction(const QString &qtAction)
{
if (qtAction == QAccessibleActionInterface::pressAction())
return NSAccessibilityPressAction;
else if (qtAction == QAccessibleActionInterface::increaseAction())
return NSAccessibilityIncrementAction;
else if (qtAction == QAccessibleActionInterface::decreaseAction())
return NSAccessibilityDecrementAction;
else if (qtAction == QAccessibleActionInterface::showMenuAction())
return NSAccessibilityShowMenuAction;
else if (qtAction == QAccessibleActionInterface::setFocusAction()) // Not 100% sure on this one
return NSAccessibilityRaiseAction;
// Not translated:
//
// Qt:
// static const QString &checkAction();
// static const QString &uncheckAction();
//
// Cocoa:
// NSAccessibilityConfirmAction;
// NSAccessibilityPickAction;
// NSAccessibilityCancelAction;
// NSAccessibilityDeleteAction;
return 0;
}
/*
Translates between a Mac action constant and a QAccessibleActionInterface action
Returns an empty QString if there is no Qt predefined equivalent.
*/
QString translateAction(NSString *nsAction)
{
if ([nsAction compare: NSAccessibilityPressAction] == NSOrderedSame)
return QAccessibleActionInterface::pressAction();
else if ([nsAction compare: NSAccessibilityIncrementAction] == NSOrderedSame)
return QAccessibleActionInterface::increaseAction();
else if ([nsAction compare: NSAccessibilityDecrementAction] == NSOrderedSame)
return QAccessibleActionInterface::decreaseAction();
else if ([nsAction compare: NSAccessibilityShowMenuAction] == NSOrderedSame)
return QAccessibleActionInterface::showMenuAction();
else if ([nsAction compare: NSAccessibilityRaiseAction] == NSOrderedSame)
return QAccessibleActionInterface::setFocusAction();
// See getTranslatedAction for not matched translations.
return QString();
}
} // namespace QCocoaAccessible

View File

@ -60,7 +60,7 @@ static QAccessibleInterface *acast(void *ptr)
if (self) {
index = aIndex;
accessibleInterface = anQAccessibleInterface;
role = macRole(acast(accessibleInterface)->role());
role = QCocoaAccessible::macRole(acast(accessibleInterface)->role());
parent = aParent;
}
@ -177,17 +177,40 @@ static QAccessibleInterface *acast(void *ptr)
// actions
- (NSArray *)accessibilityActionNames {
return [NSArray arrayWithObject:NSAccessibilityPressAction];
NSMutableArray * nsActions = [NSMutableArray new];
QAccessibleActionInterface *actionInterface = acast(accessibleInterface)->actionInterface();
if (actionInterface) {
QStringList supportedActionNames = actionInterface->actionNames();
foreach (const QString &qtAction, supportedActionNames) {
NSString *nsAction = QCocoaAccessible::getTranslatedAction(qtAction);
if (nsAction)
[nsActions addObject : nsAction];
}
}
return nsActions;
}
- (NSString *)accessibilityActionDescription:(NSString *)action {
QAccessibleActionInterface *actionInterface = acast(accessibleInterface)->actionInterface();
if (actionInterface) {
QString qtAction = QCocoaAccessible::translateAction(action);
QString description = actionInterface->localizedActionDescription(qtAction);
if (!description.isEmpty())
return qt_mac_QStringToNSString(description);
}
return NSAccessibilityActionDescription(action);
}
- (void)accessibilityPerformAction:(NSString *)action {
Q_UNUSED(action);
if (acast(accessibleInterface)->actionInterface())
acast(accessibleInterface)->actionInterface()->doAction(0);
QAccessibleActionInterface *actionInterface = acast(accessibleInterface)->actionInterface();
if (actionInterface) {
QString qtAction = QCocoaAccessible::translateAction(action);
actionInterface->doAction(QAccessibleActionInterface::pressAction());
}
}
// misc

View File

@ -60,7 +60,7 @@
- (id)accessibilityAttributeValue:(NSString *)attribute {
if ([attribute isEqualToString:NSAccessibilityRoleAttribute]) {
if (m_accessibleRoot)
return macRole(m_accessibleRoot->role());
return QCocoaAccessible::macRole(m_accessibleRoot->role());
return NSAccessibilityUnknownRole;
} else if ([attribute isEqualToString:NSAccessibilityRoleDescriptionAttribute]) {
return NSAccessibilityRoleDescriptionForUIElement(self);