Add support for accessibility actions.
authorMorten Johan Sorvig <morten.sorvig@nokia.com>
Tue, 10 Jan 2012 10:54:54 +0000 (11:54 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 11 Jan 2012 10:18:51 +0000 (11:18 +0100)
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>
src/plugins/platforms/cocoa/qcocoaaccessibility.h
src/plugins/platforms/cocoa/qcocoaaccessibility.mm
src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm
src/plugins/platforms/cocoa/qnsviewaccessibility.mm

index 36795c5..4953be0 100644 (file)
@@ -45,6 +45,8 @@
 
 #include <QtGui>
 
+namespace QCocoaAccessible {
+
 /*
     Qt Cocoa Accessibility Overview
 
 */
 
 NSString *macRole(QAccessible::Role);
+NSString *getTranslatedAction(const QString &qtAction);
+NSMutableArray *createTranslatedActionsList(const QStringList &qtActions);
+QString translateAction(NSString *nsAction);
+
+}
 
 #endif
index 4b58ef2..26ff953 100644 (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
index 11cc89f..1441a02 100644 (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
index 2e59931..8cb241a 100644 (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);